blob: 2e3d9fd7725ea625a980443b93f4a6edcc6dad06 [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
129/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000130 * When recursively copying lists and dicts we need to remember which ones we
131 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000132 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133 */
134static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000135#define COPYID_INC 2
136#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137
138/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000139 * Array to hold the hashtab with variables local to each sourced script.
140 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000142typedef struct
143{
144 dictitem_T sv_var;
145 dict_T sv_dict;
146} scriptvar_T;
147
148static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
149#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
150#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152static int echo_attr = 0; /* attributes used for ":echo" */
153
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000154/* Values for trans_function_name() argument: */
155#define TFN_INT 1 /* internal function name OK */
156#define TFN_QUIET 2 /* no error messages */
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158/*
159 * Structure to hold info for a user function.
160 */
161typedef struct ufunc ufunc_T;
162
163struct ufunc
164{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000165 int uf_varargs; /* variable nr of arguments */
166 int uf_flags;
167 int uf_calls; /* nr of active calls */
168 garray_T uf_args; /* arguments */
169 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000170#ifdef FEAT_PROFILE
171 int uf_profiling; /* TRUE when func is being profiled */
172 /* profiling the function as a whole */
173 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000174 proftime_T uf_tm_total; /* time spent in function + children */
175 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000176 proftime_T uf_tm_children; /* time spent in children this call */
177 /* profiling the function per line */
178 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000179 proftime_T *uf_tml_total; /* time spent in a line + children */
180 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000181 proftime_T uf_tml_start; /* start time for current line */
182 proftime_T uf_tml_children; /* time spent in children for this line */
183 proftime_T uf_tml_wait; /* start wait time for current line */
184 int uf_tml_idx; /* index of line being timed; -1 if none */
185 int uf_tml_execed; /* line being timed was executed */
186#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000187 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000189 int uf_refcount; /* for numbered function: reference count */
190 char_u uf_name[1]; /* name of function (actually longer); can
191 start with <SNR>123_ (<SNR> is K_SPECIAL
192 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193};
194
195/* function flags */
196#define FC_ABORT 1 /* abort function on error */
197#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000198#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199
200/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000203static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000205/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000206static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
207
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208/* list heads for garbage collection */
209static dict_T *first_dict = NULL; /* list of all dicts */
210static list_T *first_list = NULL; /* list of all lists */
211
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000212/* From user function to hashitem and back. */
213static ufunc_T dumuf;
214#define UF2HIKEY(fp) ((fp)->uf_name)
215#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
216#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
217
218#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
219#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220
Bram Moolenaar33570922005-01-25 22:26:29 +0000221#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
222#define VAR_SHORT_LEN 20 /* short variable name length */
223#define FIXVAR_CNT 12 /* number of fixed variables */
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000226typedef struct funccall_S funccall_T;
227
228struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229{
230 ufunc_T *func; /* function being called */
231 int linenr; /* next line to be executed */
232 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000233 struct /* fixed variables for arguments */
234 {
235 dictitem_T var; /* variable (without room for name) */
236 char_u room[VAR_SHORT_LEN]; /* room for the name */
237 } fixvar[FIXVAR_CNT];
238 dict_T l_vars; /* l: local function variables */
239 dictitem_T l_vars_var; /* variable for l: scope */
240 dict_T l_avars; /* a: argument variables */
241 dictitem_T l_avars_var; /* variable for a: scope */
242 list_T l_varlist; /* list for a:000 */
243 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
244 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 linenr_T breakpoint; /* next line with breakpoint or zero */
246 int dbg_tick; /* debug_tick when breakpoint was set */
247 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000248#ifdef FEAT_PROFILE
249 proftime_T prof_child; /* time spent in a child */
250#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000251 funccall_T *caller; /* calling function or NULL */
252};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253
254/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000255 * Info used by a ":for" loop.
256 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000257typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258{
259 int fi_semicolon; /* TRUE if ending in '; var]' */
260 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261 listwatch_T fi_lw; /* keep an eye on the item used. */
262 list_T *fi_list; /* list being used */
263} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000266 * Struct used by trans_function_name()
267 */
268typedef struct
269{
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000271 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000272 dictitem_T *fd_di; /* Dictionary item used */
273} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000274
Bram Moolenaara7043832005-01-21 11:56:39 +0000275
276/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 * Array to hold the value of v: variables.
278 * The value is in a dictitem, so that it can also be used in the v: scope.
279 * The reason to use this table anyway is for very quick access to the
280 * variables with the VV_ defines.
281 */
282#include "version.h"
283
284/* values for vv_flags: */
285#define VV_COMPAT 1 /* compatible, also used without "v:" */
286#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000287#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000288
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000289#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000290
291static struct vimvar
292{
293 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294 dictitem_T vv_di; /* value and name for key */
295 char vv_filler[16]; /* space for LONGEST name below!!! */
296 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
297} vimvars[VV_LEN] =
298{
299 /*
300 * The order here must match the VV_ defines in vim.h!
301 * Initializing a union does not work, leave tv.vval empty to get zero's.
302 */
303 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
304 {VV_NAME("count1", VAR_NUMBER), VV_RO},
305 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
306 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
307 {VV_NAME("warningmsg", VAR_STRING), 0},
308 {VV_NAME("statusmsg", VAR_STRING), 0},
309 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
311 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
313 {VV_NAME("termresponse", VAR_STRING), VV_RO},
314 {VV_NAME("fname", VAR_STRING), VV_RO},
315 {VV_NAME("lang", VAR_STRING), VV_RO},
316 {VV_NAME("lc_time", VAR_STRING), VV_RO},
317 {VV_NAME("ctype", VAR_STRING), VV_RO},
318 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
319 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
320 {VV_NAME("fname_in", VAR_STRING), VV_RO},
321 {VV_NAME("fname_out", VAR_STRING), VV_RO},
322 {VV_NAME("fname_new", VAR_STRING), VV_RO},
323 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
324 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
325 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
326 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
327 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
328 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
329 {VV_NAME("progname", VAR_STRING), VV_RO},
330 {VV_NAME("servername", VAR_STRING), VV_RO},
331 {VV_NAME("dying", VAR_NUMBER), VV_RO},
332 {VV_NAME("exception", VAR_STRING), VV_RO},
333 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
334 {VV_NAME("register", VAR_STRING), VV_RO},
335 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
336 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000337 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
338 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000339 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000340 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
341 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000342 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
343 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
344 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
345 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
346 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000347 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000348 {VV_NAME("swapname", VAR_STRING), VV_RO},
349 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000350 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000351 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000352 {VV_NAME("mouse_win", VAR_NUMBER), 0},
353 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
354 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000355 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000356 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000357 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000358};
359
360/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000361#define vv_type vv_di.di_tv.v_type
362#define vv_nr vv_di.di_tv.vval.v_number
363#define vv_float vv_di.di_tv.vval.v_float
364#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000365#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000367
368/*
369 * The v: variables are stored in dictionary "vimvardict".
370 * "vimvars_var" is the variable that is used for the "l:" scope.
371 */
372static dict_T vimvardict;
373static dictitem_T vimvars_var;
374#define vimvarht vimvardict.dv_hashtab
375
Bram Moolenaara40058a2005-07-11 22:42:07 +0000376static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
377static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
378#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
379static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
380#endif
381static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
382static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
383static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000384static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
385static void list_glob_vars __ARGS((int *first));
386static void list_buf_vars __ARGS((int *first));
387static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000388#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000389static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_vim_vars __ARGS((int *first));
392static void list_script_vars __ARGS((int *first));
393static void list_func_vars __ARGS((int *first));
394static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000395static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
396static int check_changedtick __ARGS((char_u *arg));
397static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
398static void clear_lval __ARGS((lval_T *lp));
399static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
400static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
401static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
402static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
403static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
404static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
405static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
406static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
407static void item_lock __ARGS((typval_T *tv, int deep, int lock));
408static int tv_islocked __ARGS((typval_T *tv));
409
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
411static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000416static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
417static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000418
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000419static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000424static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static listitem_T *listitem_alloc __ARGS((void));
426static void listitem_free __ARGS((listitem_T *item));
427static void listitem_remove __ARGS((list_T *l, listitem_T *item));
428static long list_len __ARGS((list_T *l));
429static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
430static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
431static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000433static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static void list_append __ARGS((list_T *l, listitem_T *item));
436static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000437static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
439static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
440static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000441static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *list2string __ARGS((typval_T *tv, int copyID));
444static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000445static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000446static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
447static void set_ref_in_list __ARGS((list_T *l, int copyID));
448static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000450static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static dictitem_T *dictitem_alloc __ARGS((char_u *key));
452static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
453static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
454static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000455static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int dict_add __ARGS((dict_T *d, dictitem_T *item));
457static long dict_len __ARGS((dict_T *d));
458static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000459static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000461static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
462static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
465static int string2float __ARGS((char_u *text, float_T *value));
466#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000467static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
468static int find_internal_func __ARGS((char_u *name));
469static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
470static 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));
471static int call_func __ARGS((char_u *name, 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 +0000472static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000473static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000474
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000475#ifdef FEAT_FLOAT
476static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
477#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
484static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
485#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#ifdef FEAT_FLOAT
498static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
499#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000500static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000503static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000505#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000506static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
509#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000512#ifdef FEAT_FLOAT
513static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
514#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
518static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000531static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000532static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000537#ifdef FEAT_FLOAT
538static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
540#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000541static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000550static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000552static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000558static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000566static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000567static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000568static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000569static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000570static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000572static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000573static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000580static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000594static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000600static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000612#ifdef FEAT_FLOAT
613static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
614#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000619static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000620static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000621static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000623static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000627#ifdef vim_mkdir
628static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
629#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000633static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000634#ifdef FEAT_FLOAT
635static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000638static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000639static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000641static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000642static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000654#ifdef FEAT_FLOAT
655static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
656#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000658static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000660static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000667static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000668static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000669static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000670static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000672static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000674static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000676#ifdef FEAT_FLOAT
677static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
678#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000680static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000681static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000684#ifdef FEAT_FLOAT
685static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
687#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000688static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689#ifdef HAVE_STRFTIME
690static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
691#endif
692static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
693static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
696static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
697static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
699static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
701static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000703static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000705static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000706static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000707static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000708static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000709static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000711static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000715#ifdef FEAT_FLOAT
716static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
717#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000718static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
726static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
727static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000728static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000731static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000732
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000733static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000734static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static int get_env_len __ARGS((char_u **arg));
736static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000737static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000738static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
739#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
740#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
741 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000742static 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 +0000743static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000744static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000745static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
746static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static typval_T *alloc_tv __ARGS((void));
748static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static void init_tv __ARGS((typval_T *varp));
750static long get_tv_number __ARGS((typval_T *varp));
751static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000752static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000753static char_u *get_tv_string __ARGS((typval_T *varp));
754static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000755static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000757static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
759static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
760static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000761static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
762static 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 +0000763static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
764static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000765static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000766static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000767static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000768static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
770static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
771static int eval_fname_script __ARGS((char_u *p));
772static int eval_fname_sid __ARGS((char_u *p));
773static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static ufunc_T *find_func __ARGS((char_u *name));
775static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000776static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000777#ifdef FEAT_PROFILE
778static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000779static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
780static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
781static int
782# ifdef __BORLANDC__
783 _RTLENTRYF
784# endif
785 prof_total_cmp __ARGS((const void *s1, const void *s2));
786static int
787# ifdef __BORLANDC__
788 _RTLENTRYF
789# endif
790 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000791#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000792static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000793static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000794static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000795static void func_free __ARGS((ufunc_T *fp));
796static void func_unref __ARGS((char_u *name));
797static void func_ref __ARGS((char_u *name));
798static 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 +0000799static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
800static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000802static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
803static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000804static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000805static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000806static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000807
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000808/* Character used as separated in autoload function/variable names. */
809#define AUTOLOAD_CHAR '#'
810
Bram Moolenaar33570922005-01-25 22:26:29 +0000811/*
812 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000813 */
814 void
815eval_init()
816{
Bram Moolenaar33570922005-01-25 22:26:29 +0000817 int i;
818 struct vimvar *p;
819
820 init_var_dict(&globvardict, &globvars_var);
821 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000822 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000823 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000824
825 for (i = 0; i < VV_LEN; ++i)
826 {
827 p = &vimvars[i];
828 STRCPY(p->vv_di.di_key, p->vv_name);
829 if (p->vv_flags & VV_RO)
830 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
831 else if (p->vv_flags & VV_RO_SBX)
832 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
833 else
834 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000835
836 /* add to v: scope dict, unless the value is not always available */
837 if (p->vv_type != VAR_UNKNOWN)
838 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000839 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000840 /* add to compat scope dict */
841 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000842 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000843 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaara7043832005-01-21 11:56:39 +0000844}
845
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000846#if defined(EXITFREE) || defined(PROTO)
847 void
848eval_clear()
849{
850 int i;
851 struct vimvar *p;
852
853 for (i = 0; i < VV_LEN; ++i)
854 {
855 p = &vimvars[i];
856 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000857 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000858 vim_free(p->vv_str);
859 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000860 }
861 else if (p->vv_di.di_tv.v_type == VAR_LIST)
862 {
863 list_unref(p->vv_list);
864 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000865 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000866 }
867 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000868 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000869 hash_clear(&compat_hashtab);
870
871 /* script-local variables */
872 for (i = 1; i <= ga_scripts.ga_len; ++i)
873 vars_clear(&SCRIPT_VARS(i));
874 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000875 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000876
877 /* global variables */
878 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000879
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000880 /* autoloaded script names */
881 ga_clear_strings(&ga_loaded);
882
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000883 /* unreferenced lists and dicts */
884 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000885
886 /* functions */
887 free_all_functions();
888 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000889}
890#endif
891
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 * Return the name of the executed function.
894 */
895 char_u *
896func_name(cookie)
897 void *cookie;
898{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000899 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900}
901
902/*
903 * Return the address holding the next breakpoint line for a funccall cookie.
904 */
905 linenr_T *
906func_breakpoint(cookie)
907 void *cookie;
908{
Bram Moolenaar33570922005-01-25 22:26:29 +0000909 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910}
911
912/*
913 * Return the address holding the debug tick for a funccall cookie.
914 */
915 int *
916func_dbg_tick(cookie)
917 void *cookie;
918{
Bram Moolenaar33570922005-01-25 22:26:29 +0000919 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920}
921
922/*
923 * Return the nesting level for a funccall cookie.
924 */
925 int
926func_level(cookie)
927 void *cookie;
928{
Bram Moolenaar33570922005-01-25 22:26:29 +0000929 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930}
931
932/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000933funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000935/* pointer to list of previously used funccal, still around because some
936 * item in it is still being used. */
937funccall_T *previous_funccal = NULL;
938
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939/*
940 * Return TRUE when a function was ended by a ":return" command.
941 */
942 int
943current_func_returned()
944{
945 return current_funccal->returned;
946}
947
948
949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 * Set an internal variable to a string value. Creates the variable if it does
951 * not already exist.
952 */
953 void
954set_internal_string_var(name, value)
955 char_u *name;
956 char_u *value;
957{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000958 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000959 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961 val = vim_strsave(value);
962 if (val != NULL)
963 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000964 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000965 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000967 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000968 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 }
970 }
971}
972
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000973static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000974static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000975static char_u *redir_endp = NULL;
976static char_u *redir_varname = NULL;
977
978/*
979 * Start recording command output to a variable
980 * Returns OK if successfully completed the setup. FAIL otherwise.
981 */
982 int
983var_redir_start(name, append)
984 char_u *name;
985 int append; /* append to an existing variable */
986{
987 int save_emsg;
988 int err;
989 typval_T tv;
990
991 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000992 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000993 {
994 EMSG(_(e_invarg));
995 return FAIL;
996 }
997
998 redir_varname = vim_strsave(name);
999 if (redir_varname == NULL)
1000 return FAIL;
1001
1002 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1003 if (redir_lval == NULL)
1004 {
1005 var_redir_stop();
1006 return FAIL;
1007 }
1008
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001009 /* The output is stored in growarray "redir_ga" until redirection ends. */
1010 ga_init2(&redir_ga, (int)sizeof(char), 500);
1011
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001012 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001013 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1014 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001015 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1016 {
1017 if (redir_endp != NULL && *redir_endp != NUL)
1018 /* Trailing characters are present after the variable name */
1019 EMSG(_(e_trailing));
1020 else
1021 EMSG(_(e_invarg));
1022 var_redir_stop();
1023 return FAIL;
1024 }
1025
1026 /* check if we can write to the variable: set it to or append an empty
1027 * string */
1028 save_emsg = did_emsg;
1029 did_emsg = FALSE;
1030 tv.v_type = VAR_STRING;
1031 tv.vval.v_string = (char_u *)"";
1032 if (append)
1033 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1034 else
1035 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1036 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001037 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038 if (err)
1039 {
1040 var_redir_stop();
1041 return FAIL;
1042 }
1043 if (redir_lval->ll_newkey != NULL)
1044 {
1045 /* Dictionary item was created, don't do it again. */
1046 vim_free(redir_lval->ll_newkey);
1047 redir_lval->ll_newkey = NULL;
1048 }
1049
1050 return OK;
1051}
1052
1053/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001054 * Append "value[value_len]" to the variable set by var_redir_start().
1055 * The actual appending is postponed until redirection ends, because the value
1056 * appended may in fact be the string we write to, changing it may cause freed
1057 * memory to be used:
1058 * :redir => foo
1059 * :let foo
1060 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 */
1062 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001063var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001065 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001067 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068
1069 if (redir_lval == NULL)
1070 return;
1071
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001072 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001073 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001074 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001075 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001077 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001078 {
1079 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001080 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001081 }
1082 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084}
1085
1086/*
1087 * Stop redirecting command output to a variable.
1088 */
1089 void
1090var_redir_stop()
1091{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001092 typval_T tv;
1093
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094 if (redir_lval != NULL)
1095 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001096 /* Append the trailing NUL. */
1097 ga_append(&redir_ga, NUL);
1098
1099 /* Assign the text to the variable. */
1100 tv.v_type = VAR_STRING;
1101 tv.vval.v_string = redir_ga.ga_data;
1102 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1103 vim_free(tv.vval.v_string);
1104
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 clear_lval(redir_lval);
1106 vim_free(redir_lval);
1107 redir_lval = NULL;
1108 }
1109 vim_free(redir_varname);
1110 redir_varname = NULL;
1111}
1112
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113# if defined(FEAT_MBYTE) || defined(PROTO)
1114 int
1115eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1116 char_u *enc_from;
1117 char_u *enc_to;
1118 char_u *fname_from;
1119 char_u *fname_to;
1120{
1121 int err = FALSE;
1122
1123 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1124 set_vim_var_string(VV_CC_TO, enc_to, -1);
1125 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1126 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1127 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1128 err = TRUE;
1129 set_vim_var_string(VV_CC_FROM, NULL, -1);
1130 set_vim_var_string(VV_CC_TO, NULL, -1);
1131 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1132 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1133
1134 if (err)
1135 return FAIL;
1136 return OK;
1137}
1138# endif
1139
1140# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1141 int
1142eval_printexpr(fname, args)
1143 char_u *fname;
1144 char_u *args;
1145{
1146 int err = FALSE;
1147
1148 set_vim_var_string(VV_FNAME_IN, fname, -1);
1149 set_vim_var_string(VV_CMDARG, args, -1);
1150 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1151 err = TRUE;
1152 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1153 set_vim_var_string(VV_CMDARG, NULL, -1);
1154
1155 if (err)
1156 {
1157 mch_remove(fname);
1158 return FAIL;
1159 }
1160 return OK;
1161}
1162# endif
1163
1164# if defined(FEAT_DIFF) || defined(PROTO)
1165 void
1166eval_diff(origfile, newfile, outfile)
1167 char_u *origfile;
1168 char_u *newfile;
1169 char_u *outfile;
1170{
1171 int err = FALSE;
1172
1173 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1174 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1175 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1176 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1177 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1178 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1179 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1180}
1181
1182 void
1183eval_patch(origfile, difffile, outfile)
1184 char_u *origfile;
1185 char_u *difffile;
1186 char_u *outfile;
1187{
1188 int err;
1189
1190 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1191 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1192 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1193 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1194 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1195 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1196 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1197}
1198# endif
1199
1200/*
1201 * Top level evaluation function, returning a boolean.
1202 * Sets "error" to TRUE if there was an error.
1203 * Return TRUE or FALSE.
1204 */
1205 int
1206eval_to_bool(arg, error, nextcmd, skip)
1207 char_u *arg;
1208 int *error;
1209 char_u **nextcmd;
1210 int skip; /* only parse, don't execute */
1211{
Bram Moolenaar33570922005-01-25 22:26:29 +00001212 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 int retval = FALSE;
1214
1215 if (skip)
1216 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001217 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 else
1220 {
1221 *error = FALSE;
1222 if (!skip)
1223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001224 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 }
1227 }
1228 if (skip)
1229 --emsg_skip;
1230
1231 return retval;
1232}
1233
1234/*
1235 * Top level evaluation function, returning a string. If "skip" is TRUE,
1236 * only parsing to "nextcmd" is done, without reporting errors. Return
1237 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1238 */
1239 char_u *
1240eval_to_string_skip(arg, nextcmd, skip)
1241 char_u *arg;
1242 char_u **nextcmd;
1243 int skip; /* only parse, don't execute */
1244{
Bram Moolenaar33570922005-01-25 22:26:29 +00001245 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 char_u *retval;
1247
1248 if (skip)
1249 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001250 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 retval = NULL;
1252 else
1253 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001254 retval = vim_strsave(get_tv_string(&tv));
1255 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 }
1257 if (skip)
1258 --emsg_skip;
1259
1260 return retval;
1261}
1262
1263/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001264 * Skip over an expression at "*pp".
1265 * Return FAIL for an error, OK otherwise.
1266 */
1267 int
1268skip_expr(pp)
1269 char_u **pp;
1270{
Bram Moolenaar33570922005-01-25 22:26:29 +00001271 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001272
1273 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001274 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001275}
1276
1277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001279 * When "convert" is TRUE convert a List into a sequence of lines and convert
1280 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 * Return pointer to allocated memory, or NULL for failure.
1282 */
1283 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001284eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 char_u *arg;
1286 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001287 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288{
Bram Moolenaar33570922005-01-25 22:26:29 +00001289 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001291 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001292#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001293 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001296 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 retval = NULL;
1298 else
1299 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001300 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001301 {
1302 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001303 if (tv.vval.v_list != NULL)
1304 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001305 ga_append(&ga, NUL);
1306 retval = (char_u *)ga.ga_data;
1307 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001308#ifdef FEAT_FLOAT
1309 else if (convert && tv.v_type == VAR_FLOAT)
1310 {
1311 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1312 retval = vim_strsave(numbuf);
1313 }
1314#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001315 else
1316 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001317 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
1319
1320 return retval;
1321}
1322
1323/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001324 * Call eval_to_string() without using current local variables and using
1325 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 */
1327 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001328eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 char_u *arg;
1330 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001331 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332{
1333 char_u *retval;
1334 void *save_funccalp;
1335
1336 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001337 if (use_sandbox)
1338 ++sandbox;
1339 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001340 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001341 if (use_sandbox)
1342 --sandbox;
1343 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 restore_funccal(save_funccalp);
1345 return retval;
1346}
1347
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348/*
1349 * Top level evaluation function, returning a number.
1350 * Evaluates "expr" silently.
1351 * Returns -1 for an error.
1352 */
1353 int
1354eval_to_number(expr)
1355 char_u *expr;
1356{
Bram Moolenaar33570922005-01-25 22:26:29 +00001357 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001359 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
1361 ++emsg_off;
1362
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001363 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 retval = -1;
1365 else
1366 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001367 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001368 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 }
1370 --emsg_off;
1371
1372 return retval;
1373}
1374
Bram Moolenaara40058a2005-07-11 22:42:07 +00001375/*
1376 * Prepare v: variable "idx" to be used.
1377 * Save the current typeval in "save_tv".
1378 * When not used yet add the variable to the v: hashtable.
1379 */
1380 static void
1381prepare_vimvar(idx, save_tv)
1382 int idx;
1383 typval_T *save_tv;
1384{
1385 *save_tv = vimvars[idx].vv_tv;
1386 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1387 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1388}
1389
1390/*
1391 * Restore v: variable "idx" to typeval "save_tv".
1392 * When no longer defined, remove the variable from the v: hashtable.
1393 */
1394 static void
1395restore_vimvar(idx, save_tv)
1396 int idx;
1397 typval_T *save_tv;
1398{
1399 hashitem_T *hi;
1400
Bram Moolenaara40058a2005-07-11 22:42:07 +00001401 vimvars[idx].vv_tv = *save_tv;
1402 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1403 {
1404 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1405 if (HASHITEM_EMPTY(hi))
1406 EMSG2(_(e_intern2), "restore_vimvar()");
1407 else
1408 hash_remove(&vimvarht, hi);
1409 }
1410}
1411
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001412#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001413/*
1414 * Evaluate an expression to a list with suggestions.
1415 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001416 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001417 */
1418 list_T *
1419eval_spell_expr(badword, expr)
1420 char_u *badword;
1421 char_u *expr;
1422{
1423 typval_T save_val;
1424 typval_T rettv;
1425 list_T *list = NULL;
1426 char_u *p = skipwhite(expr);
1427
1428 /* Set "v:val" to the bad word. */
1429 prepare_vimvar(VV_VAL, &save_val);
1430 vimvars[VV_VAL].vv_type = VAR_STRING;
1431 vimvars[VV_VAL].vv_str = badword;
1432 if (p_verbose == 0)
1433 ++emsg_off;
1434
1435 if (eval1(&p, &rettv, TRUE) == OK)
1436 {
1437 if (rettv.v_type != VAR_LIST)
1438 clear_tv(&rettv);
1439 else
1440 list = rettv.vval.v_list;
1441 }
1442
1443 if (p_verbose == 0)
1444 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001445 restore_vimvar(VV_VAL, &save_val);
1446
1447 return list;
1448}
1449
1450/*
1451 * "list" is supposed to contain two items: a word and a number. Return the
1452 * word in "pp" and the number as the return value.
1453 * Return -1 if anything isn't right.
1454 * Used to get the good word and score from the eval_spell_expr() result.
1455 */
1456 int
1457get_spellword(list, pp)
1458 list_T *list;
1459 char_u **pp;
1460{
1461 listitem_T *li;
1462
1463 li = list->lv_first;
1464 if (li == NULL)
1465 return -1;
1466 *pp = get_tv_string(&li->li_tv);
1467
1468 li = li->li_next;
1469 if (li == NULL)
1470 return -1;
1471 return get_tv_number(&li->li_tv);
1472}
1473#endif
1474
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001475/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001476 * Top level evaluation function.
1477 * Returns an allocated typval_T with the result.
1478 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001479 */
1480 typval_T *
1481eval_expr(arg, nextcmd)
1482 char_u *arg;
1483 char_u **nextcmd;
1484{
1485 typval_T *tv;
1486
1487 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001488 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001489 {
1490 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001491 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001492 }
1493
1494 return tv;
1495}
1496
1497
Bram Moolenaar4f688582007-07-24 12:34:30 +00001498#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1499 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001501 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001502 * Uses argv[argc] for the function arguments. Only Number and String
1503 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001504 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001506 static int
1507call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 char_u *func;
1509 int argc;
1510 char_u **argv;
1511 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
Bram Moolenaar33570922005-01-25 22:26:29 +00001514 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 long n;
1516 int len;
1517 int i;
1518 int doesrange;
1519 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001520 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001522 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001524 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
1526 for (i = 0; i < argc; i++)
1527 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001528 /* Pass a NULL or empty argument as an empty string */
1529 if (argv[i] == NULL || *argv[i] == NUL)
1530 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001531 argvars[i].v_type = VAR_STRING;
1532 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001533 continue;
1534 }
1535
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 /* Recognize a number argument, the others must be strings. */
1537 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1538 if (len != 0 && len == (int)STRLEN(argv[i]))
1539 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001540 argvars[i].v_type = VAR_NUMBER;
1541 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 }
1543 else
1544 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001545 argvars[i].v_type = VAR_STRING;
1546 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 }
1548 }
1549
1550 if (safe)
1551 {
1552 save_funccalp = save_funccal();
1553 ++sandbox;
1554 }
1555
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1557 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001559 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 if (safe)
1561 {
1562 --sandbox;
1563 restore_funccal(save_funccalp);
1564 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 vim_free(argvars);
1566
1567 if (ret == FAIL)
1568 clear_tv(rettv);
1569
1570 return ret;
1571}
1572
Bram Moolenaar4f688582007-07-24 12:34:30 +00001573# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001575 * Call vimL function "func" and return the result as a string.
1576 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001577 * Uses argv[argc] for the function arguments.
1578 */
1579 void *
1580call_func_retstr(func, argc, argv, safe)
1581 char_u *func;
1582 int argc;
1583 char_u **argv;
1584 int safe; /* use the sandbox */
1585{
1586 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001587 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001588
1589 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1590 return NULL;
1591
1592 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001593 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 return retval;
1595}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001596# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001597
Bram Moolenaar4f688582007-07-24 12:34:30 +00001598# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001599/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001600 * Call vimL function "func" and return the result as a number.
1601 * Returns -1 when calling the function fails.
1602 * Uses argv[argc] for the function arguments.
1603 */
1604 long
1605call_func_retnr(func, argc, argv, safe)
1606 char_u *func;
1607 int argc;
1608 char_u **argv;
1609 int safe; /* use the sandbox */
1610{
1611 typval_T rettv;
1612 long retval;
1613
1614 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1615 return -1;
1616
1617 retval = get_tv_number_chk(&rettv, NULL);
1618 clear_tv(&rettv);
1619 return retval;
1620}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001621# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001622
1623/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001624 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001625 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001626 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 */
1628 void *
1629call_func_retlist(func, argc, argv, safe)
1630 char_u *func;
1631 int argc;
1632 char_u **argv;
1633 int safe; /* use the sandbox */
1634{
1635 typval_T rettv;
1636
1637 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1638 return NULL;
1639
1640 if (rettv.v_type != VAR_LIST)
1641 {
1642 clear_tv(&rettv);
1643 return NULL;
1644 }
1645
1646 return rettv.vval.v_list;
1647}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648#endif
1649
Bram Moolenaar4f688582007-07-24 12:34:30 +00001650
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651/*
1652 * Save the current function call pointer, and set it to NULL.
1653 * Used when executing autocommands and for ":source".
1654 */
1655 void *
1656save_funccal()
1657{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001658 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 current_funccal = NULL;
1661 return (void *)fc;
1662}
1663
1664 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001665restore_funccal(vfc)
1666 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001668 funccall_T *fc = (funccall_T *)vfc;
1669
1670 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671}
1672
Bram Moolenaar05159a02005-02-26 23:04:13 +00001673#if defined(FEAT_PROFILE) || defined(PROTO)
1674/*
1675 * Prepare profiling for entering a child or something else that is not
1676 * counted for the script/function itself.
1677 * Should always be called in pair with prof_child_exit().
1678 */
1679 void
1680prof_child_enter(tm)
1681 proftime_T *tm; /* place to store waittime */
1682{
1683 funccall_T *fc = current_funccal;
1684
1685 if (fc != NULL && fc->func->uf_profiling)
1686 profile_start(&fc->prof_child);
1687 script_prof_save(tm);
1688}
1689
1690/*
1691 * Take care of time spent in a child.
1692 * Should always be called after prof_child_enter().
1693 */
1694 void
1695prof_child_exit(tm)
1696 proftime_T *tm; /* where waittime was stored */
1697{
1698 funccall_T *fc = current_funccal;
1699
1700 if (fc != NULL && fc->func->uf_profiling)
1701 {
1702 profile_end(&fc->prof_child);
1703 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1704 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1705 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1706 }
1707 script_prof_restore(tm);
1708}
1709#endif
1710
1711
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712#ifdef FEAT_FOLDING
1713/*
1714 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1715 * it in "*cp". Doesn't give error messages.
1716 */
1717 int
1718eval_foldexpr(arg, cp)
1719 char_u *arg;
1720 int *cp;
1721{
Bram Moolenaar33570922005-01-25 22:26:29 +00001722 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 int retval;
1724 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001725 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1726 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
1728 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001729 if (use_sandbox)
1730 ++sandbox;
1731 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001733 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 retval = 0;
1735 else
1736 {
1737 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001738 if (tv.v_type == VAR_NUMBER)
1739 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001740 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 retval = 0;
1742 else
1743 {
1744 /* If the result is a string, check if there is a non-digit before
1745 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001746 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if (!VIM_ISDIGIT(*s) && *s != '-')
1748 *cp = *s++;
1749 retval = atol((char *)s);
1750 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001751 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 }
1753 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001754 if (use_sandbox)
1755 --sandbox;
1756 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
1758 return retval;
1759}
1760#endif
1761
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001763 * ":let" list all variable values
1764 * ":let var1 var2" list variable values
1765 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001766 * ":let var += expr" assignment command.
1767 * ":let var -= expr" assignment command.
1768 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001769 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 */
1771 void
1772ex_let(eap)
1773 exarg_T *eap;
1774{
1775 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001776 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001777 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779 int var_count = 0;
1780 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001781 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001782 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001783 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784
Bram Moolenaardb552d602006-03-23 22:59:57 +00001785 argend = skip_var_list(arg, &var_count, &semicolon);
1786 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001788 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1789 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001790 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001793 /*
1794 * ":let" without "=": list variables
1795 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001796 if (*arg == '[')
1797 EMSG(_(e_invarg));
1798 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001799 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001800 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001802 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001804 list_glob_vars(&first);
1805 list_buf_vars(&first);
1806 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001807#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001808 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001809#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001810 list_script_vars(&first);
1811 list_func_vars(&first);
1812 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 eap->nextcmd = check_nextcmd(arg);
1815 }
1816 else
1817 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001818 op[0] = '=';
1819 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001820 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001821 {
1822 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1823 op[0] = expr[-1]; /* +=, -= or .= */
1824 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001825 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001826
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if (eap->skip)
1828 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 if (eap->skip)
1831 {
1832 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 --emsg_skip;
1835 }
1836 else if (i != FAIL)
1837 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001838 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001839 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
1842 }
1843}
1844
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001845/*
1846 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1847 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001848 * When "nextchars" is not NULL it points to a string with characters that
1849 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1850 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 * Returns OK or FAIL;
1852 */
1853 static int
1854ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1855 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001856 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 int copy; /* copy values from "tv", don't move */
1858 int semicolon; /* from skip_var_list() */
1859 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001860 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861{
1862 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001863 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001865 listitem_T *item;
1866 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001867
1868 if (*arg != '[')
1869 {
1870 /*
1871 * ":let var = expr" or ":for var in list"
1872 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001873 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874 return FAIL;
1875 return OK;
1876 }
1877
1878 /*
1879 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1880 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001881 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001882 {
1883 EMSG(_(e_listreq));
1884 return FAIL;
1885 }
1886
1887 i = list_len(l);
1888 if (semicolon == 0 && var_count < i)
1889 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001890 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001891 return FAIL;
1892 }
1893 if (var_count - semicolon > i)
1894 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001895 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001896 return FAIL;
1897 }
1898
1899 item = l->lv_first;
1900 while (*arg != ']')
1901 {
1902 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001904 item = item->li_next;
1905 if (arg == NULL)
1906 return FAIL;
1907
1908 arg = skipwhite(arg);
1909 if (*arg == ';')
1910 {
1911 /* Put the rest of the list (may be empty) in the var after ';'.
1912 * Create a new list for this. */
1913 l = list_alloc();
1914 if (l == NULL)
1915 return FAIL;
1916 while (item != NULL)
1917 {
1918 list_append_tv(l, &item->li_tv);
1919 item = item->li_next;
1920 }
1921
1922 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001923 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 ltv.vval.v_list = l;
1925 l->lv_refcount = 1;
1926
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001927 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1928 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 clear_tv(&ltv);
1930 if (arg == NULL)
1931 return FAIL;
1932 break;
1933 }
1934 else if (*arg != ',' && *arg != ']')
1935 {
1936 EMSG2(_(e_intern2), "ex_let_vars()");
1937 return FAIL;
1938 }
1939 }
1940
1941 return OK;
1942}
1943
1944/*
1945 * Skip over assignable variable "var" or list of variables "[var, var]".
1946 * Used for ":let varvar = expr" and ":for varvar in expr".
1947 * For "[var, var]" increment "*var_count" for each variable.
1948 * for "[var, var; var]" set "semicolon".
1949 * Return NULL for an error.
1950 */
1951 static char_u *
1952skip_var_list(arg, var_count, semicolon)
1953 char_u *arg;
1954 int *var_count;
1955 int *semicolon;
1956{
1957 char_u *p, *s;
1958
1959 if (*arg == '[')
1960 {
1961 /* "[var, var]": find the matching ']'. */
1962 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001963 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 {
1965 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1966 s = skip_var_one(p);
1967 if (s == p)
1968 {
1969 EMSG2(_(e_invarg2), p);
1970 return NULL;
1971 }
1972 ++*var_count;
1973
1974 p = skipwhite(s);
1975 if (*p == ']')
1976 break;
1977 else if (*p == ';')
1978 {
1979 if (*semicolon == 1)
1980 {
1981 EMSG(_("Double ; in list of variables"));
1982 return NULL;
1983 }
1984 *semicolon = 1;
1985 }
1986 else if (*p != ',')
1987 {
1988 EMSG2(_(e_invarg2), p);
1989 return NULL;
1990 }
1991 }
1992 return p + 1;
1993 }
1994 else
1995 return skip_var_one(arg);
1996}
1997
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001998/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001999 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002000 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002001 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 static char_u *
2003skip_var_one(arg)
2004 char_u *arg;
2005{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002006 if (*arg == '@' && arg[1] != NUL)
2007 return arg + 2;
2008 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2009 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002010}
2011
Bram Moolenaara7043832005-01-21 11:56:39 +00002012/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002013 * List variables for hashtab "ht" with prefix "prefix".
2014 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002015 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002016 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002017list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002018 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002019 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002020 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002021 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002022{
Bram Moolenaar33570922005-01-25 22:26:29 +00002023 hashitem_T *hi;
2024 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002025 int todo;
2026
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002027 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002028 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2029 {
2030 if (!HASHITEM_EMPTY(hi))
2031 {
2032 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002033 di = HI2DI(hi);
2034 if (empty || di->di_tv.v_type != VAR_STRING
2035 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002036 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002037 }
2038 }
2039}
2040
2041/*
2042 * List global variables.
2043 */
2044 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002045list_glob_vars(first)
2046 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002047{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002048 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002049}
2050
2051/*
2052 * List buffer variables.
2053 */
2054 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002055list_buf_vars(first)
2056 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002057{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002058 char_u numbuf[NUMBUFLEN];
2059
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002060 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2061 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002062
2063 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002064 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2065 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002066}
2067
2068/*
2069 * List window variables.
2070 */
2071 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002072list_win_vars(first)
2073 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002074{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002075 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2076 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002077}
2078
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002079#ifdef FEAT_WINDOWS
2080/*
2081 * List tab page variables.
2082 */
2083 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002084list_tab_vars(first)
2085 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002086{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002087 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2088 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002089}
2090#endif
2091
Bram Moolenaara7043832005-01-21 11:56:39 +00002092/*
2093 * List Vim variables.
2094 */
2095 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002096list_vim_vars(first)
2097 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002099 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100}
2101
2102/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002103 * List script-local variables, if there is a script.
2104 */
2105 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002106list_script_vars(first)
2107 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002108{
2109 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2111 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002112}
2113
2114/*
2115 * List function variables, if there is a function.
2116 */
2117 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002118list_func_vars(first)
2119 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002120{
2121 if (current_funccal != NULL)
2122 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002124}
2125
2126/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002127 * List variables in "arg".
2128 */
2129 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002130list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002131 exarg_T *eap;
2132 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002134{
2135 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002136 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002137 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002138 char_u *name_start;
2139 char_u *arg_subsc;
2140 char_u *tofree;
2141 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002142
2143 while (!ends_excmd(*arg) && !got_int)
2144 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002145 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002146 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002147 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002148 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2149 {
2150 emsg_severe = TRUE;
2151 EMSG(_(e_trailing));
2152 break;
2153 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002155 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002156 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002157 /* get_name_len() takes care of expanding curly braces */
2158 name_start = name = arg;
2159 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2160 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002162 /* This is mainly to keep test 49 working: when expanding
2163 * curly braces fails overrule the exception error message. */
2164 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002165 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002166 emsg_severe = TRUE;
2167 EMSG2(_(e_invarg2), arg);
2168 break;
2169 }
2170 error = TRUE;
2171 }
2172 else
2173 {
2174 if (tofree != NULL)
2175 name = tofree;
2176 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002178 else
2179 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002180 /* handle d.key, l[idx], f(expr) */
2181 arg_subsc = arg;
2182 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002183 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002185 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002186 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002187 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002188 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002189 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190 case 'g': list_glob_vars(first); break;
2191 case 'b': list_buf_vars(first); break;
2192 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002193#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002195#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196 case 'v': list_vim_vars(first); break;
2197 case 's': list_script_vars(first); break;
2198 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002199 default:
2200 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002201 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002202 }
2203 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 {
2205 char_u numbuf[NUMBUFLEN];
2206 char_u *tf;
2207 int c;
2208 char_u *s;
2209
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002210 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 c = *arg;
2212 *arg = NUL;
2213 list_one_var_a((char_u *)"",
2214 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 tv.v_type,
2216 s == NULL ? (char_u *)"" : s,
2217 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 *arg = c;
2219 vim_free(tf);
2220 }
2221 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002222 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 }
2224 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225
2226 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228
2229 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 }
2231
2232 return arg;
2233}
2234
2235/*
2236 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2237 * Returns a pointer to the char just after the var name.
2238 * Returns NULL if there is an error.
2239 */
2240 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002241ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002243 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002245 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002246 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247{
2248 int c1;
2249 char_u *name;
2250 char_u *p;
2251 char_u *arg_end = NULL;
2252 int len;
2253 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002254 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255
2256 /*
2257 * ":let $VAR = expr": Set environment variable.
2258 */
2259 if (*arg == '$')
2260 {
2261 /* Find the end of the name. */
2262 ++arg;
2263 name = arg;
2264 len = get_env_len(&arg);
2265 if (len == 0)
2266 EMSG2(_(e_invarg2), name - 1);
2267 else
2268 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002269 if (op != NULL && (*op == '+' || *op == '-'))
2270 EMSG2(_(e_letwrong), op);
2271 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002272 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 EMSG(_(e_letunexp));
2274 else
2275 {
2276 c1 = name[len];
2277 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002278 p = get_tv_string_chk(tv);
2279 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002280 {
2281 int mustfree = FALSE;
2282 char_u *s = vim_getenv(name, &mustfree);
2283
2284 if (s != NULL)
2285 {
2286 p = tofree = concat_str(s, p);
2287 if (mustfree)
2288 vim_free(s);
2289 }
2290 }
2291 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002292 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002293 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002294 if (STRICMP(name, "HOME") == 0)
2295 init_homedir();
2296 else if (didset_vim && STRICMP(name, "VIM") == 0)
2297 didset_vim = FALSE;
2298 else if (didset_vimruntime
2299 && STRICMP(name, "VIMRUNTIME") == 0)
2300 didset_vimruntime = FALSE;
2301 arg_end = arg;
2302 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002304 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 }
2306 }
2307 }
2308
2309 /*
2310 * ":let &option = expr": Set option value.
2311 * ":let &l:option = expr": Set local option value.
2312 * ":let &g:option = expr": Set global option value.
2313 */
2314 else if (*arg == '&')
2315 {
2316 /* Find the end of the name. */
2317 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002318 if (p == NULL || (endchars != NULL
2319 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 EMSG(_(e_letunexp));
2321 else
2322 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002323 long n;
2324 int opt_type;
2325 long numval;
2326 char_u *stringval = NULL;
2327 char_u *s;
2328
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 c1 = *p;
2330 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002331
2332 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002333 s = get_tv_string_chk(tv); /* != NULL if number or string */
2334 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335 {
2336 opt_type = get_option_value(arg, &numval,
2337 &stringval, opt_flags);
2338 if ((opt_type == 1 && *op == '.')
2339 || (opt_type == 0 && *op != '.'))
2340 EMSG2(_(e_letwrong), op);
2341 else
2342 {
2343 if (opt_type == 1) /* number */
2344 {
2345 if (*op == '+')
2346 n = numval + n;
2347 else
2348 n = numval - n;
2349 }
2350 else if (opt_type == 0 && stringval != NULL) /* string */
2351 {
2352 s = concat_str(stringval, s);
2353 vim_free(stringval);
2354 stringval = s;
2355 }
2356 }
2357 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002358 if (s != NULL)
2359 {
2360 set_option_value(arg, n, s, opt_flags);
2361 arg_end = p;
2362 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 }
2366 }
2367
2368 /*
2369 * ":let @r = expr": Set register contents.
2370 */
2371 else if (*arg == '@')
2372 {
2373 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002374 if (op != NULL && (*op == '+' || *op == '-'))
2375 EMSG2(_(e_letwrong), op);
2376 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002377 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 EMSG(_(e_letunexp));
2379 else
2380 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002381 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002382 char_u *s;
2383
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 p = get_tv_string_chk(tv);
2385 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002387 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 if (s != NULL)
2389 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002390 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 vim_free(s);
2392 }
2393 }
2394 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002395 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002397 arg_end = arg + 1;
2398 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002399 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002400 }
2401 }
2402
2403 /*
2404 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002407 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002408 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002409 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002410
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002411 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002414 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2415 EMSG(_(e_letunexp));
2416 else
2417 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002419 arg_end = p;
2420 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 }
2424
2425 else
2426 EMSG2(_(e_invarg2), arg);
2427
2428 return arg_end;
2429}
2430
2431/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002432 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2433 */
2434 static int
2435check_changedtick(arg)
2436 char_u *arg;
2437{
2438 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2439 {
2440 EMSG2(_(e_readonlyvar), arg);
2441 return TRUE;
2442 }
2443 return FALSE;
2444}
2445
2446/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 * Get an lval: variable, Dict item or List item that can be assigned a value
2448 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2449 * "name.key", "name.key[expr]" etc.
2450 * Indexing only works if "name" is an existing List or Dictionary.
2451 * "name" points to the start of the name.
2452 * If "rettv" is not NULL it points to the value to be assigned.
2453 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2454 * wrong; must end in space or cmd separator.
2455 *
2456 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002457 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002459 */
2460 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002461get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002463 typval_T *rettv;
2464 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 int unlet;
2466 int skip;
2467 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002468 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 char_u *expr_start, *expr_end;
2472 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002473 dictitem_T *v;
2474 typval_T var1;
2475 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002477 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002478 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002479 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002480 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002483 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484
2485 if (skip)
2486 {
2487 /* When skipping just find the end of the name. */
2488 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002489 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 }
2491
2492 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002493 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 if (expr_start != NULL)
2495 {
2496 /* Don't expand the name when we already know there is an error. */
2497 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2498 && *p != '[' && *p != '.')
2499 {
2500 EMSG(_(e_trailing));
2501 return NULL;
2502 }
2503
2504 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2505 if (lp->ll_exp_name == NULL)
2506 {
2507 /* Report an invalid expression in braces, unless the
2508 * expression evaluation has been cancelled due to an
2509 * aborting error, an interrupt, or an exception. */
2510 if (!aborting() && !quiet)
2511 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002512 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 EMSG2(_(e_invarg2), name);
2514 return NULL;
2515 }
2516 }
2517 lp->ll_name = lp->ll_exp_name;
2518 }
2519 else
2520 lp->ll_name = name;
2521
2522 /* Without [idx] or .key we are done. */
2523 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2524 return p;
2525
2526 cc = *p;
2527 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002528 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 if (v == NULL && !quiet)
2530 EMSG2(_(e_undefvar), lp->ll_name);
2531 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 if (v == NULL)
2533 return NULL;
2534
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 /*
2536 * Loop until no more [idx] or .key is following.
2537 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002538 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2542 && !(lp->ll_tv->v_type == VAR_DICT
2543 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002544 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 if (!quiet)
2546 EMSG(_("E689: Can only index a List or Dictionary"));
2547 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002548 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 if (!quiet)
2552 EMSG(_("E708: [:] must come last"));
2553 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002555
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 len = -1;
2557 if (*p == '.')
2558 {
2559 key = p + 1;
2560 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2561 ;
2562 if (len == 0)
2563 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 if (!quiet)
2565 EMSG(_(e_emptykey));
2566 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 }
2568 p = key + len;
2569 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002570 else
2571 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002572 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002573 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002574 if (*p == ':')
2575 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002576 else
2577 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002578 empty1 = FALSE;
2579 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002581 if (get_tv_string_chk(&var1) == NULL)
2582 {
2583 /* not a number or string */
2584 clear_tv(&var1);
2585 return NULL;
2586 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002587 }
2588
2589 /* Optionally get the second index [ :expr]. */
2590 if (*p == ':')
2591 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002595 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002596 if (!empty1)
2597 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002599 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (rettv != NULL && (rettv->v_type != VAR_LIST
2601 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (!quiet)
2604 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002605 if (!empty1)
2606 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 }
2609 p = skipwhite(p + 1);
2610 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 else
2613 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2616 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 if (!empty1)
2618 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002621 if (get_tv_string_chk(&var2) == NULL)
2622 {
2623 /* not a number or string */
2624 if (!empty1)
2625 clear_tv(&var1);
2626 clear_tv(&var2);
2627 return NULL;
2628 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (!quiet)
2638 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 if (!empty1)
2640 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 }
2645
2646 /* Skip to past ']'. */
2647 ++p;
2648 }
2649
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 {
2652 if (len == -1)
2653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002655 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 if (*key == NUL)
2657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (!quiet)
2659 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 }
2663 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002664 lp->ll_list = NULL;
2665 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002666 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002669 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002673 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 if (len == -1)
2675 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 }
2678 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 if (len == -1)
2683 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 p = NULL;
2686 break;
2687 }
2688 if (len == -1)
2689 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 }
2692 else
2693 {
2694 /*
2695 * Get the number and item for the only or first index of the List.
2696 */
2697 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 else
2700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002701 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 clear_tv(&var1);
2703 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002704 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 lp->ll_list = lp->ll_tv->vval.v_list;
2706 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2707 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002709 if (lp->ll_n1 < 0)
2710 {
2711 lp->ll_n1 = 0;
2712 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2713 }
2714 }
2715 if (lp->ll_li == NULL)
2716 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 }
2721
2722 /*
2723 * May need to find the item or absolute index for the second
2724 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 * When no index given: "lp->ll_empty2" is TRUE.
2726 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002730 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 }
2739
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2741 if (lp->ll_n1 < 0)
2742 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2743 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002745 }
2746
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002748 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002749 }
2750
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 return p;
2752}
2753
2754/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002755 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 */
2757 static void
2758clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002759 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760{
2761 vim_free(lp->ll_exp_name);
2762 vim_free(lp->ll_newkey);
2763}
2764
2765/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002766 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002768 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 */
2770 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002771set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002772 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002774 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002776 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777{
2778 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002779 listitem_T *ri;
2780 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781
2782 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002783 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002785 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 cc = *endp;
2787 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002788 if (op != NULL && *op != '=')
2789 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002790 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002791
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002792 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002793 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002794 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002795 {
2796 if (tv_op(&tv, rettv, op) == OK)
2797 set_var(lp->ll_name, &tv, FALSE);
2798 clear_tv(&tv);
2799 }
2800 }
2801 else
2802 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002804 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002806 else if (tv_check_lock(lp->ll_newkey == NULL
2807 ? lp->ll_tv->v_lock
2808 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2809 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 else if (lp->ll_range)
2811 {
2812 /*
2813 * Assign the List values to the list items.
2814 */
2815 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002816 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002817 if (op != NULL && *op != '=')
2818 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2819 else
2820 {
2821 clear_tv(&lp->ll_li->li_tv);
2822 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2823 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 ri = ri->li_next;
2825 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2826 break;
2827 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002828 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002830 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002831 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 ri = NULL;
2833 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002834 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002835 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 lp->ll_li = lp->ll_li->li_next;
2837 ++lp->ll_n1;
2838 }
2839 if (ri != NULL)
2840 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002841 else if (lp->ll_empty2
2842 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 : lp->ll_n1 != lp->ll_n2)
2844 EMSG(_("E711: List value has not enough items"));
2845 }
2846 else
2847 {
2848 /*
2849 * Assign to a List or Dictionary item.
2850 */
2851 if (lp->ll_newkey != NULL)
2852 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002853 if (op != NULL && *op != '=')
2854 {
2855 EMSG2(_(e_letwrong), op);
2856 return;
2857 }
2858
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002860 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 if (di == NULL)
2862 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002863 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2864 {
2865 vim_free(di);
2866 return;
2867 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002869 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002870 else if (op != NULL && *op != '=')
2871 {
2872 tv_op(lp->ll_tv, rettv, op);
2873 return;
2874 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002875 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 /*
2879 * Assign the value to the variable or list item.
2880 */
2881 if (copy)
2882 copy_tv(rettv, lp->ll_tv);
2883 else
2884 {
2885 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002886 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002888 }
2889 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002890}
2891
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002892/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002893 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2894 * Returns OK or FAIL.
2895 */
2896 static int
2897tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 typval_T *tv1;
2899 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002900 char_u *op;
2901{
2902 long n;
2903 char_u numbuf[NUMBUFLEN];
2904 char_u *s;
2905
2906 /* Can't do anything with a Funcref or a Dict on the right. */
2907 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2908 {
2909 switch (tv1->v_type)
2910 {
2911 case VAR_DICT:
2912 case VAR_FUNC:
2913 break;
2914
2915 case VAR_LIST:
2916 if (*op != '+' || tv2->v_type != VAR_LIST)
2917 break;
2918 /* List += List */
2919 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2920 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2921 return OK;
2922
2923 case VAR_NUMBER:
2924 case VAR_STRING:
2925 if (tv2->v_type == VAR_LIST)
2926 break;
2927 if (*op == '+' || *op == '-')
2928 {
2929 /* nr += nr or nr -= nr*/
2930 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002931#ifdef FEAT_FLOAT
2932 if (tv2->v_type == VAR_FLOAT)
2933 {
2934 float_T f = n;
2935
2936 if (*op == '+')
2937 f += tv2->vval.v_float;
2938 else
2939 f -= tv2->vval.v_float;
2940 clear_tv(tv1);
2941 tv1->v_type = VAR_FLOAT;
2942 tv1->vval.v_float = f;
2943 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002944 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002945#endif
2946 {
2947 if (*op == '+')
2948 n += get_tv_number(tv2);
2949 else
2950 n -= get_tv_number(tv2);
2951 clear_tv(tv1);
2952 tv1->v_type = VAR_NUMBER;
2953 tv1->vval.v_number = n;
2954 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002955 }
2956 else
2957 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002958 if (tv2->v_type == VAR_FLOAT)
2959 break;
2960
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002961 /* str .= str */
2962 s = get_tv_string(tv1);
2963 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2964 clear_tv(tv1);
2965 tv1->v_type = VAR_STRING;
2966 tv1->vval.v_string = s;
2967 }
2968 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002969
2970#ifdef FEAT_FLOAT
2971 case VAR_FLOAT:
2972 {
2973 float_T f;
2974
2975 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2976 && tv2->v_type != VAR_NUMBER
2977 && tv2->v_type != VAR_STRING))
2978 break;
2979 if (tv2->v_type == VAR_FLOAT)
2980 f = tv2->vval.v_float;
2981 else
2982 f = get_tv_number(tv2);
2983 if (*op == '+')
2984 tv1->vval.v_float += f;
2985 else
2986 tv1->vval.v_float -= f;
2987 }
2988 return OK;
2989#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002990 }
2991 }
2992
2993 EMSG2(_(e_letwrong), op);
2994 return FAIL;
2995}
2996
2997/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002998 * Add a watcher to a list.
2999 */
3000 static void
3001list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003002 list_T *l;
3003 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003004{
3005 lw->lw_next = l->lv_watch;
3006 l->lv_watch = lw;
3007}
3008
3009/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003010 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003011 * No warning when it isn't found...
3012 */
3013 static void
3014list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003015 list_T *l;
3016 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003017{
Bram Moolenaar33570922005-01-25 22:26:29 +00003018 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003019
3020 lwp = &l->lv_watch;
3021 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3022 {
3023 if (lw == lwrem)
3024 {
3025 *lwp = lw->lw_next;
3026 break;
3027 }
3028 lwp = &lw->lw_next;
3029 }
3030}
3031
3032/*
3033 * Just before removing an item from a list: advance watchers to the next
3034 * item.
3035 */
3036 static void
3037list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003038 list_T *l;
3039 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003040{
Bram Moolenaar33570922005-01-25 22:26:29 +00003041 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003042
3043 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3044 if (lw->lw_item == item)
3045 lw->lw_item = item->li_next;
3046}
3047
3048/*
3049 * Evaluate the expression used in a ":for var in expr" command.
3050 * "arg" points to "var".
3051 * Set "*errp" to TRUE for an error, FALSE otherwise;
3052 * Return a pointer that holds the info. Null when there is an error.
3053 */
3054 void *
3055eval_for_line(arg, errp, nextcmdp, skip)
3056 char_u *arg;
3057 int *errp;
3058 char_u **nextcmdp;
3059 int skip;
3060{
Bram Moolenaar33570922005-01-25 22:26:29 +00003061 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003062 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003063 typval_T tv;
3064 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003065
3066 *errp = TRUE; /* default: there is an error */
3067
Bram Moolenaar33570922005-01-25 22:26:29 +00003068 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003069 if (fi == NULL)
3070 return NULL;
3071
3072 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3073 if (expr == NULL)
3074 return fi;
3075
3076 expr = skipwhite(expr);
3077 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3078 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003079 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003080 return fi;
3081 }
3082
3083 if (skip)
3084 ++emsg_skip;
3085 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3086 {
3087 *errp = FALSE;
3088 if (!skip)
3089 {
3090 l = tv.vval.v_list;
3091 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003092 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003093 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003094 clear_tv(&tv);
3095 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003096 else
3097 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003098 /* No need to increment the refcount, it's already set for the
3099 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003100 fi->fi_list = l;
3101 list_add_watch(l, &fi->fi_lw);
3102 fi->fi_lw.lw_item = l->lv_first;
3103 }
3104 }
3105 }
3106 if (skip)
3107 --emsg_skip;
3108
3109 return fi;
3110}
3111
3112/*
3113 * Use the first item in a ":for" list. Advance to the next.
3114 * Assign the values to the variable (list). "arg" points to the first one.
3115 * Return TRUE when a valid item was found, FALSE when at end of list or
3116 * something wrong.
3117 */
3118 int
3119next_for_item(fi_void, arg)
3120 void *fi_void;
3121 char_u *arg;
3122{
Bram Moolenaar33570922005-01-25 22:26:29 +00003123 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003125 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003126
3127 item = fi->fi_lw.lw_item;
3128 if (item == NULL)
3129 result = FALSE;
3130 else
3131 {
3132 fi->fi_lw.lw_item = item->li_next;
3133 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3134 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3135 }
3136 return result;
3137}
3138
3139/*
3140 * Free the structure used to store info used by ":for".
3141 */
3142 void
3143free_for_info(fi_void)
3144 void *fi_void;
3145{
Bram Moolenaar33570922005-01-25 22:26:29 +00003146 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003147
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003148 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003149 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003151 list_unref(fi->fi_list);
3152 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003153 vim_free(fi);
3154}
3155
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3157
3158 void
3159set_context_for_expression(xp, arg, cmdidx)
3160 expand_T *xp;
3161 char_u *arg;
3162 cmdidx_T cmdidx;
3163{
3164 int got_eq = FALSE;
3165 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003166 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168 if (cmdidx == CMD_let)
3169 {
3170 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003171 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172 {
3173 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003174 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175 {
3176 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003177 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178 if (vim_iswhite(*p))
3179 break;
3180 }
3181 return;
3182 }
3183 }
3184 else
3185 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3186 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 while ((xp->xp_pattern = vim_strpbrk(arg,
3188 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3189 {
3190 c = *xp->xp_pattern;
3191 if (c == '&')
3192 {
3193 c = xp->xp_pattern[1];
3194 if (c == '&')
3195 {
3196 ++xp->xp_pattern;
3197 xp->xp_context = cmdidx != CMD_let || got_eq
3198 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3199 }
3200 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003201 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003203 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3204 xp->xp_pattern += 2;
3205
3206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 }
3208 else if (c == '$')
3209 {
3210 /* environment variable */
3211 xp->xp_context = EXPAND_ENV_VARS;
3212 }
3213 else if (c == '=')
3214 {
3215 got_eq = TRUE;
3216 xp->xp_context = EXPAND_EXPRESSION;
3217 }
3218 else if (c == '<'
3219 && xp->xp_context == EXPAND_FUNCTIONS
3220 && vim_strchr(xp->xp_pattern, '(') == NULL)
3221 {
3222 /* Function name can start with "<SNR>" */
3223 break;
3224 }
3225 else if (cmdidx != CMD_let || got_eq)
3226 {
3227 if (c == '"') /* string */
3228 {
3229 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3230 if (c == '\\' && xp->xp_pattern[1] != NUL)
3231 ++xp->xp_pattern;
3232 xp->xp_context = EXPAND_NOTHING;
3233 }
3234 else if (c == '\'') /* literal string */
3235 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003236 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3238 /* skip */ ;
3239 xp->xp_context = EXPAND_NOTHING;
3240 }
3241 else if (c == '|')
3242 {
3243 if (xp->xp_pattern[1] == '|')
3244 {
3245 ++xp->xp_pattern;
3246 xp->xp_context = EXPAND_EXPRESSION;
3247 }
3248 else
3249 xp->xp_context = EXPAND_COMMANDS;
3250 }
3251 else
3252 xp->xp_context = EXPAND_EXPRESSION;
3253 }
3254 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255 /* Doesn't look like something valid, expand as an expression
3256 * anyway. */
3257 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 arg = xp->xp_pattern;
3259 if (*arg != NUL)
3260 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3261 /* skip */ ;
3262 }
3263 xp->xp_pattern = arg;
3264}
3265
3266#endif /* FEAT_CMDL_COMPL */
3267
3268/*
3269 * ":1,25call func(arg1, arg2)" function call.
3270 */
3271 void
3272ex_call(eap)
3273 exarg_T *eap;
3274{
3275 char_u *arg = eap->arg;
3276 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003278 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003280 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 linenr_T lnum;
3282 int doesrange;
3283 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003284 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003286 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003287 if (fudi.fd_newkey != NULL)
3288 {
3289 /* Still need to give an error message for missing key. */
3290 EMSG2(_(e_dictkey), fudi.fd_newkey);
3291 vim_free(fudi.fd_newkey);
3292 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003293 if (tofree == NULL)
3294 return;
3295
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003296 /* Increase refcount on dictionary, it could get deleted when evaluating
3297 * the arguments. */
3298 if (fudi.fd_dict != NULL)
3299 ++fudi.fd_dict->dv_refcount;
3300
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003301 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003302 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003303 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
Bram Moolenaar532c7802005-01-27 14:44:31 +00003305 /* Skip white space to allow ":call func ()". Not good, but required for
3306 * backward compatibility. */
3307 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003308 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309
3310 if (*startarg != '(')
3311 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003312 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 goto end;
3314 }
3315
3316 /*
3317 * When skipping, evaluate the function once, to find the end of the
3318 * arguments.
3319 * When the function takes a range, this is discovered after the first
3320 * call, and the loop is broken.
3321 */
3322 if (eap->skip)
3323 {
3324 ++emsg_skip;
3325 lnum = eap->line2; /* do it once, also with an invalid range */
3326 }
3327 else
3328 lnum = eap->line1;
3329 for ( ; lnum <= eap->line2; ++lnum)
3330 {
3331 if (!eap->skip && eap->addr_count > 0)
3332 {
3333 curwin->w_cursor.lnum = lnum;
3334 curwin->w_cursor.col = 0;
3335 }
3336 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003337 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003338 eap->line1, eap->line2, &doesrange,
3339 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 {
3341 failed = TRUE;
3342 break;
3343 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003344
3345 /* Handle a function returning a Funcref, Dictionary or List. */
3346 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3347 {
3348 failed = TRUE;
3349 break;
3350 }
3351
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003352 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 if (doesrange || eap->skip)
3354 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003355
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003357 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003358 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003359 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 if (aborting())
3361 break;
3362 }
3363 if (eap->skip)
3364 --emsg_skip;
3365
3366 if (!failed)
3367 {
3368 /* Check for trailing illegal characters and a following command. */
3369 if (!ends_excmd(*arg))
3370 {
3371 emsg_severe = TRUE;
3372 EMSG(_(e_trailing));
3373 }
3374 else
3375 eap->nextcmd = check_nextcmd(arg);
3376 }
3377
3378end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003379 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003380 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381}
3382
3383/*
3384 * ":unlet[!] var1 ... " command.
3385 */
3386 void
3387ex_unlet(eap)
3388 exarg_T *eap;
3389{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003390 ex_unletlock(eap, eap->arg, 0);
3391}
3392
3393/*
3394 * ":lockvar" and ":unlockvar" commands
3395 */
3396 void
3397ex_lockvar(eap)
3398 exarg_T *eap;
3399{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003401 int deep = 2;
3402
3403 if (eap->forceit)
3404 deep = -1;
3405 else if (vim_isdigit(*arg))
3406 {
3407 deep = getdigits(&arg);
3408 arg = skipwhite(arg);
3409 }
3410
3411 ex_unletlock(eap, arg, deep);
3412}
3413
3414/*
3415 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3416 */
3417 static void
3418ex_unletlock(eap, argstart, deep)
3419 exarg_T *eap;
3420 char_u *argstart;
3421 int deep;
3422{
3423 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003426 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
3428 do
3429 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003430 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003431 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3432 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003433 if (lv.ll_name == NULL)
3434 error = TRUE; /* error but continue parsing */
3435 if (name_end == NULL || (!vim_iswhite(*name_end)
3436 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003438 if (name_end != NULL)
3439 {
3440 emsg_severe = TRUE;
3441 EMSG(_(e_trailing));
3442 }
3443 if (!(eap->skip || error))
3444 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 break;
3446 }
3447
3448 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003449 {
3450 if (eap->cmdidx == CMD_unlet)
3451 {
3452 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3453 error = TRUE;
3454 }
3455 else
3456 {
3457 if (do_lock_var(&lv, name_end, deep,
3458 eap->cmdidx == CMD_lockvar) == FAIL)
3459 error = TRUE;
3460 }
3461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003463 if (!eap->skip)
3464 clear_lval(&lv);
3465
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 arg = skipwhite(name_end);
3467 } while (!ends_excmd(*arg));
3468
3469 eap->nextcmd = check_nextcmd(arg);
3470}
3471
Bram Moolenaar8c711452005-01-14 21:53:12 +00003472 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003473do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003474 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003475 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003476 int forceit;
3477{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003478 int ret = OK;
3479 int cc;
3480
3481 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003482 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003483 cc = *name_end;
3484 *name_end = NUL;
3485
3486 /* Normal name or expanded name. */
3487 if (check_changedtick(lp->ll_name))
3488 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003489 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003490 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003491 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003492 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003493 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3494 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003495 else if (lp->ll_range)
3496 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003497 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003498
3499 /* Delete a range of List items. */
3500 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3501 {
3502 li = lp->ll_li->li_next;
3503 listitem_remove(lp->ll_list, lp->ll_li);
3504 lp->ll_li = li;
3505 ++lp->ll_n1;
3506 }
3507 }
3508 else
3509 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003510 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003511 /* unlet a List item. */
3512 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003513 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003514 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003515 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003516 }
3517
3518 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003519}
3520
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521/*
3522 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003523 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 */
3525 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003526do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003528 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529{
Bram Moolenaar33570922005-01-25 22:26:29 +00003530 hashtab_T *ht;
3531 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003532 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003533 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534
Bram Moolenaar33570922005-01-25 22:26:29 +00003535 ht = find_var_ht(name, &varname);
3536 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003538 hi = hash_find(ht, varname);
3539 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003540 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003541 di = HI2DI(hi);
3542 if (var_check_fixed(di->di_flags, name)
3543 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003544 return FAIL;
3545 delete_var(ht, hi);
3546 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003549 if (forceit)
3550 return OK;
3551 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 return FAIL;
3553}
3554
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003555/*
3556 * Lock or unlock variable indicated by "lp".
3557 * "deep" is the levels to go (-1 for unlimited);
3558 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3559 */
3560 static int
3561do_lock_var(lp, name_end, deep, lock)
3562 lval_T *lp;
3563 char_u *name_end;
3564 int deep;
3565 int lock;
3566{
3567 int ret = OK;
3568 int cc;
3569 dictitem_T *di;
3570
3571 if (deep == 0) /* nothing to do */
3572 return OK;
3573
3574 if (lp->ll_tv == NULL)
3575 {
3576 cc = *name_end;
3577 *name_end = NUL;
3578
3579 /* Normal name or expanded name. */
3580 if (check_changedtick(lp->ll_name))
3581 ret = FAIL;
3582 else
3583 {
3584 di = find_var(lp->ll_name, NULL);
3585 if (di == NULL)
3586 ret = FAIL;
3587 else
3588 {
3589 if (lock)
3590 di->di_flags |= DI_FLAGS_LOCK;
3591 else
3592 di->di_flags &= ~DI_FLAGS_LOCK;
3593 item_lock(&di->di_tv, deep, lock);
3594 }
3595 }
3596 *name_end = cc;
3597 }
3598 else if (lp->ll_range)
3599 {
3600 listitem_T *li = lp->ll_li;
3601
3602 /* (un)lock a range of List items. */
3603 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3604 {
3605 item_lock(&li->li_tv, deep, lock);
3606 li = li->li_next;
3607 ++lp->ll_n1;
3608 }
3609 }
3610 else if (lp->ll_list != NULL)
3611 /* (un)lock a List item. */
3612 item_lock(&lp->ll_li->li_tv, deep, lock);
3613 else
3614 /* un(lock) a Dictionary item. */
3615 item_lock(&lp->ll_di->di_tv, deep, lock);
3616
3617 return ret;
3618}
3619
3620/*
3621 * Lock or unlock an item. "deep" is nr of levels to go.
3622 */
3623 static void
3624item_lock(tv, deep, lock)
3625 typval_T *tv;
3626 int deep;
3627 int lock;
3628{
3629 static int recurse = 0;
3630 list_T *l;
3631 listitem_T *li;
3632 dict_T *d;
3633 hashitem_T *hi;
3634 int todo;
3635
3636 if (recurse >= DICT_MAXNEST)
3637 {
3638 EMSG(_("E743: variable nested too deep for (un)lock"));
3639 return;
3640 }
3641 if (deep == 0)
3642 return;
3643 ++recurse;
3644
3645 /* lock/unlock the item itself */
3646 if (lock)
3647 tv->v_lock |= VAR_LOCKED;
3648 else
3649 tv->v_lock &= ~VAR_LOCKED;
3650
3651 switch (tv->v_type)
3652 {
3653 case VAR_LIST:
3654 if ((l = tv->vval.v_list) != NULL)
3655 {
3656 if (lock)
3657 l->lv_lock |= VAR_LOCKED;
3658 else
3659 l->lv_lock &= ~VAR_LOCKED;
3660 if (deep < 0 || deep > 1)
3661 /* recursive: lock/unlock the items the List contains */
3662 for (li = l->lv_first; li != NULL; li = li->li_next)
3663 item_lock(&li->li_tv, deep - 1, lock);
3664 }
3665 break;
3666 case VAR_DICT:
3667 if ((d = tv->vval.v_dict) != NULL)
3668 {
3669 if (lock)
3670 d->dv_lock |= VAR_LOCKED;
3671 else
3672 d->dv_lock &= ~VAR_LOCKED;
3673 if (deep < 0 || deep > 1)
3674 {
3675 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003676 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003677 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3678 {
3679 if (!HASHITEM_EMPTY(hi))
3680 {
3681 --todo;
3682 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3683 }
3684 }
3685 }
3686 }
3687 }
3688 --recurse;
3689}
3690
Bram Moolenaara40058a2005-07-11 22:42:07 +00003691/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003692 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3693 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003694 */
3695 static int
3696tv_islocked(tv)
3697 typval_T *tv;
3698{
3699 return (tv->v_lock & VAR_LOCKED)
3700 || (tv->v_type == VAR_LIST
3701 && tv->vval.v_list != NULL
3702 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3703 || (tv->v_type == VAR_DICT
3704 && tv->vval.v_dict != NULL
3705 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3706}
3707
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3709/*
3710 * Delete all "menutrans_" variables.
3711 */
3712 void
3713del_menutrans_vars()
3714{
Bram Moolenaar33570922005-01-25 22:26:29 +00003715 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003716 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
Bram Moolenaar33570922005-01-25 22:26:29 +00003718 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003719 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003720 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003721 {
3722 if (!HASHITEM_EMPTY(hi))
3723 {
3724 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003725 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3726 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003727 }
3728 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003729 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730}
3731#endif
3732
3733#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3734
3735/*
3736 * Local string buffer for the next two functions to store a variable name
3737 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3738 * get_user_var_name().
3739 */
3740
3741static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3742
3743static char_u *varnamebuf = NULL;
3744static int varnamebuflen = 0;
3745
3746/*
3747 * Function to concatenate a prefix and a variable name.
3748 */
3749 static char_u *
3750cat_prefix_varname(prefix, name)
3751 int prefix;
3752 char_u *name;
3753{
3754 int len;
3755
3756 len = (int)STRLEN(name) + 3;
3757 if (len > varnamebuflen)
3758 {
3759 vim_free(varnamebuf);
3760 len += 10; /* some additional space */
3761 varnamebuf = alloc(len);
3762 if (varnamebuf == NULL)
3763 {
3764 varnamebuflen = 0;
3765 return NULL;
3766 }
3767 varnamebuflen = len;
3768 }
3769 *varnamebuf = prefix;
3770 varnamebuf[1] = ':';
3771 STRCPY(varnamebuf + 2, name);
3772 return varnamebuf;
3773}
3774
3775/*
3776 * Function given to ExpandGeneric() to obtain the list of user defined
3777 * (global/buffer/window/built-in) variable names.
3778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 char_u *
3780get_user_var_name(xp, idx)
3781 expand_T *xp;
3782 int idx;
3783{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003784 static long_u gdone;
3785 static long_u bdone;
3786 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003787#ifdef FEAT_WINDOWS
3788 static long_u tdone;
3789#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003790 static int vidx;
3791 static hashitem_T *hi;
3792 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793
3794 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003795 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003796 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003797#ifdef FEAT_WINDOWS
3798 tdone = 0;
3799#endif
3800 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003801
3802 /* Global variables */
3803 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003805 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003806 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003807 else
3808 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003809 while (HASHITEM_EMPTY(hi))
3810 ++hi;
3811 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3812 return cat_prefix_varname('g', hi->hi_key);
3813 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003815
3816 /* b: variables */
3817 ht = &curbuf->b_vars.dv_hashtab;
3818 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003820 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003821 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003822 else
3823 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003824 while (HASHITEM_EMPTY(hi))
3825 ++hi;
3826 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003828 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003830 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 return (char_u *)"b:changedtick";
3832 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003833
3834 /* w: variables */
3835 ht = &curwin->w_vars.dv_hashtab;
3836 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003838 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003839 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003840 else
3841 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003842 while (HASHITEM_EMPTY(hi))
3843 ++hi;
3844 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003846
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003847#ifdef FEAT_WINDOWS
3848 /* t: variables */
3849 ht = &curtab->tp_vars.dv_hashtab;
3850 if (tdone < ht->ht_used)
3851 {
3852 if (tdone++ == 0)
3853 hi = ht->ht_array;
3854 else
3855 ++hi;
3856 while (HASHITEM_EMPTY(hi))
3857 ++hi;
3858 return cat_prefix_varname('t', hi->hi_key);
3859 }
3860#endif
3861
Bram Moolenaar33570922005-01-25 22:26:29 +00003862 /* v: variables */
3863 if (vidx < VV_LEN)
3864 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865
3866 vim_free(varnamebuf);
3867 varnamebuf = NULL;
3868 varnamebuflen = 0;
3869 return NULL;
3870}
3871
3872#endif /* FEAT_CMDL_COMPL */
3873
3874/*
3875 * types for expressions.
3876 */
3877typedef enum
3878{
3879 TYPE_UNKNOWN = 0
3880 , TYPE_EQUAL /* == */
3881 , TYPE_NEQUAL /* != */
3882 , TYPE_GREATER /* > */
3883 , TYPE_GEQUAL /* >= */
3884 , TYPE_SMALLER /* < */
3885 , TYPE_SEQUAL /* <= */
3886 , TYPE_MATCH /* =~ */
3887 , TYPE_NOMATCH /* !~ */
3888} exptype_T;
3889
3890/*
3891 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003892 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3894 */
3895
3896/*
3897 * Handle zero level expression.
3898 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003899 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003900 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 * Return OK or FAIL.
3902 */
3903 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003904eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003906 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 char_u **nextcmd;
3908 int evaluate;
3909{
3910 int ret;
3911 char_u *p;
3912
3913 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003914 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 if (ret == FAIL || !ends_excmd(*p))
3916 {
3917 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003918 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 /*
3920 * Report the invalid expression unless the expression evaluation has
3921 * been cancelled due to an aborting error, an interrupt, or an
3922 * exception.
3923 */
3924 if (!aborting())
3925 EMSG2(_(e_invexpr2), arg);
3926 ret = FAIL;
3927 }
3928 if (nextcmd != NULL)
3929 *nextcmd = check_nextcmd(p);
3930
3931 return ret;
3932}
3933
3934/*
3935 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003936 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 *
3938 * "arg" must point to the first non-white of the expression.
3939 * "arg" is advanced to the next non-white after the recognized expression.
3940 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003941 * Note: "rettv.v_lock" is not set.
3942 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 * Return OK or FAIL.
3944 */
3945 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003946eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 int evaluate;
3950{
3951 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953
3954 /*
3955 * Get the first variable.
3956 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003957 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 return FAIL;
3959
3960 if ((*arg)[0] == '?')
3961 {
3962 result = FALSE;
3963 if (evaluate)
3964 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003965 int error = FALSE;
3966
3967 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003969 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003970 if (error)
3971 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
3973
3974 /*
3975 * Get the second variable.
3976 */
3977 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003978 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 return FAIL;
3980
3981 /*
3982 * Check for the ":".
3983 */
3984 if ((*arg)[0] != ':')
3985 {
3986 EMSG(_("E109: Missing ':' after '?'"));
3987 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003988 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 return FAIL;
3990 }
3991
3992 /*
3993 * Get the third variable.
3994 */
3995 *arg = skipwhite(*arg + 1);
3996 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3997 {
3998 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003999 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 return FAIL;
4001 }
4002 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004003 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
4005
4006 return OK;
4007}
4008
4009/*
4010 * Handle first level expression:
4011 * expr2 || expr2 || expr2 logical OR
4012 *
4013 * "arg" must point to the first non-white of the expression.
4014 * "arg" is advanced to the next non-white after the recognized expression.
4015 *
4016 * Return OK or FAIL.
4017 */
4018 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 int evaluate;
4023{
Bram Moolenaar33570922005-01-25 22:26:29 +00004024 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 long result;
4026 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004027 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
4029 /*
4030 * Get the first variable.
4031 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004032 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 return FAIL;
4034
4035 /*
4036 * Repeat until there is no following "||".
4037 */
4038 first = TRUE;
4039 result = FALSE;
4040 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4041 {
4042 if (evaluate && first)
4043 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004044 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004046 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004047 if (error)
4048 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 first = FALSE;
4050 }
4051
4052 /*
4053 * Get the second variable.
4054 */
4055 *arg = skipwhite(*arg + 2);
4056 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4057 return FAIL;
4058
4059 /*
4060 * Compute the result.
4061 */
4062 if (evaluate && !result)
4063 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004064 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004066 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004067 if (error)
4068 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
4070 if (evaluate)
4071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004072 rettv->v_type = VAR_NUMBER;
4073 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
4075 }
4076
4077 return OK;
4078}
4079
4080/*
4081 * Handle second level expression:
4082 * expr3 && expr3 && expr3 logical AND
4083 *
4084 * "arg" must point to the first non-white of the expression.
4085 * "arg" is advanced to the next non-white after the recognized expression.
4086 *
4087 * Return OK or FAIL.
4088 */
4089 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 int evaluate;
4094{
Bram Moolenaar33570922005-01-25 22:26:29 +00004095 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 long result;
4097 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004098 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099
4100 /*
4101 * Get the first variable.
4102 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004103 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 return FAIL;
4105
4106 /*
4107 * Repeat until there is no following "&&".
4108 */
4109 first = TRUE;
4110 result = TRUE;
4111 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4112 {
4113 if (evaluate && first)
4114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004115 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004117 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004118 if (error)
4119 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 first = FALSE;
4121 }
4122
4123 /*
4124 * Get the second variable.
4125 */
4126 *arg = skipwhite(*arg + 2);
4127 if (eval4(arg, &var2, evaluate && result) == FAIL)
4128 return FAIL;
4129
4130 /*
4131 * Compute the result.
4132 */
4133 if (evaluate && result)
4134 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004135 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004138 if (error)
4139 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
4141 if (evaluate)
4142 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 rettv->v_type = VAR_NUMBER;
4144 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 }
4146 }
4147
4148 return OK;
4149}
4150
4151/*
4152 * Handle third level expression:
4153 * var1 == var2
4154 * var1 =~ var2
4155 * var1 != var2
4156 * var1 !~ var2
4157 * var1 > var2
4158 * var1 >= var2
4159 * var1 < var2
4160 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004161 * var1 is var2
4162 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 *
4164 * "arg" must point to the first non-white of the expression.
4165 * "arg" is advanced to the next non-white after the recognized expression.
4166 *
4167 * Return OK or FAIL.
4168 */
4169 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004170eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 int evaluate;
4174{
Bram Moolenaar33570922005-01-25 22:26:29 +00004175 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 char_u *p;
4177 int i;
4178 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004179 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 int len = 2;
4181 long n1, n2;
4182 char_u *s1, *s2;
4183 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4184 regmatch_T regmatch;
4185 int ic;
4186 char_u *save_cpo;
4187
4188 /*
4189 * Get the first variable.
4190 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 return FAIL;
4193
4194 p = *arg;
4195 switch (p[0])
4196 {
4197 case '=': if (p[1] == '=')
4198 type = TYPE_EQUAL;
4199 else if (p[1] == '~')
4200 type = TYPE_MATCH;
4201 break;
4202 case '!': if (p[1] == '=')
4203 type = TYPE_NEQUAL;
4204 else if (p[1] == '~')
4205 type = TYPE_NOMATCH;
4206 break;
4207 case '>': if (p[1] != '=')
4208 {
4209 type = TYPE_GREATER;
4210 len = 1;
4211 }
4212 else
4213 type = TYPE_GEQUAL;
4214 break;
4215 case '<': if (p[1] != '=')
4216 {
4217 type = TYPE_SMALLER;
4218 len = 1;
4219 }
4220 else
4221 type = TYPE_SEQUAL;
4222 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004223 case 'i': if (p[1] == 's')
4224 {
4225 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4226 len = 5;
4227 if (!vim_isIDc(p[len]))
4228 {
4229 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4230 type_is = TRUE;
4231 }
4232 }
4233 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
4235
4236 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004237 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 */
4239 if (type != TYPE_UNKNOWN)
4240 {
4241 /* extra question mark appended: ignore case */
4242 if (p[len] == '?')
4243 {
4244 ic = TRUE;
4245 ++len;
4246 }
4247 /* extra '#' appended: match case */
4248 else if (p[len] == '#')
4249 {
4250 ic = FALSE;
4251 ++len;
4252 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004253 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 else
4255 ic = p_ic;
4256
4257 /*
4258 * Get the second variable.
4259 */
4260 *arg = skipwhite(p + len);
4261 if (eval5(arg, &var2, evaluate) == FAIL)
4262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 return FAIL;
4265 }
4266
4267 if (evaluate)
4268 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004269 if (type_is && rettv->v_type != var2.v_type)
4270 {
4271 /* For "is" a different type always means FALSE, for "notis"
4272 * it means TRUE. */
4273 n1 = (type == TYPE_NEQUAL);
4274 }
4275 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4276 {
4277 if (type_is)
4278 {
4279 n1 = (rettv->v_type == var2.v_type
4280 && rettv->vval.v_list == var2.vval.v_list);
4281 if (type == TYPE_NEQUAL)
4282 n1 = !n1;
4283 }
4284 else if (rettv->v_type != var2.v_type
4285 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4286 {
4287 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004288 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004289 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004290 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004291 clear_tv(rettv);
4292 clear_tv(&var2);
4293 return FAIL;
4294 }
4295 else
4296 {
4297 /* Compare two Lists for being equal or unequal. */
4298 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4299 if (type == TYPE_NEQUAL)
4300 n1 = !n1;
4301 }
4302 }
4303
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004304 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4305 {
4306 if (type_is)
4307 {
4308 n1 = (rettv->v_type == var2.v_type
4309 && rettv->vval.v_dict == var2.vval.v_dict);
4310 if (type == TYPE_NEQUAL)
4311 n1 = !n1;
4312 }
4313 else if (rettv->v_type != var2.v_type
4314 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4315 {
4316 if (rettv->v_type != var2.v_type)
4317 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4318 else
4319 EMSG(_("E736: Invalid operation for Dictionary"));
4320 clear_tv(rettv);
4321 clear_tv(&var2);
4322 return FAIL;
4323 }
4324 else
4325 {
4326 /* Compare two Dictionaries for being equal or unequal. */
4327 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4328 if (type == TYPE_NEQUAL)
4329 n1 = !n1;
4330 }
4331 }
4332
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004333 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4334 {
4335 if (rettv->v_type != var2.v_type
4336 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4337 {
4338 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004339 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004340 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004341 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004342 clear_tv(rettv);
4343 clear_tv(&var2);
4344 return FAIL;
4345 }
4346 else
4347 {
4348 /* Compare two Funcrefs for being equal or unequal. */
4349 if (rettv->vval.v_string == NULL
4350 || var2.vval.v_string == NULL)
4351 n1 = FALSE;
4352 else
4353 n1 = STRCMP(rettv->vval.v_string,
4354 var2.vval.v_string) == 0;
4355 if (type == TYPE_NEQUAL)
4356 n1 = !n1;
4357 }
4358 }
4359
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004360#ifdef FEAT_FLOAT
4361 /*
4362 * If one of the two variables is a float, compare as a float.
4363 * When using "=~" or "!~", always compare as string.
4364 */
4365 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4366 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4367 {
4368 float_T f1, f2;
4369
4370 if (rettv->v_type == VAR_FLOAT)
4371 f1 = rettv->vval.v_float;
4372 else
4373 f1 = get_tv_number(rettv);
4374 if (var2.v_type == VAR_FLOAT)
4375 f2 = var2.vval.v_float;
4376 else
4377 f2 = get_tv_number(&var2);
4378 n1 = FALSE;
4379 switch (type)
4380 {
4381 case TYPE_EQUAL: n1 = (f1 == f2); break;
4382 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4383 case TYPE_GREATER: n1 = (f1 > f2); break;
4384 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4385 case TYPE_SMALLER: n1 = (f1 < f2); break;
4386 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4387 case TYPE_UNKNOWN:
4388 case TYPE_MATCH:
4389 case TYPE_NOMATCH: break; /* avoid gcc warning */
4390 }
4391 }
4392#endif
4393
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 /*
4395 * If one of the two variables is a number, compare as a number.
4396 * When using "=~" or "!~", always compare as string.
4397 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004398 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4400 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004401 n1 = get_tv_number(rettv);
4402 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 switch (type)
4404 {
4405 case TYPE_EQUAL: n1 = (n1 == n2); break;
4406 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4407 case TYPE_GREATER: n1 = (n1 > n2); break;
4408 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4409 case TYPE_SMALLER: n1 = (n1 < n2); break;
4410 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4411 case TYPE_UNKNOWN:
4412 case TYPE_MATCH:
4413 case TYPE_NOMATCH: break; /* avoid gcc warning */
4414 }
4415 }
4416 else
4417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004418 s1 = get_tv_string_buf(rettv, buf1);
4419 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4421 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4422 else
4423 i = 0;
4424 n1 = FALSE;
4425 switch (type)
4426 {
4427 case TYPE_EQUAL: n1 = (i == 0); break;
4428 case TYPE_NEQUAL: n1 = (i != 0); break;
4429 case TYPE_GREATER: n1 = (i > 0); break;
4430 case TYPE_GEQUAL: n1 = (i >= 0); break;
4431 case TYPE_SMALLER: n1 = (i < 0); break;
4432 case TYPE_SEQUAL: n1 = (i <= 0); break;
4433
4434 case TYPE_MATCH:
4435 case TYPE_NOMATCH:
4436 /* avoid 'l' flag in 'cpoptions' */
4437 save_cpo = p_cpo;
4438 p_cpo = (char_u *)"";
4439 regmatch.regprog = vim_regcomp(s2,
4440 RE_MAGIC + RE_STRING);
4441 regmatch.rm_ic = ic;
4442 if (regmatch.regprog != NULL)
4443 {
4444 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4445 vim_free(regmatch.regprog);
4446 if (type == TYPE_NOMATCH)
4447 n1 = !n1;
4448 }
4449 p_cpo = save_cpo;
4450 break;
4451
4452 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4453 }
4454 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004455 clear_tv(rettv);
4456 clear_tv(&var2);
4457 rettv->v_type = VAR_NUMBER;
4458 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
4460 }
4461
4462 return OK;
4463}
4464
4465/*
4466 * Handle fourth level expression:
4467 * + number addition
4468 * - number subtraction
4469 * . string concatenation
4470 *
4471 * "arg" must point to the first non-white of the expression.
4472 * "arg" is advanced to the next non-white after the recognized expression.
4473 *
4474 * Return OK or FAIL.
4475 */
4476 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004477eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 int evaluate;
4481{
Bram Moolenaar33570922005-01-25 22:26:29 +00004482 typval_T var2;
4483 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 int op;
4485 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004486#ifdef FEAT_FLOAT
4487 float_T f1 = 0, f2 = 0;
4488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 char_u *s1, *s2;
4490 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4491 char_u *p;
4492
4493 /*
4494 * Get the first variable.
4495 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004496 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 return FAIL;
4498
4499 /*
4500 * Repeat computing, until no '+', '-' or '.' is following.
4501 */
4502 for (;;)
4503 {
4504 op = **arg;
4505 if (op != '+' && op != '-' && op != '.')
4506 break;
4507
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004508 if ((op != '+' || rettv->v_type != VAR_LIST)
4509#ifdef FEAT_FLOAT
4510 && (op == '.' || rettv->v_type != VAR_FLOAT)
4511#endif
4512 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004513 {
4514 /* For "list + ...", an illegal use of the first operand as
4515 * a number cannot be determined before evaluating the 2nd
4516 * operand: if this is also a list, all is ok.
4517 * For "something . ...", "something - ..." or "non-list + ...",
4518 * we know that the first operand needs to be a string or number
4519 * without evaluating the 2nd operand. So check before to avoid
4520 * side effects after an error. */
4521 if (evaluate && get_tv_string_chk(rettv) == NULL)
4522 {
4523 clear_tv(rettv);
4524 return FAIL;
4525 }
4526 }
4527
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 /*
4529 * Get the second variable.
4530 */
4531 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004532 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004534 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 return FAIL;
4536 }
4537
4538 if (evaluate)
4539 {
4540 /*
4541 * Compute the result.
4542 */
4543 if (op == '.')
4544 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004545 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4546 s2 = get_tv_string_buf_chk(&var2, buf2);
4547 if (s2 == NULL) /* type error ? */
4548 {
4549 clear_tv(rettv);
4550 clear_tv(&var2);
4551 return FAIL;
4552 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004553 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 clear_tv(rettv);
4555 rettv->v_type = VAR_STRING;
4556 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004558 else if (op == '+' && rettv->v_type == VAR_LIST
4559 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004560 {
4561 /* concatenate Lists */
4562 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4563 &var3) == FAIL)
4564 {
4565 clear_tv(rettv);
4566 clear_tv(&var2);
4567 return FAIL;
4568 }
4569 clear_tv(rettv);
4570 *rettv = var3;
4571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 else
4573 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004574 int error = FALSE;
4575
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004576#ifdef FEAT_FLOAT
4577 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004578 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004579 f1 = rettv->vval.v_float;
4580 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004581 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004582 else
4583#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004584 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004585 n1 = get_tv_number_chk(rettv, &error);
4586 if (error)
4587 {
4588 /* This can only happen for "list + non-list". For
4589 * "non-list + ..." or "something - ...", we returned
4590 * before evaluating the 2nd operand. */
4591 clear_tv(rettv);
4592 return FAIL;
4593 }
4594#ifdef FEAT_FLOAT
4595 if (var2.v_type == VAR_FLOAT)
4596 f1 = n1;
4597#endif
4598 }
4599#ifdef FEAT_FLOAT
4600 if (var2.v_type == VAR_FLOAT)
4601 {
4602 f2 = var2.vval.v_float;
4603 n2 = 0;
4604 }
4605 else
4606#endif
4607 {
4608 n2 = get_tv_number_chk(&var2, &error);
4609 if (error)
4610 {
4611 clear_tv(rettv);
4612 clear_tv(&var2);
4613 return FAIL;
4614 }
4615#ifdef FEAT_FLOAT
4616 if (rettv->v_type == VAR_FLOAT)
4617 f2 = n2;
4618#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004619 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004620 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004621
4622#ifdef FEAT_FLOAT
4623 /* If there is a float on either side the result is a float. */
4624 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4625 {
4626 if (op == '+')
4627 f1 = f1 + f2;
4628 else
4629 f1 = f1 - f2;
4630 rettv->v_type = VAR_FLOAT;
4631 rettv->vval.v_float = f1;
4632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004634#endif
4635 {
4636 if (op == '+')
4637 n1 = n1 + n2;
4638 else
4639 n1 = n1 - n2;
4640 rettv->v_type = VAR_NUMBER;
4641 rettv->vval.v_number = n1;
4642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004644 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 }
4646 }
4647 return OK;
4648}
4649
4650/*
4651 * Handle fifth level expression:
4652 * * number multiplication
4653 * / number division
4654 * % number modulo
4655 *
4656 * "arg" must point to the first non-white of the expression.
4657 * "arg" is advanced to the next non-white after the recognized expression.
4658 *
4659 * Return OK or FAIL.
4660 */
4661 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004662eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004666 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667{
Bram Moolenaar33570922005-01-25 22:26:29 +00004668 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 int op;
4670 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004671#ifdef FEAT_FLOAT
4672 int use_float = FALSE;
4673 float_T f1 = 0, f2;
4674#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004675 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676
4677 /*
4678 * Get the first variable.
4679 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004680 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 return FAIL;
4682
4683 /*
4684 * Repeat computing, until no '*', '/' or '%' is following.
4685 */
4686 for (;;)
4687 {
4688 op = **arg;
4689 if (op != '*' && op != '/' && op != '%')
4690 break;
4691
4692 if (evaluate)
4693 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004694#ifdef FEAT_FLOAT
4695 if (rettv->v_type == VAR_FLOAT)
4696 {
4697 f1 = rettv->vval.v_float;
4698 use_float = TRUE;
4699 n1 = 0;
4700 }
4701 else
4702#endif
4703 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004704 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004705 if (error)
4706 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 }
4708 else
4709 n1 = 0;
4710
4711 /*
4712 * Get the second variable.
4713 */
4714 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004715 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 return FAIL;
4717
4718 if (evaluate)
4719 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004720#ifdef FEAT_FLOAT
4721 if (var2.v_type == VAR_FLOAT)
4722 {
4723 if (!use_float)
4724 {
4725 f1 = n1;
4726 use_float = TRUE;
4727 }
4728 f2 = var2.vval.v_float;
4729 n2 = 0;
4730 }
4731 else
4732#endif
4733 {
4734 n2 = get_tv_number_chk(&var2, &error);
4735 clear_tv(&var2);
4736 if (error)
4737 return FAIL;
4738#ifdef FEAT_FLOAT
4739 if (use_float)
4740 f2 = n2;
4741#endif
4742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743
4744 /*
4745 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004746 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004748#ifdef FEAT_FLOAT
4749 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004751 if (op == '*')
4752 f1 = f1 * f2;
4753 else if (op == '/')
4754 {
4755 /* We rely on the floating point library to handle divide
4756 * by zero to result in "inf" and not a crash. */
4757 f1 = f1 / f2;
4758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004760 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004761 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004762 return FAIL;
4763 }
4764 rettv->v_type = VAR_FLOAT;
4765 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
4767 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004770 if (op == '*')
4771 n1 = n1 * n2;
4772 else if (op == '/')
4773 {
4774 if (n2 == 0) /* give an error message? */
4775 {
4776 if (n1 == 0)
4777 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4778 else if (n1 < 0)
4779 n1 = -0x7fffffffL;
4780 else
4781 n1 = 0x7fffffffL;
4782 }
4783 else
4784 n1 = n1 / n2;
4785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004787 {
4788 if (n2 == 0) /* give an error message? */
4789 n1 = 0;
4790 else
4791 n1 = n1 % n2;
4792 }
4793 rettv->v_type = VAR_NUMBER;
4794 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
4797 }
4798
4799 return OK;
4800}
4801
4802/*
4803 * Handle sixth level expression:
4804 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004805 * "string" string constant
4806 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 * &option-name option value
4808 * @r register contents
4809 * identifier variable value
4810 * function() function call
4811 * $VAR environment variable
4812 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004813 * [expr, expr] List
4814 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 *
4816 * Also handle:
4817 * ! in front logical NOT
4818 * - in front unary minus
4819 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004820 * trailing [] subscript in String or List
4821 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 *
4823 * "arg" must point to the first non-white of the expression.
4824 * "arg" is advanced to the next non-white after the recognized expression.
4825 *
4826 * Return OK or FAIL.
4827 */
4828 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004829eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004833 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 long n;
4836 int len;
4837 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 char_u *start_leader, *end_leader;
4839 int ret = OK;
4840 char_u *alias;
4841
4842 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004843 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004844 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
4848 /*
4849 * Skip '!' and '-' characters. They are handled later.
4850 */
4851 start_leader = *arg;
4852 while (**arg == '!' || **arg == '-' || **arg == '+')
4853 *arg = skipwhite(*arg + 1);
4854 end_leader = *arg;
4855
4856 switch (**arg)
4857 {
4858 /*
4859 * Number constant.
4860 */
4861 case '0':
4862 case '1':
4863 case '2':
4864 case '3':
4865 case '4':
4866 case '5':
4867 case '6':
4868 case '7':
4869 case '8':
4870 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004871 {
4872#ifdef FEAT_FLOAT
4873 char_u *p = skipdigits(*arg + 1);
4874 int get_float = FALSE;
4875
4876 /* We accept a float when the format matches
4877 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004878 * strict to avoid backwards compatibility problems.
4879 * Don't look for a float after the "." operator, so that
4880 * ":let vers = 1.2.3" doesn't fail. */
4881 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883 get_float = TRUE;
4884 p = skipdigits(p + 2);
4885 if (*p == 'e' || *p == 'E')
4886 {
4887 ++p;
4888 if (*p == '-' || *p == '+')
4889 ++p;
4890 if (!vim_isdigit(*p))
4891 get_float = FALSE;
4892 else
4893 p = skipdigits(p + 1);
4894 }
4895 if (ASCII_ISALPHA(*p) || *p == '.')
4896 get_float = FALSE;
4897 }
4898 if (get_float)
4899 {
4900 float_T f;
4901
4902 *arg += string2float(*arg, &f);
4903 if (evaluate)
4904 {
4905 rettv->v_type = VAR_FLOAT;
4906 rettv->vval.v_float = f;
4907 }
4908 }
4909 else
4910#endif
4911 {
4912 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4913 *arg += len;
4914 if (evaluate)
4915 {
4916 rettv->v_type = VAR_NUMBER;
4917 rettv->vval.v_number = n;
4918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 }
4920 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
4923 /*
4924 * String constant: "string".
4925 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004926 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 break;
4928
4929 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004930 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004932 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004933 break;
4934
4935 /*
4936 * List: [expr, expr]
4937 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004938 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 break;
4940
4941 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004942 * Dictionary: {key: val, key: val}
4943 */
4944 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4945 break;
4946
4947 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004948 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004950 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 break;
4952
4953 /*
4954 * Environment variable: $VAR.
4955 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004956 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 break;
4958
4959 /*
4960 * Register contents: @r.
4961 */
4962 case '@': ++*arg;
4963 if (evaluate)
4964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004965 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004966 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 if (**arg != NUL)
4969 ++*arg;
4970 break;
4971
4972 /*
4973 * nested expression: (expression).
4974 */
4975 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004976 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 if (**arg == ')')
4978 ++*arg;
4979 else if (ret == OK)
4980 {
4981 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004982 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 ret = FAIL;
4984 }
4985 break;
4986
Bram Moolenaar8c711452005-01-14 21:53:12 +00004987 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 break;
4989 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004990
4991 if (ret == NOTDONE)
4992 {
4993 /*
4994 * Must be a variable or function name.
4995 * Can also be a curly-braces kind of name: {expr}.
4996 */
4997 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004998 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999 if (alias != NULL)
5000 s = alias;
5001
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005002 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005003 ret = FAIL;
5004 else
5005 {
5006 if (**arg == '(') /* recursive! */
5007 {
5008 /* If "s" is the name of a variable of type VAR_FUNC
5009 * use its contents. */
5010 s = deref_func_name(s, &len);
5011
5012 /* Invoke the function. */
5013 ret = get_func_tv(s, len, rettv, arg,
5014 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005015 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016 /* Stop the expression evaluation when immediately
5017 * aborting on error, or when an interrupt occurred or
5018 * an exception was thrown but not caught. */
5019 if (aborting())
5020 {
5021 if (ret == OK)
5022 clear_tv(rettv);
5023 ret = FAIL;
5024 }
5025 }
5026 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005027 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005028 else
5029 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005030 }
5031
5032 if (alias != NULL)
5033 vim_free(alias);
5034 }
5035
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 *arg = skipwhite(*arg);
5037
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005038 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5039 * expr(expr). */
5040 if (ret == OK)
5041 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
5043 /*
5044 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5045 */
5046 if (ret == OK && evaluate && end_leader > start_leader)
5047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005048 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005049 int val = 0;
5050#ifdef FEAT_FLOAT
5051 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005052
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005053 if (rettv->v_type == VAR_FLOAT)
5054 f = rettv->vval.v_float;
5055 else
5056#endif
5057 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005058 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005060 clear_tv(rettv);
5061 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005063 else
5064 {
5065 while (end_leader > start_leader)
5066 {
5067 --end_leader;
5068 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005069 {
5070#ifdef FEAT_FLOAT
5071 if (rettv->v_type == VAR_FLOAT)
5072 f = !f;
5073 else
5074#endif
5075 val = !val;
5076 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005077 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005078 {
5079#ifdef FEAT_FLOAT
5080 if (rettv->v_type == VAR_FLOAT)
5081 f = -f;
5082 else
5083#endif
5084 val = -val;
5085 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005086 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005087#ifdef FEAT_FLOAT
5088 if (rettv->v_type == VAR_FLOAT)
5089 {
5090 clear_tv(rettv);
5091 rettv->vval.v_float = f;
5092 }
5093 else
5094#endif
5095 {
5096 clear_tv(rettv);
5097 rettv->v_type = VAR_NUMBER;
5098 rettv->vval.v_number = val;
5099 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 }
5102
5103 return ret;
5104}
5105
5106/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005107 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5108 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005109 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5110 */
5111 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005112eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005113 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005114 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005115 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005116 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005117{
5118 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005119 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005120 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 long len = -1;
5122 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005123 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005124 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005125
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005126 if (rettv->v_type == VAR_FUNC
5127#ifdef FEAT_FLOAT
5128 || rettv->v_type == VAR_FLOAT
5129#endif
5130 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005131 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005132 if (verbose)
5133 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005134 return FAIL;
5135 }
5136
Bram Moolenaar8c711452005-01-14 21:53:12 +00005137 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005138 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 /*
5140 * dict.name
5141 */
5142 key = *arg + 1;
5143 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5144 ;
5145 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005146 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005147 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005148 }
5149 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005150 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005151 /*
5152 * something[idx]
5153 *
5154 * Get the (first) variable from inside the [].
5155 */
5156 *arg = skipwhite(*arg + 1);
5157 if (**arg == ':')
5158 empty1 = TRUE;
5159 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5160 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005161 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5162 {
5163 /* not a number or string */
5164 clear_tv(&var1);
5165 return FAIL;
5166 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167
5168 /*
5169 * Get the second variable from inside the [:].
5170 */
5171 if (**arg == ':')
5172 {
5173 range = TRUE;
5174 *arg = skipwhite(*arg + 1);
5175 if (**arg == ']')
5176 empty2 = TRUE;
5177 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005179 if (!empty1)
5180 clear_tv(&var1);
5181 return FAIL;
5182 }
5183 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5184 {
5185 /* not a number or string */
5186 if (!empty1)
5187 clear_tv(&var1);
5188 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005189 return FAIL;
5190 }
5191 }
5192
5193 /* Check for the ']'. */
5194 if (**arg != ']')
5195 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005196 if (verbose)
5197 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005198 clear_tv(&var1);
5199 if (range)
5200 clear_tv(&var2);
5201 return FAIL;
5202 }
5203 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005204 }
5205
5206 if (evaluate)
5207 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 n1 = 0;
5209 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005210 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005211 n1 = get_tv_number(&var1);
5212 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005213 }
5214 if (range)
5215 {
5216 if (empty2)
5217 n2 = -1;
5218 else
5219 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005220 n2 = get_tv_number(&var2);
5221 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005222 }
5223 }
5224
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005225 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226 {
5227 case VAR_NUMBER:
5228 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005229 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005230 len = (long)STRLEN(s);
5231 if (range)
5232 {
5233 /* The resulting variable is a substring. If the indexes
5234 * are out of range the result is empty. */
5235 if (n1 < 0)
5236 {
5237 n1 = len + n1;
5238 if (n1 < 0)
5239 n1 = 0;
5240 }
5241 if (n2 < 0)
5242 n2 = len + n2;
5243 else if (n2 >= len)
5244 n2 = len;
5245 if (n1 >= len || n2 < 0 || n1 > n2)
5246 s = NULL;
5247 else
5248 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5249 }
5250 else
5251 {
5252 /* The resulting variable is a string of a single
5253 * character. If the index is too big or negative the
5254 * result is empty. */
5255 if (n1 >= len || n1 < 0)
5256 s = NULL;
5257 else
5258 s = vim_strnsave(s + n1, 1);
5259 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005260 clear_tv(rettv);
5261 rettv->v_type = VAR_STRING;
5262 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 break;
5264
5265 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005266 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267 if (n1 < 0)
5268 n1 = len + n1;
5269 if (!empty1 && (n1 < 0 || n1 >= len))
5270 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005271 /* For a range we allow invalid values and return an empty
5272 * list. A list index out of range is an error. */
5273 if (!range)
5274 {
5275 if (verbose)
5276 EMSGN(_(e_listidx), n1);
5277 return FAIL;
5278 }
5279 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 }
5281 if (range)
5282 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005283 list_T *l;
5284 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285
5286 if (n2 < 0)
5287 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005288 else if (n2 >= len)
5289 n2 = len - 1;
5290 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005291 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005292 l = list_alloc();
5293 if (l == NULL)
5294 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005295 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 n1 <= n2; ++n1)
5297 {
5298 if (list_append_tv(l, &item->li_tv) == FAIL)
5299 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005300 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301 return FAIL;
5302 }
5303 item = item->li_next;
5304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005305 clear_tv(rettv);
5306 rettv->v_type = VAR_LIST;
5307 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005308 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309 }
5310 else
5311 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005312 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005313 clear_tv(rettv);
5314 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005315 }
5316 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005317
5318 case VAR_DICT:
5319 if (range)
5320 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005321 if (verbose)
5322 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005323 if (len == -1)
5324 clear_tv(&var1);
5325 return FAIL;
5326 }
5327 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005328 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005329
5330 if (len == -1)
5331 {
5332 key = get_tv_string(&var1);
5333 if (*key == NUL)
5334 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005335 if (verbose)
5336 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005337 clear_tv(&var1);
5338 return FAIL;
5339 }
5340 }
5341
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005342 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005344 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005345 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 if (len == -1)
5347 clear_tv(&var1);
5348 if (item == NULL)
5349 return FAIL;
5350
5351 copy_tv(&item->di_tv, &var1);
5352 clear_tv(rettv);
5353 *rettv = var1;
5354 }
5355 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 }
5357 }
5358
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 return OK;
5360}
5361
5362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 * Get an option value.
5364 * "arg" points to the '&' or '+' before the option name.
5365 * "arg" is advanced to character after the option name.
5366 * Return OK or FAIL.
5367 */
5368 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005369get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005371 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 int evaluate;
5373{
5374 char_u *option_end;
5375 long numval;
5376 char_u *stringval;
5377 int opt_type;
5378 int c;
5379 int working = (**arg == '+'); /* has("+option") */
5380 int ret = OK;
5381 int opt_flags;
5382
5383 /*
5384 * Isolate the option name and find its value.
5385 */
5386 option_end = find_option_end(arg, &opt_flags);
5387 if (option_end == NULL)
5388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005389 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 EMSG2(_("E112: Option name missing: %s"), *arg);
5391 return FAIL;
5392 }
5393
5394 if (!evaluate)
5395 {
5396 *arg = option_end;
5397 return OK;
5398 }
5399
5400 c = *option_end;
5401 *option_end = NUL;
5402 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005403 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404
5405 if (opt_type == -3) /* invalid name */
5406 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005407 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 EMSG2(_("E113: Unknown option: %s"), *arg);
5409 ret = FAIL;
5410 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005411 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 {
5413 if (opt_type == -2) /* hidden string option */
5414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 rettv->v_type = VAR_STRING;
5416 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
5418 else if (opt_type == -1) /* hidden number option */
5419 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005420 rettv->v_type = VAR_NUMBER;
5421 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 }
5423 else if (opt_type == 1) /* number option */
5424 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005425 rettv->v_type = VAR_NUMBER;
5426 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 }
5428 else /* string option */
5429 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005430 rettv->v_type = VAR_STRING;
5431 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 }
5433 }
5434 else if (working && (opt_type == -2 || opt_type == -1))
5435 ret = FAIL;
5436
5437 *option_end = c; /* put back for error messages */
5438 *arg = option_end;
5439
5440 return ret;
5441}
5442
5443/*
5444 * Allocate a variable for a string constant.
5445 * Return OK or FAIL.
5446 */
5447 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005448get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 int evaluate;
5452{
5453 char_u *p;
5454 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 int extra = 0;
5456
5457 /*
5458 * Find the end of the string, skipping backslashed characters.
5459 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005460 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 {
5462 if (*p == '\\' && p[1] != NUL)
5463 {
5464 ++p;
5465 /* A "\<x>" form occupies at least 4 characters, and produces up
5466 * to 6 characters: reserve space for 2 extra */
5467 if (*p == '<')
5468 extra += 2;
5469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 }
5471
5472 if (*p != '"')
5473 {
5474 EMSG2(_("E114: Missing quote: %s"), *arg);
5475 return FAIL;
5476 }
5477
5478 /* If only parsing, set *arg and return here */
5479 if (!evaluate)
5480 {
5481 *arg = p + 1;
5482 return OK;
5483 }
5484
5485 /*
5486 * Copy the string into allocated memory, handling backslashed
5487 * characters.
5488 */
5489 name = alloc((unsigned)(p - *arg + extra));
5490 if (name == NULL)
5491 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005492 rettv->v_type = VAR_STRING;
5493 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494
Bram Moolenaar8c711452005-01-14 21:53:12 +00005495 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 {
5497 if (*p == '\\')
5498 {
5499 switch (*++p)
5500 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501 case 'b': *name++ = BS; ++p; break;
5502 case 'e': *name++ = ESC; ++p; break;
5503 case 'f': *name++ = FF; ++p; break;
5504 case 'n': *name++ = NL; ++p; break;
5505 case 'r': *name++ = CAR; ++p; break;
5506 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507
5508 case 'X': /* hex: "\x1", "\x12" */
5509 case 'x':
5510 case 'u': /* Unicode: "\u0023" */
5511 case 'U':
5512 if (vim_isxdigit(p[1]))
5513 {
5514 int n, nr;
5515 int c = toupper(*p);
5516
5517 if (c == 'X')
5518 n = 2;
5519 else
5520 n = 4;
5521 nr = 0;
5522 while (--n >= 0 && vim_isxdigit(p[1]))
5523 {
5524 ++p;
5525 nr = (nr << 4) + hex2nr(*p);
5526 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005527 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528#ifdef FEAT_MBYTE
5529 /* For "\u" store the number according to
5530 * 'encoding'. */
5531 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005532 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 else
5534#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005535 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 break;
5538
5539 /* octal: "\1", "\12", "\123" */
5540 case '0':
5541 case '1':
5542 case '2':
5543 case '3':
5544 case '4':
5545 case '5':
5546 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005547 case '7': *name = *p++ - '0';
5548 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005550 *name = (*name << 3) + *p++ - '0';
5551 if (*p >= '0' && *p <= '7')
5552 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005554 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 break;
5556
5557 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005558 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 if (extra != 0)
5560 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005561 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 break;
5563 }
5564 /* FALLTHROUGH */
5565
Bram Moolenaar8c711452005-01-14 21:53:12 +00005566 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 break;
5568 }
5569 }
5570 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005571 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 *arg = p + 1;
5576
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 return OK;
5578}
5579
5580/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005581 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 * Return OK or FAIL.
5583 */
5584 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005585get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 int evaluate;
5589{
5590 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005591 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005592 int reduce = 0;
5593
5594 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005596 */
5597 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5598 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005600 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005601 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005602 break;
5603 ++reduce;
5604 ++p;
5605 }
5606 }
5607
Bram Moolenaar8c711452005-01-14 21:53:12 +00005608 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005609 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005610 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005611 return FAIL;
5612 }
5613
Bram Moolenaar8c711452005-01-14 21:53:12 +00005614 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005615 if (!evaluate)
5616 {
5617 *arg = p + 1;
5618 return OK;
5619 }
5620
5621 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005622 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005623 */
5624 str = alloc((unsigned)((p - *arg) - reduce));
5625 if (str == NULL)
5626 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005627 rettv->v_type = VAR_STRING;
5628 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005629
Bram Moolenaar8c711452005-01-14 21:53:12 +00005630 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005631 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005632 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005633 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005634 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005635 break;
5636 ++p;
5637 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005638 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005639 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005641 *arg = p + 1;
5642
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005643 return OK;
5644}
5645
5646/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005647 * Allocate a variable for a List and fill it from "*arg".
5648 * Return OK or FAIL.
5649 */
5650 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005651get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005652 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005653 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005654 int evaluate;
5655{
Bram Moolenaar33570922005-01-25 22:26:29 +00005656 list_T *l = NULL;
5657 typval_T tv;
5658 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005659
5660 if (evaluate)
5661 {
5662 l = list_alloc();
5663 if (l == NULL)
5664 return FAIL;
5665 }
5666
5667 *arg = skipwhite(*arg + 1);
5668 while (**arg != ']' && **arg != NUL)
5669 {
5670 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5671 goto failret;
5672 if (evaluate)
5673 {
5674 item = listitem_alloc();
5675 if (item != NULL)
5676 {
5677 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005678 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 list_append(l, item);
5680 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005681 else
5682 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005683 }
5684
5685 if (**arg == ']')
5686 break;
5687 if (**arg != ',')
5688 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005689 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005690 goto failret;
5691 }
5692 *arg = skipwhite(*arg + 1);
5693 }
5694
5695 if (**arg != ']')
5696 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698failret:
5699 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005700 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005701 return FAIL;
5702 }
5703
5704 *arg = skipwhite(*arg + 1);
5705 if (evaluate)
5706 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005707 rettv->v_type = VAR_LIST;
5708 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005709 ++l->lv_refcount;
5710 }
5711
5712 return OK;
5713}
5714
5715/*
5716 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005717 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005718 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005719 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005720list_alloc()
5721{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005722 list_T *l;
5723
5724 l = (list_T *)alloc_clear(sizeof(list_T));
5725 if (l != NULL)
5726 {
5727 /* Prepend the list to the list of lists for garbage collection. */
5728 if (first_list != NULL)
5729 first_list->lv_used_prev = l;
5730 l->lv_used_prev = NULL;
5731 l->lv_used_next = first_list;
5732 first_list = l;
5733 }
5734 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005735}
5736
5737/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005738 * Allocate an empty list for a return value.
5739 * Returns OK or FAIL.
5740 */
5741 static int
5742rettv_list_alloc(rettv)
5743 typval_T *rettv;
5744{
5745 list_T *l = list_alloc();
5746
5747 if (l == NULL)
5748 return FAIL;
5749
5750 rettv->vval.v_list = l;
5751 rettv->v_type = VAR_LIST;
5752 ++l->lv_refcount;
5753 return OK;
5754}
5755
5756/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005757 * Unreference a list: decrement the reference count and free it when it
5758 * becomes zero.
5759 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005760 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005762 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005763{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005764 if (l != NULL && --l->lv_refcount <= 0)
5765 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766}
5767
5768/*
5769 * Free a list, including all items it points to.
5770 * Ignores the reference count.
5771 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005772 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005773list_free(l, recurse)
5774 list_T *l;
5775 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005776{
Bram Moolenaar33570922005-01-25 22:26:29 +00005777 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005778
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005779 /* Remove the list from the list of lists for garbage collection. */
5780 if (l->lv_used_prev == NULL)
5781 first_list = l->lv_used_next;
5782 else
5783 l->lv_used_prev->lv_used_next = l->lv_used_next;
5784 if (l->lv_used_next != NULL)
5785 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5786
Bram Moolenaard9fba312005-06-26 22:34:35 +00005787 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005788 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005789 /* Remove the item before deleting it. */
5790 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005791 if (recurse || (item->li_tv.v_type != VAR_LIST
5792 && item->li_tv.v_type != VAR_DICT))
5793 clear_tv(&item->li_tv);
5794 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005795 }
5796 vim_free(l);
5797}
5798
5799/*
5800 * Allocate a list item.
5801 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005802 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803listitem_alloc()
5804{
Bram Moolenaar33570922005-01-25 22:26:29 +00005805 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806}
5807
5808/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005809 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005810 */
5811 static void
5812listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005813 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005815 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005816 vim_free(item);
5817}
5818
5819/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005820 * Remove a list item from a List and free it. Also clears the value.
5821 */
5822 static void
5823listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005824 list_T *l;
5825 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005826{
5827 list_remove(l, item, item);
5828 listitem_free(item);
5829}
5830
5831/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832 * Get the number of items in a list.
5833 */
5834 static long
5835list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005836 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005838 if (l == NULL)
5839 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005840 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005841}
5842
5843/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005844 * Return TRUE when two lists have exactly the same values.
5845 */
5846 static int
5847list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005848 list_T *l1;
5849 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005850 int ic; /* ignore case for strings */
5851{
Bram Moolenaar33570922005-01-25 22:26:29 +00005852 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005853
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005854 if (l1 == NULL || l2 == NULL)
5855 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005856 if (l1 == l2)
5857 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005858 if (list_len(l1) != list_len(l2))
5859 return FALSE;
5860
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005861 for (item1 = l1->lv_first, item2 = l2->lv_first;
5862 item1 != NULL && item2 != NULL;
5863 item1 = item1->li_next, item2 = item2->li_next)
5864 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5865 return FALSE;
5866 return item1 == NULL && item2 == NULL;
5867}
5868
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005869#if defined(FEAT_PYTHON) || defined(PROTO)
5870/*
5871 * Return the dictitem that an entry in a hashtable points to.
5872 */
5873 dictitem_T *
5874dict_lookup(hi)
5875 hashitem_T *hi;
5876{
5877 return HI2DI(hi);
5878}
5879#endif
5880
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005881/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005882 * Return TRUE when two dictionaries have exactly the same key/values.
5883 */
5884 static int
5885dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005886 dict_T *d1;
5887 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005888 int ic; /* ignore case for strings */
5889{
Bram Moolenaar33570922005-01-25 22:26:29 +00005890 hashitem_T *hi;
5891 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005892 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005893
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005894 if (d1 == NULL || d2 == NULL)
5895 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005896 if (d1 == d2)
5897 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005898 if (dict_len(d1) != dict_len(d2))
5899 return FALSE;
5900
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005901 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005902 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005903 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005904 if (!HASHITEM_EMPTY(hi))
5905 {
5906 item2 = dict_find(d2, hi->hi_key, -1);
5907 if (item2 == NULL)
5908 return FALSE;
5909 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5910 return FALSE;
5911 --todo;
5912 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005913 }
5914 return TRUE;
5915}
5916
5917/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005918 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005919 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005920 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005921 */
5922 static int
5923tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005924 typval_T *tv1;
5925 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005926 int ic; /* ignore case */
5927{
5928 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005929 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005930 static int recursive = 0; /* cach recursive loops */
5931 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005932
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005933 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005934 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005935 /* Catch lists and dicts that have an endless loop by limiting
5936 * recursiveness to 1000. We guess they are equal then. */
5937 if (recursive >= 1000)
5938 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005939
5940 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005941 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005942 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005943 ++recursive;
5944 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5945 --recursive;
5946 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005947
5948 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005949 ++recursive;
5950 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5951 --recursive;
5952 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005953
5954 case VAR_FUNC:
5955 return (tv1->vval.v_string != NULL
5956 && tv2->vval.v_string != NULL
5957 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5958
5959 case VAR_NUMBER:
5960 return tv1->vval.v_number == tv2->vval.v_number;
5961
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005962#ifdef FEAT_FLOAT
5963 case VAR_FLOAT:
5964 return tv1->vval.v_float == tv2->vval.v_float;
5965#endif
5966
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005967 case VAR_STRING:
5968 s1 = get_tv_string_buf(tv1, buf1);
5969 s2 = get_tv_string_buf(tv2, buf2);
5970 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005971 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005972
5973 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005974 return TRUE;
5975}
5976
5977/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978 * Locate item with index "n" in list "l" and return it.
5979 * A negative index is counted from the end; -1 is the last item.
5980 * Returns NULL when "n" is out of range.
5981 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005982 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005984 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985 long n;
5986{
Bram Moolenaar33570922005-01-25 22:26:29 +00005987 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988 long idx;
5989
5990 if (l == NULL)
5991 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005992
5993 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005995 n = l->lv_len + n;
5996
5997 /* Check for index out of range. */
5998 if (n < 0 || n >= l->lv_len)
5999 return NULL;
6000
6001 /* When there is a cached index may start search from there. */
6002 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006004 if (n < l->lv_idx / 2)
6005 {
6006 /* closest to the start of the list */
6007 item = l->lv_first;
6008 idx = 0;
6009 }
6010 else if (n > (l->lv_idx + l->lv_len) / 2)
6011 {
6012 /* closest to the end of the list */
6013 item = l->lv_last;
6014 idx = l->lv_len - 1;
6015 }
6016 else
6017 {
6018 /* closest to the cached index */
6019 item = l->lv_idx_item;
6020 idx = l->lv_idx;
6021 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022 }
6023 else
6024 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006025 if (n < l->lv_len / 2)
6026 {
6027 /* closest to the start of the list */
6028 item = l->lv_first;
6029 idx = 0;
6030 }
6031 else
6032 {
6033 /* closest to the end of the list */
6034 item = l->lv_last;
6035 idx = l->lv_len - 1;
6036 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006038
6039 while (n > idx)
6040 {
6041 /* search forward */
6042 item = item->li_next;
6043 ++idx;
6044 }
6045 while (n < idx)
6046 {
6047 /* search backward */
6048 item = item->li_prev;
6049 --idx;
6050 }
6051
6052 /* cache the used index */
6053 l->lv_idx = idx;
6054 l->lv_idx_item = item;
6055
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 return item;
6057}
6058
6059/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006060 * Get list item "l[idx]" as a number.
6061 */
6062 static long
6063list_find_nr(l, idx, errorp)
6064 list_T *l;
6065 long idx;
6066 int *errorp; /* set to TRUE when something wrong */
6067{
6068 listitem_T *li;
6069
6070 li = list_find(l, idx);
6071 if (li == NULL)
6072 {
6073 if (errorp != NULL)
6074 *errorp = TRUE;
6075 return -1L;
6076 }
6077 return get_tv_number_chk(&li->li_tv, errorp);
6078}
6079
6080/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006081 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6082 */
6083 char_u *
6084list_find_str(l, idx)
6085 list_T *l;
6086 long idx;
6087{
6088 listitem_T *li;
6089
6090 li = list_find(l, idx - 1);
6091 if (li == NULL)
6092 {
6093 EMSGN(_(e_listidx), idx);
6094 return NULL;
6095 }
6096 return get_tv_string(&li->li_tv);
6097}
6098
6099/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006100 * Locate "item" list "l" and return its index.
6101 * Returns -1 when "item" is not in the list.
6102 */
6103 static long
6104list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006105 list_T *l;
6106 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006107{
6108 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006109 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006110
6111 if (l == NULL)
6112 return -1;
6113 idx = 0;
6114 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6115 ++idx;
6116 if (li == NULL)
6117 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006118 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006119}
6120
6121/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006122 * Append item "item" to the end of list "l".
6123 */
6124 static void
6125list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006126 list_T *l;
6127 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006128{
6129 if (l->lv_last == NULL)
6130 {
6131 /* empty list */
6132 l->lv_first = item;
6133 l->lv_last = item;
6134 item->li_prev = NULL;
6135 }
6136 else
6137 {
6138 l->lv_last->li_next = item;
6139 item->li_prev = l->lv_last;
6140 l->lv_last = item;
6141 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006142 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006143 item->li_next = NULL;
6144}
6145
6146/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006148 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149 */
6150 static int
6151list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 list_T *l;
6153 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006154{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006155 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006156
Bram Moolenaar05159a02005-02-26 23:04:13 +00006157 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006158 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006159 copy_tv(tv, &li->li_tv);
6160 list_append(l, li);
6161 return OK;
6162}
6163
6164/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006165 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006166 * Return FAIL when out of memory.
6167 */
6168 int
6169list_append_dict(list, dict)
6170 list_T *list;
6171 dict_T *dict;
6172{
6173 listitem_T *li = listitem_alloc();
6174
6175 if (li == NULL)
6176 return FAIL;
6177 li->li_tv.v_type = VAR_DICT;
6178 li->li_tv.v_lock = 0;
6179 li->li_tv.vval.v_dict = dict;
6180 list_append(list, li);
6181 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006182 return OK;
6183}
6184
6185/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006186 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006187 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006188 * Returns FAIL when out of memory.
6189 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006190 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006191list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006192 list_T *l;
6193 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006194 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006195{
6196 listitem_T *li = listitem_alloc();
6197
6198 if (li == NULL)
6199 return FAIL;
6200 list_append(l, li);
6201 li->li_tv.v_type = VAR_STRING;
6202 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006203 if (str == NULL)
6204 li->li_tv.vval.v_string = NULL;
6205 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006206 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006207 return FAIL;
6208 return OK;
6209}
6210
6211/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006212 * Append "n" to list "l".
6213 * Returns FAIL when out of memory.
6214 */
6215 static int
6216list_append_number(l, n)
6217 list_T *l;
6218 varnumber_T n;
6219{
6220 listitem_T *li;
6221
6222 li = listitem_alloc();
6223 if (li == NULL)
6224 return FAIL;
6225 li->li_tv.v_type = VAR_NUMBER;
6226 li->li_tv.v_lock = 0;
6227 li->li_tv.vval.v_number = n;
6228 list_append(l, li);
6229 return OK;
6230}
6231
6232/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006233 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006234 * If "item" is NULL append at the end.
6235 * Return FAIL when out of memory.
6236 */
6237 static int
6238list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006239 list_T *l;
6240 typval_T *tv;
6241 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006242{
Bram Moolenaar33570922005-01-25 22:26:29 +00006243 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006244
6245 if (ni == NULL)
6246 return FAIL;
6247 copy_tv(tv, &ni->li_tv);
6248 if (item == NULL)
6249 /* Append new item at end of list. */
6250 list_append(l, ni);
6251 else
6252 {
6253 /* Insert new item before existing item. */
6254 ni->li_prev = item->li_prev;
6255 ni->li_next = item;
6256 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006257 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006258 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006259 ++l->lv_idx;
6260 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006261 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006262 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006263 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006264 l->lv_idx_item = NULL;
6265 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006266 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006267 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006268 }
6269 return OK;
6270}
6271
6272/*
6273 * Extend "l1" with "l2".
6274 * If "bef" is NULL append at the end, otherwise insert before this item.
6275 * Returns FAIL when out of memory.
6276 */
6277 static int
6278list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006279 list_T *l1;
6280 list_T *l2;
6281 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006282{
Bram Moolenaar33570922005-01-25 22:26:29 +00006283 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006284 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006285
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006286 /* We also quit the loop when we have inserted the original item count of
6287 * the list, avoid a hang when we extend a list with itself. */
6288 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006289 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6290 return FAIL;
6291 return OK;
6292}
6293
6294/*
6295 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6296 * Return FAIL when out of memory.
6297 */
6298 static int
6299list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006300 list_T *l1;
6301 list_T *l2;
6302 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006303{
Bram Moolenaar33570922005-01-25 22:26:29 +00006304 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006305
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006306 if (l1 == NULL || l2 == NULL)
6307 return FAIL;
6308
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006309 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006310 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006311 if (l == NULL)
6312 return FAIL;
6313 tv->v_type = VAR_LIST;
6314 tv->vval.v_list = l;
6315
6316 /* append all items from the second list */
6317 return list_extend(l, l2, NULL);
6318}
6319
6320/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006321 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006323 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324 * Returns NULL when out of memory.
6325 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006326 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006327list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006328 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006330 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331{
Bram Moolenaar33570922005-01-25 22:26:29 +00006332 list_T *copy;
6333 listitem_T *item;
6334 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006335
6336 if (orig == NULL)
6337 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006338
6339 copy = list_alloc();
6340 if (copy != NULL)
6341 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006342 if (copyID != 0)
6343 {
6344 /* Do this before adding the items, because one of the items may
6345 * refer back to this list. */
6346 orig->lv_copyID = copyID;
6347 orig->lv_copylist = copy;
6348 }
6349 for (item = orig->lv_first; item != NULL && !got_int;
6350 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351 {
6352 ni = listitem_alloc();
6353 if (ni == NULL)
6354 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006355 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006356 {
6357 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6358 {
6359 vim_free(ni);
6360 break;
6361 }
6362 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006363 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006364 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006365 list_append(copy, ni);
6366 }
6367 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006368 if (item != NULL)
6369 {
6370 list_unref(copy);
6371 copy = NULL;
6372 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006373 }
6374
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006375 return copy;
6376}
6377
6378/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006379 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006380 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006381 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006382 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006383list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 list_T *l;
6385 listitem_T *item;
6386 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006387{
Bram Moolenaar33570922005-01-25 22:26:29 +00006388 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006389
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006390 /* notify watchers */
6391 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006392 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006393 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006394 list_fix_watch(l, ip);
6395 if (ip == item2)
6396 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006398
6399 if (item2->li_next == NULL)
6400 l->lv_last = item->li_prev;
6401 else
6402 item2->li_next->li_prev = item->li_prev;
6403 if (item->li_prev == NULL)
6404 l->lv_first = item2->li_next;
6405 else
6406 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006407 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006408}
6409
6410/*
6411 * Return an allocated string with the string representation of a list.
6412 * May return NULL.
6413 */
6414 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006415list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006417 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418{
6419 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006420
6421 if (tv->vval.v_list == NULL)
6422 return NULL;
6423 ga_init2(&ga, (int)sizeof(char), 80);
6424 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006425 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006426 {
6427 vim_free(ga.ga_data);
6428 return NULL;
6429 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006430 ga_append(&ga, ']');
6431 ga_append(&ga, NUL);
6432 return (char_u *)ga.ga_data;
6433}
6434
6435/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006436 * Join list "l" into a string in "*gap", using separator "sep".
6437 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006438 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006439 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006440 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006441list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006442 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006443 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006444 char_u *sep;
6445 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006446 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006447{
6448 int first = TRUE;
6449 char_u *tofree;
6450 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006451 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006452 char_u *s;
6453
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006454 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006455 {
6456 if (first)
6457 first = FALSE;
6458 else
6459 ga_concat(gap, sep);
6460
6461 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006462 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006463 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006464 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006465 if (s != NULL)
6466 ga_concat(gap, s);
6467 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006468 if (s == NULL)
6469 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006470 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006471 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006472}
6473
6474/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006475 * Garbage collection for lists and dictionaries.
6476 *
6477 * We use reference counts to be able to free most items right away when they
6478 * are no longer used. But for composite items it's possible that it becomes
6479 * unused while the reference count is > 0: When there is a recursive
6480 * reference. Example:
6481 * :let l = [1, 2, 3]
6482 * :let d = {9: l}
6483 * :let l[1] = d
6484 *
6485 * Since this is quite unusual we handle this with garbage collection: every
6486 * once in a while find out which lists and dicts are not referenced from any
6487 * variable.
6488 *
6489 * Here is a good reference text about garbage collection (refers to Python
6490 * but it applies to all reference-counting mechanisms):
6491 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006492 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006493
6494/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006495 * Do garbage collection for lists and dicts.
6496 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006497 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006498 int
6499garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006500{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006501 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006502 buf_T *buf;
6503 win_T *wp;
6504 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006505 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006506 int did_free;
6507 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006508#ifdef FEAT_WINDOWS
6509 tabpage_T *tp;
6510#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006511
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006512 /* Only do this once. */
6513 want_garbage_collect = FALSE;
6514 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006515 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006516
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006517 /* We advance by two because we add one for items referenced through
6518 * previous_funccal. */
6519 current_copyID += COPYID_INC;
6520 copyID = current_copyID;
6521
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006522 /*
6523 * 1. Go through all accessible variables and mark all lists and dicts
6524 * with copyID.
6525 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006526
6527 /* Don't free variables in the previous_funccal list unless they are only
6528 * referenced through previous_funccal. This must be first, because if
6529 * the item is referenced elsewhere it must not be freed. */
6530 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6531 {
6532 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6533 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6534 }
6535
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006536 /* script-local variables */
6537 for (i = 1; i <= ga_scripts.ga_len; ++i)
6538 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6539
6540 /* buffer-local variables */
6541 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6542 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6543
6544 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006545 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006546 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6547
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006548#ifdef FEAT_WINDOWS
6549 /* tabpage-local variables */
6550 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6551 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6552#endif
6553
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006554 /* global variables */
6555 set_ref_in_ht(&globvarht, copyID);
6556
6557 /* function-local variables */
6558 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6559 {
6560 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6561 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6562 }
6563
Bram Moolenaard812df62008-11-09 12:46:09 +00006564 /* v: vars */
6565 set_ref_in_ht(&vimvarht, copyID);
6566
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006567 /* Free lists and dictionaries that are not referenced. */
6568 did_free = free_unref_items(copyID);
6569
6570 /* check if any funccal can be freed now */
6571 for (pfc = &previous_funccal; *pfc != NULL; )
6572 {
6573 if (can_free_funccal(*pfc, copyID))
6574 {
6575 fc = *pfc;
6576 *pfc = fc->caller;
6577 free_funccal(fc, TRUE);
6578 did_free = TRUE;
6579 did_free_funccal = TRUE;
6580 }
6581 else
6582 pfc = &(*pfc)->caller;
6583 }
6584 if (did_free_funccal)
6585 /* When a funccal was freed some more items might be garbage
6586 * collected, so run again. */
6587 (void)garbage_collect();
6588
6589 return did_free;
6590}
6591
6592/*
6593 * Free lists and dictionaries that are no longer referenced.
6594 */
6595 static int
6596free_unref_items(copyID)
6597 int copyID;
6598{
6599 dict_T *dd;
6600 list_T *ll;
6601 int did_free = FALSE;
6602
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006603 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006604 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006605 */
6606 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006607 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006608 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006609 /* Free the Dictionary and ordinary items it contains, but don't
6610 * recurse into Lists and Dictionaries, they will be in the list
6611 * of dicts or list of lists. */
6612 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006613 did_free = TRUE;
6614
6615 /* restart, next dict may also have been freed */
6616 dd = first_dict;
6617 }
6618 else
6619 dd = dd->dv_used_next;
6620
6621 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006622 * Go through the list of lists and free items without the copyID.
6623 * But don't free a list that has a watcher (used in a for loop), these
6624 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006625 */
6626 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006627 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6628 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006629 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006630 /* Free the List and ordinary items it contains, but don't recurse
6631 * into Lists and Dictionaries, they will be in the list of dicts
6632 * or list of lists. */
6633 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006634 did_free = TRUE;
6635
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006636 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006637 ll = first_list;
6638 }
6639 else
6640 ll = ll->lv_used_next;
6641
6642 return did_free;
6643}
6644
6645/*
6646 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6647 */
6648 static void
6649set_ref_in_ht(ht, copyID)
6650 hashtab_T *ht;
6651 int copyID;
6652{
6653 int todo;
6654 hashitem_T *hi;
6655
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006656 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006657 for (hi = ht->ht_array; todo > 0; ++hi)
6658 if (!HASHITEM_EMPTY(hi))
6659 {
6660 --todo;
6661 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6662 }
6663}
6664
6665/*
6666 * Mark all lists and dicts referenced through list "l" with "copyID".
6667 */
6668 static void
6669set_ref_in_list(l, copyID)
6670 list_T *l;
6671 int copyID;
6672{
6673 listitem_T *li;
6674
6675 for (li = l->lv_first; li != NULL; li = li->li_next)
6676 set_ref_in_item(&li->li_tv, copyID);
6677}
6678
6679/*
6680 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6681 */
6682 static void
6683set_ref_in_item(tv, copyID)
6684 typval_T *tv;
6685 int copyID;
6686{
6687 dict_T *dd;
6688 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006689
6690 switch (tv->v_type)
6691 {
6692 case VAR_DICT:
6693 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006694 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006695 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006696 /* Didn't see this dict yet. */
6697 dd->dv_copyID = copyID;
6698 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006699 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006700 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006701
6702 case VAR_LIST:
6703 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006704 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006705 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006706 /* Didn't see this list yet. */
6707 ll->lv_copyID = copyID;
6708 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006709 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006710 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006711 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006712 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006713}
6714
6715/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006716 * Allocate an empty header for a dictionary.
6717 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006718 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006719dict_alloc()
6720{
Bram Moolenaar33570922005-01-25 22:26:29 +00006721 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006722
Bram Moolenaar33570922005-01-25 22:26:29 +00006723 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006724 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006725 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006726 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006727 if (first_dict != NULL)
6728 first_dict->dv_used_prev = d;
6729 d->dv_used_next = first_dict;
6730 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006731 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006732
Bram Moolenaar33570922005-01-25 22:26:29 +00006733 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006734 d->dv_lock = 0;
6735 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006736 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006737 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006738 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006739}
6740
6741/*
6742 * Unreference a Dictionary: decrement the reference count and free it when it
6743 * becomes zero.
6744 */
6745 static void
6746dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006747 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006748{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006749 if (d != NULL && --d->dv_refcount <= 0)
6750 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006751}
6752
6753/*
6754 * Free a Dictionary, including all items it contains.
6755 * Ignores the reference count.
6756 */
6757 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006758dict_free(d, recurse)
6759 dict_T *d;
6760 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006761{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006762 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006763 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006764 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006765
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006766 /* Remove the dict from the list of dicts for garbage collection. */
6767 if (d->dv_used_prev == NULL)
6768 first_dict = d->dv_used_next;
6769 else
6770 d->dv_used_prev->dv_used_next = d->dv_used_next;
6771 if (d->dv_used_next != NULL)
6772 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6773
6774 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006775 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006776 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006777 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006778 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006779 if (!HASHITEM_EMPTY(hi))
6780 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006781 /* Remove the item before deleting it, just in case there is
6782 * something recursive causing trouble. */
6783 di = HI2DI(hi);
6784 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006785 if (recurse || (di->di_tv.v_type != VAR_LIST
6786 && di->di_tv.v_type != VAR_DICT))
6787 clear_tv(&di->di_tv);
6788 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006789 --todo;
6790 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006791 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006792 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006793 vim_free(d);
6794}
6795
6796/*
6797 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006798 * The "key" is copied to the new item.
6799 * Note that the value of the item "di_tv" still needs to be initialized!
6800 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006801 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006802 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006803dictitem_alloc(key)
6804 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006805{
Bram Moolenaar33570922005-01-25 22:26:29 +00006806 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006807
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006808 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006809 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006810 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006811 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006812 di->di_flags = 0;
6813 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006814 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006815}
6816
6817/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006818 * Make a copy of a Dictionary item.
6819 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006820 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006821dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006822 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006823{
Bram Moolenaar33570922005-01-25 22:26:29 +00006824 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006825
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006826 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6827 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006828 if (di != NULL)
6829 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006830 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006831 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006832 copy_tv(&org->di_tv, &di->di_tv);
6833 }
6834 return di;
6835}
6836
6837/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006838 * Remove item "item" from Dictionary "dict" and free it.
6839 */
6840 static void
6841dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006842 dict_T *dict;
6843 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006844{
Bram Moolenaar33570922005-01-25 22:26:29 +00006845 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006846
Bram Moolenaar33570922005-01-25 22:26:29 +00006847 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006848 if (HASHITEM_EMPTY(hi))
6849 EMSG2(_(e_intern2), "dictitem_remove()");
6850 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006851 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006852 dictitem_free(item);
6853}
6854
6855/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006856 * Free a dict item. Also clears the value.
6857 */
6858 static void
6859dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006860 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006861{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006862 clear_tv(&item->di_tv);
6863 vim_free(item);
6864}
6865
6866/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006867 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6868 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006869 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006870 * Returns NULL when out of memory.
6871 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006872 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006873dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006874 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006875 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006876 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006877{
Bram Moolenaar33570922005-01-25 22:26:29 +00006878 dict_T *copy;
6879 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006880 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006881 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006882
6883 if (orig == NULL)
6884 return NULL;
6885
6886 copy = dict_alloc();
6887 if (copy != NULL)
6888 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006889 if (copyID != 0)
6890 {
6891 orig->dv_copyID = copyID;
6892 orig->dv_copydict = copy;
6893 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006894 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006895 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006896 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006897 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006898 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006899 --todo;
6900
6901 di = dictitem_alloc(hi->hi_key);
6902 if (di == NULL)
6903 break;
6904 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006905 {
6906 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6907 copyID) == FAIL)
6908 {
6909 vim_free(di);
6910 break;
6911 }
6912 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006913 else
6914 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6915 if (dict_add(copy, di) == FAIL)
6916 {
6917 dictitem_free(di);
6918 break;
6919 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006920 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006921 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006922
Bram Moolenaare9a41262005-01-15 22:18:47 +00006923 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006924 if (todo > 0)
6925 {
6926 dict_unref(copy);
6927 copy = NULL;
6928 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006929 }
6930
6931 return copy;
6932}
6933
6934/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006935 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006936 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006937 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006938 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006939dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006940 dict_T *d;
6941 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006942{
Bram Moolenaar33570922005-01-25 22:26:29 +00006943 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006944}
6945
Bram Moolenaar8c711452005-01-14 21:53:12 +00006946/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006947 * Add a number or string entry to dictionary "d".
6948 * When "str" is NULL use number "nr", otherwise use "str".
6949 * Returns FAIL when out of memory and when key already exists.
6950 */
6951 int
6952dict_add_nr_str(d, key, nr, str)
6953 dict_T *d;
6954 char *key;
6955 long nr;
6956 char_u *str;
6957{
6958 dictitem_T *item;
6959
6960 item = dictitem_alloc((char_u *)key);
6961 if (item == NULL)
6962 return FAIL;
6963 item->di_tv.v_lock = 0;
6964 if (str == NULL)
6965 {
6966 item->di_tv.v_type = VAR_NUMBER;
6967 item->di_tv.vval.v_number = nr;
6968 }
6969 else
6970 {
6971 item->di_tv.v_type = VAR_STRING;
6972 item->di_tv.vval.v_string = vim_strsave(str);
6973 }
6974 if (dict_add(d, item) == FAIL)
6975 {
6976 dictitem_free(item);
6977 return FAIL;
6978 }
6979 return OK;
6980}
6981
6982/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006983 * Get the number of items in a Dictionary.
6984 */
6985 static long
6986dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006987 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006988{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006989 if (d == NULL)
6990 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006991 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006992}
6993
6994/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006995 * Find item "key[len]" in Dictionary "d".
6996 * If "len" is negative use strlen(key).
6997 * Returns NULL when not found.
6998 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006999 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007000dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007001 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007002 char_u *key;
7003 int len;
7004{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007005#define AKEYLEN 200
7006 char_u buf[AKEYLEN];
7007 char_u *akey;
7008 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007009 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007010
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007011 if (len < 0)
7012 akey = key;
7013 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007014 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007015 tofree = akey = vim_strnsave(key, len);
7016 if (akey == NULL)
7017 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007018 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007019 else
7020 {
7021 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007022 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007023 akey = buf;
7024 }
7025
Bram Moolenaar33570922005-01-25 22:26:29 +00007026 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007027 vim_free(tofree);
7028 if (HASHITEM_EMPTY(hi))
7029 return NULL;
7030 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007031}
7032
7033/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007034 * Get a string item from a dictionary.
7035 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007036 * Returns NULL if the entry doesn't exist or out of memory.
7037 */
7038 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007039get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007040 dict_T *d;
7041 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007042 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007043{
7044 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007045 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007046
7047 di = dict_find(d, key, -1);
7048 if (di == NULL)
7049 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007050 s = get_tv_string(&di->di_tv);
7051 if (save && s != NULL)
7052 s = vim_strsave(s);
7053 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007054}
7055
7056/*
7057 * Get a number item from a dictionary.
7058 * Returns 0 if the entry doesn't exist or out of memory.
7059 */
7060 long
7061get_dict_number(d, key)
7062 dict_T *d;
7063 char_u *key;
7064{
7065 dictitem_T *di;
7066
7067 di = dict_find(d, key, -1);
7068 if (di == NULL)
7069 return 0;
7070 return get_tv_number(&di->di_tv);
7071}
7072
7073/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007074 * Return an allocated string with the string representation of a Dictionary.
7075 * May return NULL.
7076 */
7077 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007078dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007079 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007080 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007081{
7082 garray_T ga;
7083 int first = TRUE;
7084 char_u *tofree;
7085 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007086 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007087 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007088 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007089 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007090
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007091 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007092 return NULL;
7093 ga_init2(&ga, (int)sizeof(char), 80);
7094 ga_append(&ga, '{');
7095
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007096 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007097 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007098 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007099 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007100 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007101 --todo;
7102
7103 if (first)
7104 first = FALSE;
7105 else
7106 ga_concat(&ga, (char_u *)", ");
7107
7108 tofree = string_quote(hi->hi_key, FALSE);
7109 if (tofree != NULL)
7110 {
7111 ga_concat(&ga, tofree);
7112 vim_free(tofree);
7113 }
7114 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007115 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007116 if (s != NULL)
7117 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007118 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007119 if (s == NULL)
7120 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007121 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007122 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007123 if (todo > 0)
7124 {
7125 vim_free(ga.ga_data);
7126 return NULL;
7127 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007128
7129 ga_append(&ga, '}');
7130 ga_append(&ga, NUL);
7131 return (char_u *)ga.ga_data;
7132}
7133
7134/*
7135 * Allocate a variable for a Dictionary and fill it from "*arg".
7136 * Return OK or FAIL. Returns NOTDONE for {expr}.
7137 */
7138 static int
7139get_dict_tv(arg, rettv, evaluate)
7140 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007141 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007142 int evaluate;
7143{
Bram Moolenaar33570922005-01-25 22:26:29 +00007144 dict_T *d = NULL;
7145 typval_T tvkey;
7146 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007147 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007148 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007149 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007150 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007151
7152 /*
7153 * First check if it's not a curly-braces thing: {expr}.
7154 * Must do this without evaluating, otherwise a function may be called
7155 * twice. Unfortunately this means we need to call eval1() twice for the
7156 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007158 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007159 if (*start != '}')
7160 {
7161 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7162 return FAIL;
7163 if (*start == '}')
7164 return NOTDONE;
7165 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007166
7167 if (evaluate)
7168 {
7169 d = dict_alloc();
7170 if (d == NULL)
7171 return FAIL;
7172 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007173 tvkey.v_type = VAR_UNKNOWN;
7174 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175
7176 *arg = skipwhite(*arg + 1);
7177 while (**arg != '}' && **arg != NUL)
7178 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007179 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007180 goto failret;
7181 if (**arg != ':')
7182 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007183 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007184 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007185 goto failret;
7186 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007187 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007188 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007189 key = get_tv_string_buf_chk(&tvkey, buf);
7190 if (key == NULL || *key == NUL)
7191 {
7192 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7193 if (key != NULL)
7194 EMSG(_(e_emptykey));
7195 clear_tv(&tvkey);
7196 goto failret;
7197 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007199
7200 *arg = skipwhite(*arg + 1);
7201 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7202 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007203 if (evaluate)
7204 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007205 goto failret;
7206 }
7207 if (evaluate)
7208 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007209 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210 if (item != NULL)
7211 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007212 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007213 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214 clear_tv(&tv);
7215 goto failret;
7216 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007217 item = dictitem_alloc(key);
7218 clear_tv(&tvkey);
7219 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007222 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007223 if (dict_add(d, item) == FAIL)
7224 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225 }
7226 }
7227
7228 if (**arg == '}')
7229 break;
7230 if (**arg != ',')
7231 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007232 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233 goto failret;
7234 }
7235 *arg = skipwhite(*arg + 1);
7236 }
7237
7238 if (**arg != '}')
7239 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007240 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007241failret:
7242 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007243 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244 return FAIL;
7245 }
7246
7247 *arg = skipwhite(*arg + 1);
7248 if (evaluate)
7249 {
7250 rettv->v_type = VAR_DICT;
7251 rettv->vval.v_dict = d;
7252 ++d->dv_refcount;
7253 }
7254
7255 return OK;
7256}
7257
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007259 * Return a string with the string representation of a variable.
7260 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007261 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007262 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007263 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007264 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007265 */
7266 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007267echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007268 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007269 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007270 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007271 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007272{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007273 static int recurse = 0;
7274 char_u *r = NULL;
7275
Bram Moolenaar33570922005-01-25 22:26:29 +00007276 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007277 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007278 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007279 *tofree = NULL;
7280 return NULL;
7281 }
7282 ++recurse;
7283
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007284 switch (tv->v_type)
7285 {
7286 case VAR_FUNC:
7287 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007288 r = tv->vval.v_string;
7289 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007290
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007291 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007292 if (tv->vval.v_list == NULL)
7293 {
7294 *tofree = NULL;
7295 r = NULL;
7296 }
7297 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7298 {
7299 *tofree = NULL;
7300 r = (char_u *)"[...]";
7301 }
7302 else
7303 {
7304 tv->vval.v_list->lv_copyID = copyID;
7305 *tofree = list2string(tv, copyID);
7306 r = *tofree;
7307 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007308 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007309
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007311 if (tv->vval.v_dict == NULL)
7312 {
7313 *tofree = NULL;
7314 r = NULL;
7315 }
7316 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7317 {
7318 *tofree = NULL;
7319 r = (char_u *)"{...}";
7320 }
7321 else
7322 {
7323 tv->vval.v_dict->dv_copyID = copyID;
7324 *tofree = dict2string(tv, copyID);
7325 r = *tofree;
7326 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007328
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007329 case VAR_STRING:
7330 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007331 *tofree = NULL;
7332 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007333 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007334
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007335#ifdef FEAT_FLOAT
7336 case VAR_FLOAT:
7337 *tofree = NULL;
7338 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7339 r = numbuf;
7340 break;
7341#endif
7342
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007343 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007344 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007345 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007346 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007347
7348 --recurse;
7349 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007350}
7351
7352/*
7353 * Return a string with the string representation of a variable.
7354 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7355 * "numbuf" is used for a number.
7356 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007357 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007358 */
7359 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007360tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007362 char_u **tofree;
7363 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007364 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007365{
7366 switch (tv->v_type)
7367 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007368 case VAR_FUNC:
7369 *tofree = string_quote(tv->vval.v_string, TRUE);
7370 return *tofree;
7371 case VAR_STRING:
7372 *tofree = string_quote(tv->vval.v_string, FALSE);
7373 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007374#ifdef FEAT_FLOAT
7375 case VAR_FLOAT:
7376 *tofree = NULL;
7377 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7378 return numbuf;
7379#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007380 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007381 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007382 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007383 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007384 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007385 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007386 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007387 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007388}
7389
7390/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007391 * Return string "str" in ' quotes, doubling ' characters.
7392 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007393 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007394 */
7395 static char_u *
7396string_quote(str, function)
7397 char_u *str;
7398 int function;
7399{
Bram Moolenaar33570922005-01-25 22:26:29 +00007400 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007401 char_u *p, *r, *s;
7402
Bram Moolenaar33570922005-01-25 22:26:29 +00007403 len = (function ? 13 : 3);
7404 if (str != NULL)
7405 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007406 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007407 for (p = str; *p != NUL; mb_ptr_adv(p))
7408 if (*p == '\'')
7409 ++len;
7410 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007411 s = r = alloc(len);
7412 if (r != NULL)
7413 {
7414 if (function)
7415 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007417 r += 10;
7418 }
7419 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007421 if (str != NULL)
7422 for (p = str; *p != NUL; )
7423 {
7424 if (*p == '\'')
7425 *r++ = '\'';
7426 MB_COPY_CHAR(p, r);
7427 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007429 if (function)
7430 *r++ = ')';
7431 *r++ = NUL;
7432 }
7433 return s;
7434}
7435
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007436#ifdef FEAT_FLOAT
7437/*
7438 * Convert the string "text" to a floating point number.
7439 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7440 * this always uses a decimal point.
7441 * Returns the length of the text that was consumed.
7442 */
7443 static int
7444string2float(text, value)
7445 char_u *text;
7446 float_T *value; /* result stored here */
7447{
7448 char *s = (char *)text;
7449 float_T f;
7450
7451 f = strtod(s, &s);
7452 *value = f;
7453 return (int)((char_u *)s - text);
7454}
7455#endif
7456
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 * Get the value of an environment variable.
7459 * "arg" is pointing to the '$'. It is advanced to after the name.
7460 * If the environment variable was not set, silently assume it is empty.
7461 * Always return OK.
7462 */
7463 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007464get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 int evaluate;
7468{
7469 char_u *string = NULL;
7470 int len;
7471 int cc;
7472 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007473 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474
7475 ++*arg;
7476 name = *arg;
7477 len = get_env_len(arg);
7478 if (evaluate)
7479 {
7480 if (len != 0)
7481 {
7482 cc = name[len];
7483 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007484 /* first try vim_getenv(), fast for normal environment vars */
7485 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007487 {
7488 if (!mustfree)
7489 string = vim_strsave(string);
7490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 else
7492 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007493 if (mustfree)
7494 vim_free(string);
7495
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 /* next try expanding things like $VIM and ${HOME} */
7497 string = expand_env_save(name - 1);
7498 if (string != NULL && *string == '$')
7499 {
7500 vim_free(string);
7501 string = NULL;
7502 }
7503 }
7504 name[len] = cc;
7505 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007506 rettv->v_type = VAR_STRING;
7507 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 }
7509
7510 return OK;
7511}
7512
7513/*
7514 * Array with names and number of arguments of all internal functions
7515 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7516 */
7517static struct fst
7518{
7519 char *f_name; /* function name */
7520 char f_min_argc; /* minimal number of arguments */
7521 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007522 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007523 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524} functions[] =
7525{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007526#ifdef FEAT_FLOAT
7527 {"abs", 1, 1, f_abs},
7528#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007529 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 {"append", 2, 2, f_append},
7531 {"argc", 0, 0, f_argc},
7532 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007533 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007534#ifdef FEAT_FLOAT
7535 {"atan", 1, 1, f_atan},
7536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007538 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 {"bufexists", 1, 1, f_bufexists},
7540 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7541 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7542 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7543 {"buflisted", 1, 1, f_buflisted},
7544 {"bufloaded", 1, 1, f_bufloaded},
7545 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007546 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 {"bufwinnr", 1, 1, f_bufwinnr},
7548 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007549 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007550 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007551#ifdef FEAT_FLOAT
7552 {"ceil", 1, 1, f_ceil},
7553#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007554 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 {"char2nr", 1, 1, f_char2nr},
7556 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007557 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007559#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007560 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007561 {"complete_add", 1, 1, f_complete_add},
7562 {"complete_check", 0, 0, f_complete_check},
7563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007565 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007566#ifdef FEAT_FLOAT
7567 {"cos", 1, 1, f_cos},
7568#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007569 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007571 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007572 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 {"delete", 1, 1, f_delete},
7574 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007575 {"diff_filler", 1, 1, f_diff_filler},
7576 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007577 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007579 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 {"eventhandler", 0, 0, f_eventhandler},
7581 {"executable", 1, 1, f_executable},
7582 {"exists", 1, 1, f_exists},
7583 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007584 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007585 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7587 {"filereadable", 1, 1, f_filereadable},
7588 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007589 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007590 {"finddir", 1, 3, f_finddir},
7591 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007592#ifdef FEAT_FLOAT
7593 {"float2nr", 1, 1, f_float2nr},
7594 {"floor", 1, 1, f_floor},
7595#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007596 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 {"fnamemodify", 2, 2, f_fnamemodify},
7598 {"foldclosed", 1, 1, f_foldclosed},
7599 {"foldclosedend", 1, 1, f_foldclosedend},
7600 {"foldlevel", 1, 1, f_foldlevel},
7601 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007602 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007604 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007605 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007606 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007607 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 {"getbufvar", 2, 2, f_getbufvar},
7609 {"getchar", 0, 1, f_getchar},
7610 {"getcharmod", 0, 0, f_getcharmod},
7611 {"getcmdline", 0, 0, f_getcmdline},
7612 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007613 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007615 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007616 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 {"getfsize", 1, 1, f_getfsize},
7618 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007619 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007620 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007621 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007622 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007623 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007624 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007625 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007626 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007628 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 {"getwinposx", 0, 0, f_getwinposx},
7630 {"getwinposy", 0, 0, f_getwinposy},
7631 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007632 {"glob", 1, 2, f_glob},
7633 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007635 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007636 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007637 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7639 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7640 {"histadd", 2, 2, f_histadd},
7641 {"histdel", 1, 2, f_histdel},
7642 {"histget", 1, 2, f_histget},
7643 {"histnr", 1, 1, f_histnr},
7644 {"hlID", 1, 1, f_hlID},
7645 {"hlexists", 1, 1, f_hlexists},
7646 {"hostname", 0, 0, f_hostname},
7647 {"iconv", 3, 3, f_iconv},
7648 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007649 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007650 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007652 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 {"inputrestore", 0, 0, f_inputrestore},
7654 {"inputsave", 0, 0, f_inputsave},
7655 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007656 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007658 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007660 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007663 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 {"libcall", 3, 3, f_libcall},
7665 {"libcallnr", 3, 3, f_libcallnr},
7666 {"line", 1, 1, f_line},
7667 {"line2byte", 1, 1, f_line2byte},
7668 {"lispindent", 1, 1, f_lispindent},
7669 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007670#ifdef FEAT_FLOAT
7671 {"log10", 1, 1, f_log10},
7672#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007673 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007674 {"maparg", 1, 3, f_maparg},
7675 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007676 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007677 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007678 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007679 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007680 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007681 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007682 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007683 {"max", 1, 1, f_max},
7684 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007685#ifdef vim_mkdir
7686 {"mkdir", 1, 3, f_mkdir},
7687#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007688 {"mode", 0, 1, f_mode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 {"nextnonblank", 1, 1, f_nextnonblank},
7690 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007691 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007692#ifdef FEAT_FLOAT
7693 {"pow", 2, 2, f_pow},
7694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007696 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007697 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007698 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007699 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007700 {"reltime", 0, 2, f_reltime},
7701 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 {"remote_expr", 2, 3, f_remote_expr},
7703 {"remote_foreground", 1, 1, f_remote_foreground},
7704 {"remote_peek", 1, 2, f_remote_peek},
7705 {"remote_read", 1, 1, f_remote_read},
7706 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007707 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007709 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007711 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007712#ifdef FEAT_FLOAT
7713 {"round", 1, 1, f_round},
7714#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007715 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007716 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007717 {"searchpair", 3, 7, f_searchpair},
7718 {"searchpairpos", 3, 7, f_searchpairpos},
7719 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 {"server2client", 2, 2, f_server2client},
7721 {"serverlist", 0, 0, f_serverlist},
7722 {"setbufvar", 3, 3, f_setbufvar},
7723 {"setcmdpos", 1, 1, f_setcmdpos},
7724 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007725 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007726 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007727 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007728 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007730 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007732 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007734#ifdef FEAT_FLOAT
7735 {"sin", 1, 1, f_sin},
7736#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007737 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007738 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007739 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007740 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007741 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007742#ifdef FEAT_FLOAT
7743 {"sqrt", 1, 1, f_sqrt},
7744 {"str2float", 1, 1, f_str2float},
7745#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007746 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747#ifdef HAVE_STRFTIME
7748 {"strftime", 1, 2, f_strftime},
7749#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007750 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007751 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 {"strlen", 1, 1, f_strlen},
7753 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007754 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {"strtrans", 1, 1, f_strtrans},
7756 {"submatch", 1, 1, f_submatch},
7757 {"substitute", 4, 4, f_substitute},
7758 {"synID", 3, 3, f_synID},
7759 {"synIDattr", 2, 3, f_synIDattr},
7760 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007761 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007762 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007763 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007764 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007765 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007766 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007767 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007769 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 {"tolower", 1, 1, f_tolower},
7771 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007772 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007773#ifdef FEAT_FLOAT
7774 {"trunc", 1, 1, f_trunc},
7775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007777 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 {"virtcol", 1, 1, f_virtcol},
7779 {"visualmode", 0, 1, f_visualmode},
7780 {"winbufnr", 1, 1, f_winbufnr},
7781 {"wincol", 0, 0, f_wincol},
7782 {"winheight", 1, 1, f_winheight},
7783 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007784 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007786 {"winrestview", 1, 1, f_winrestview},
7787 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007789 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790};
7791
7792#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7793
7794/*
7795 * Function given to ExpandGeneric() to obtain the list of internal
7796 * or user defined function names.
7797 */
7798 char_u *
7799get_function_name(xp, idx)
7800 expand_T *xp;
7801 int idx;
7802{
7803 static int intidx = -1;
7804 char_u *name;
7805
7806 if (idx == 0)
7807 intidx = -1;
7808 if (intidx < 0)
7809 {
7810 name = get_user_func_name(xp, idx);
7811 if (name != NULL)
7812 return name;
7813 }
7814 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7815 {
7816 STRCPY(IObuff, functions[intidx].f_name);
7817 STRCAT(IObuff, "(");
7818 if (functions[intidx].f_max_argc == 0)
7819 STRCAT(IObuff, ")");
7820 return IObuff;
7821 }
7822
7823 return NULL;
7824}
7825
7826/*
7827 * Function given to ExpandGeneric() to obtain the list of internal or
7828 * user defined variable or function names.
7829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 char_u *
7831get_expr_name(xp, idx)
7832 expand_T *xp;
7833 int idx;
7834{
7835 static int intidx = -1;
7836 char_u *name;
7837
7838 if (idx == 0)
7839 intidx = -1;
7840 if (intidx < 0)
7841 {
7842 name = get_function_name(xp, idx);
7843 if (name != NULL)
7844 return name;
7845 }
7846 return get_user_var_name(xp, ++intidx);
7847}
7848
7849#endif /* FEAT_CMDL_COMPL */
7850
7851/*
7852 * Find internal function in table above.
7853 * Return index, or -1 if not found
7854 */
7855 static int
7856find_internal_func(name)
7857 char_u *name; /* name of the function */
7858{
7859 int first = 0;
7860 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7861 int cmp;
7862 int x;
7863
7864 /*
7865 * Find the function name in the table. Binary search.
7866 */
7867 while (first <= last)
7868 {
7869 x = first + ((unsigned)(last - first) >> 1);
7870 cmp = STRCMP(name, functions[x].f_name);
7871 if (cmp < 0)
7872 last = x - 1;
7873 else if (cmp > 0)
7874 first = x + 1;
7875 else
7876 return x;
7877 }
7878 return -1;
7879}
7880
7881/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007882 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7883 * name it contains, otherwise return "name".
7884 */
7885 static char_u *
7886deref_func_name(name, lenp)
7887 char_u *name;
7888 int *lenp;
7889{
Bram Moolenaar33570922005-01-25 22:26:29 +00007890 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007891 int cc;
7892
7893 cc = name[*lenp];
7894 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007895 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007896 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007897 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007898 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007899 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007900 {
7901 *lenp = 0;
7902 return (char_u *)""; /* just in case */
7903 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007904 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007905 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007906 }
7907
7908 return name;
7909}
7910
7911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 * Allocate a variable for the result of a function.
7913 * Return OK or FAIL.
7914 */
7915 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007916get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7917 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 char_u *name; /* name of the function */
7919 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 char_u **arg; /* argument, pointing to the '(' */
7922 linenr_T firstline; /* first line of range */
7923 linenr_T lastline; /* last line of range */
7924 int *doesrange; /* return: function handled range */
7925 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007926 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927{
7928 char_u *argp;
7929 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007930 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 int argcount = 0; /* number of arguments found */
7932
7933 /*
7934 * Get the arguments.
7935 */
7936 argp = *arg;
7937 while (argcount < MAX_FUNC_ARGS)
7938 {
7939 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7940 if (*argp == ')' || *argp == ',' || *argp == NUL)
7941 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7943 {
7944 ret = FAIL;
7945 break;
7946 }
7947 ++argcount;
7948 if (*argp != ',')
7949 break;
7950 }
7951 if (*argp == ')')
7952 ++argp;
7953 else
7954 ret = FAIL;
7955
7956 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007957 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007958 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007960 {
7961 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00007962 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007963 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00007964 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966
7967 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007968 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969
7970 *arg = skipwhite(argp);
7971 return ret;
7972}
7973
7974
7975/*
7976 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007977 * Return OK when the function can't be called, FAIL otherwise.
7978 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 */
7980 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007981call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007982 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 char_u *name; /* name of the function */
7984 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007985 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007987 typval_T *argvars; /* vars for arguments, must have "argcount"
7988 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 linenr_T firstline; /* first line of range */
7990 linenr_T lastline; /* last line of range */
7991 int *doesrange; /* return: function handled range */
7992 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007993 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994{
7995 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996#define ERROR_UNKNOWN 0
7997#define ERROR_TOOMANY 1
7998#define ERROR_TOOFEW 2
7999#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008000#define ERROR_DICT 4
8001#define ERROR_NONE 5
8002#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 int error = ERROR_NONE;
8004 int i;
8005 int llen;
8006 ufunc_T *fp;
8007 int cc;
8008#define FLEN_FIXED 40
8009 char_u fname_buf[FLEN_FIXED + 1];
8010 char_u *fname;
8011
8012 /*
8013 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8014 * Change <SNR>123_name() to K_SNR 123_name().
8015 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8016 */
8017 cc = name[len];
8018 name[len] = NUL;
8019 llen = eval_fname_script(name);
8020 if (llen > 0)
8021 {
8022 fname_buf[0] = K_SPECIAL;
8023 fname_buf[1] = KS_EXTRA;
8024 fname_buf[2] = (int)KE_SNR;
8025 i = 3;
8026 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8027 {
8028 if (current_SID <= 0)
8029 error = ERROR_SCRIPT;
8030 else
8031 {
8032 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8033 i = (int)STRLEN(fname_buf);
8034 }
8035 }
8036 if (i + STRLEN(name + llen) < FLEN_FIXED)
8037 {
8038 STRCPY(fname_buf + i, name + llen);
8039 fname = fname_buf;
8040 }
8041 else
8042 {
8043 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8044 if (fname == NULL)
8045 error = ERROR_OTHER;
8046 else
8047 {
8048 mch_memmove(fname, fname_buf, (size_t)i);
8049 STRCPY(fname + i, name + llen);
8050 }
8051 }
8052 }
8053 else
8054 fname = name;
8055
8056 *doesrange = FALSE;
8057
8058
8059 /* execute the function if no errors detected and executing */
8060 if (evaluate && error == ERROR_NONE)
8061 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008062 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8063 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 error = ERROR_UNKNOWN;
8065
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008066 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {
8068 /*
8069 * User defined function.
8070 */
8071 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008072
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008074 /* Trigger FuncUndefined event, may load the function. */
8075 if (fp == NULL
8076 && apply_autocmds(EVENT_FUNCUNDEFINED,
8077 fname, fname, TRUE, NULL)
8078 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008080 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 fp = find_func(fname);
8082 }
8083#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008084 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008085 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008086 {
8087 /* loaded a package, search for the function again */
8088 fp = find_func(fname);
8089 }
8090
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 if (fp != NULL)
8092 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008093 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008095 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008097 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008099 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008100 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 else
8102 {
8103 /*
8104 * Call the user function.
8105 * Save and restore search patterns, script variables and
8106 * redo buffer.
8107 */
8108 save_search_patterns();
8109 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008110 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008111 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008112 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008113 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8114 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8115 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008116 /* Function was unreferenced while being used, free it
8117 * now. */
8118 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 restoreRedobuff();
8120 restore_search_patterns();
8121 error = ERROR_NONE;
8122 }
8123 }
8124 }
8125 else
8126 {
8127 /*
8128 * Find the function name in the table, call its implementation.
8129 */
8130 i = find_internal_func(fname);
8131 if (i >= 0)
8132 {
8133 if (argcount < functions[i].f_min_argc)
8134 error = ERROR_TOOFEW;
8135 else if (argcount > functions[i].f_max_argc)
8136 error = ERROR_TOOMANY;
8137 else
8138 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008139 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008140 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 error = ERROR_NONE;
8142 }
8143 }
8144 }
8145 /*
8146 * The function call (or "FuncUndefined" autocommand sequence) might
8147 * have been aborted by an error, an interrupt, or an explicitly thrown
8148 * exception that has not been caught so far. This situation can be
8149 * tested for by calling aborting(). For an error in an internal
8150 * function or for the "E132" error in call_user_func(), however, the
8151 * throw point at which the "force_abort" flag (temporarily reset by
8152 * emsg()) is normally updated has not been reached yet. We need to
8153 * update that flag first to make aborting() reliable.
8154 */
8155 update_force_abort();
8156 }
8157 if (error == ERROR_NONE)
8158 ret = OK;
8159
8160 /*
8161 * Report an error unless the argument evaluation or function call has been
8162 * cancelled due to an aborting error, an interrupt, or an exception.
8163 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008164 if (!aborting())
8165 {
8166 switch (error)
8167 {
8168 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008169 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008170 break;
8171 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008172 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008173 break;
8174 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008175 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008176 name);
8177 break;
8178 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008179 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008180 name);
8181 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008182 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008183 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008184 name);
8185 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008186 }
8187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188
8189 name[len] = cc;
8190 if (fname != name && fname != fname_buf)
8191 vim_free(fname);
8192
8193 return ret;
8194}
8195
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008196/*
8197 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008198 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008199 */
8200 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008201emsg_funcname(ermsg, name)
8202 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008203 char_u *name;
8204{
8205 char_u *p;
8206
8207 if (*name == K_SPECIAL)
8208 p = concat_str((char_u *)"<SNR>", name + 3);
8209 else
8210 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008211 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008212 if (p != name)
8213 vim_free(p);
8214}
8215
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008216/*
8217 * Return TRUE for a non-zero Number and a non-empty String.
8218 */
8219 static int
8220non_zero_arg(argvars)
8221 typval_T *argvars;
8222{
8223 return ((argvars[0].v_type == VAR_NUMBER
8224 && argvars[0].vval.v_number != 0)
8225 || (argvars[0].v_type == VAR_STRING
8226 && argvars[0].vval.v_string != NULL
8227 && *argvars[0].vval.v_string != NUL));
8228}
8229
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230/*********************************************
8231 * Implementation of the built-in functions
8232 */
8233
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008234#ifdef FEAT_FLOAT
8235/*
8236 * "abs(expr)" function
8237 */
8238 static void
8239f_abs(argvars, rettv)
8240 typval_T *argvars;
8241 typval_T *rettv;
8242{
8243 if (argvars[0].v_type == VAR_FLOAT)
8244 {
8245 rettv->v_type = VAR_FLOAT;
8246 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8247 }
8248 else
8249 {
8250 varnumber_T n;
8251 int error = FALSE;
8252
8253 n = get_tv_number_chk(&argvars[0], &error);
8254 if (error)
8255 rettv->vval.v_number = -1;
8256 else if (n > 0)
8257 rettv->vval.v_number = n;
8258 else
8259 rettv->vval.v_number = -n;
8260 }
8261}
8262#endif
8263
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008265 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 */
8267 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008268f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008269 typval_T *argvars;
8270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271{
Bram Moolenaar33570922005-01-25 22:26:29 +00008272 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008274 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008275 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008277 if ((l = argvars[0].vval.v_list) != NULL
8278 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8279 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008280 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008281 }
8282 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008283 EMSG(_(e_listreq));
8284}
8285
8286/*
8287 * "append(lnum, string/list)" function
8288 */
8289 static void
8290f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008291 typval_T *argvars;
8292 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008293{
8294 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008295 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008296 list_T *l = NULL;
8297 listitem_T *li = NULL;
8298 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008299 long added = 0;
8300
Bram Moolenaar0d660222005-01-07 21:51:51 +00008301 lnum = get_tv_lnum(argvars);
8302 if (lnum >= 0
8303 && lnum <= curbuf->b_ml.ml_line_count
8304 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008305 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008306 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008307 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008308 l = argvars[1].vval.v_list;
8309 if (l == NULL)
8310 return;
8311 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008312 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008313 for (;;)
8314 {
8315 if (l == NULL)
8316 tv = &argvars[1]; /* append a string */
8317 else if (li == NULL)
8318 break; /* end of list */
8319 else
8320 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008321 line = get_tv_string_chk(tv);
8322 if (line == NULL) /* type error */
8323 {
8324 rettv->vval.v_number = 1; /* Failed */
8325 break;
8326 }
8327 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008328 ++added;
8329 if (l == NULL)
8330 break;
8331 li = li->li_next;
8332 }
8333
8334 appended_lines_mark(lnum, added);
8335 if (curwin->w_cursor.lnum > lnum)
8336 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008338 else
8339 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340}
8341
8342/*
8343 * "argc()" function
8344 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008346f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008347 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008350 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351}
8352
8353/*
8354 * "argidx()" function
8355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008357f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008358 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008361 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362}
8363
8364/*
8365 * "argv(nr)" function
8366 */
8367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008368f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008369 typval_T *argvars;
8370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371{
8372 int idx;
8373
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008374 if (argvars[0].v_type != VAR_UNKNOWN)
8375 {
8376 idx = get_tv_number_chk(&argvars[0], NULL);
8377 if (idx >= 0 && idx < ARGCOUNT)
8378 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8379 else
8380 rettv->vval.v_string = NULL;
8381 rettv->v_type = VAR_STRING;
8382 }
8383 else if (rettv_list_alloc(rettv) == OK)
8384 for (idx = 0; idx < ARGCOUNT; ++idx)
8385 list_append_string(rettv->vval.v_list,
8386 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387}
8388
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008389#ifdef FEAT_FLOAT
8390static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8391
8392/*
8393 * Get the float value of "argvars[0]" into "f".
8394 * Returns FAIL when the argument is not a Number or Float.
8395 */
8396 static int
8397get_float_arg(argvars, f)
8398 typval_T *argvars;
8399 float_T *f;
8400{
8401 if (argvars[0].v_type == VAR_FLOAT)
8402 {
8403 *f = argvars[0].vval.v_float;
8404 return OK;
8405 }
8406 if (argvars[0].v_type == VAR_NUMBER)
8407 {
8408 *f = (float_T)argvars[0].vval.v_number;
8409 return OK;
8410 }
8411 EMSG(_("E808: Number or Float required"));
8412 return FAIL;
8413}
8414
8415/*
8416 * "atan()" function
8417 */
8418 static void
8419f_atan(argvars, rettv)
8420 typval_T *argvars;
8421 typval_T *rettv;
8422{
8423 float_T f;
8424
8425 rettv->v_type = VAR_FLOAT;
8426 if (get_float_arg(argvars, &f) == OK)
8427 rettv->vval.v_float = atan(f);
8428 else
8429 rettv->vval.v_float = 0.0;
8430}
8431#endif
8432
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433/*
8434 * "browse(save, title, initdir, default)" function
8435 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008437f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008438 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440{
8441#ifdef FEAT_BROWSE
8442 int save;
8443 char_u *title;
8444 char_u *initdir;
8445 char_u *defname;
8446 char_u buf[NUMBUFLEN];
8447 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008448 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008450 save = get_tv_number_chk(&argvars[0], &error);
8451 title = get_tv_string_chk(&argvars[1]);
8452 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8453 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008455 if (error || title == NULL || initdir == NULL || defname == NULL)
8456 rettv->vval.v_string = NULL;
8457 else
8458 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008459 do_browse(save ? BROWSE_SAVE : 0,
8460 title, defname, NULL, initdir, NULL, curbuf);
8461#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008462 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008463#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008464 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008465}
8466
8467/*
8468 * "browsedir(title, initdir)" function
8469 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008471f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008472 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008473 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008474{
8475#ifdef FEAT_BROWSE
8476 char_u *title;
8477 char_u *initdir;
8478 char_u buf[NUMBUFLEN];
8479
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008480 title = get_tv_string_chk(&argvars[0]);
8481 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008482
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008483 if (title == NULL || initdir == NULL)
8484 rettv->vval.v_string = NULL;
8485 else
8486 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008487 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008489 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008491 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492}
8493
Bram Moolenaar33570922005-01-25 22:26:29 +00008494static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008495
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496/*
8497 * Find a buffer by number or exact name.
8498 */
8499 static buf_T *
8500find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008501 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502{
8503 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008505 if (avar->v_type == VAR_NUMBER)
8506 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008507 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008509 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008510 if (buf == NULL)
8511 {
8512 /* No full path name match, try a match with a URL or a "nofile"
8513 * buffer, these don't use the full path. */
8514 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8515 if (buf->b_fname != NULL
8516 && (path_with_url(buf->b_fname)
8517#ifdef FEAT_QUICKFIX
8518 || bt_nofile(buf)
8519#endif
8520 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008521 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008522 break;
8523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 }
8525 return buf;
8526}
8527
8528/*
8529 * "bufexists(expr)" function
8530 */
8531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008532f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008533 typval_T *argvars;
8534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008536 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537}
8538
8539/*
8540 * "buflisted(expr)" function
8541 */
8542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008543f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008544 typval_T *argvars;
8545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546{
8547 buf_T *buf;
8548
8549 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008550 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551}
8552
8553/*
8554 * "bufloaded(expr)" function
8555 */
8556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008557f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008558 typval_T *argvars;
8559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560{
8561 buf_T *buf;
8562
8563 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008564 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565}
8566
Bram Moolenaar33570922005-01-25 22:26:29 +00008567static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008568
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569/*
8570 * Get buffer by number or pattern.
8571 */
8572 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008573get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008574 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008576 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 int save_magic;
8578 char_u *save_cpo;
8579 buf_T *buf;
8580
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008581 if (tv->v_type == VAR_NUMBER)
8582 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008583 if (tv->v_type != VAR_STRING)
8584 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 if (name == NULL || *name == NUL)
8586 return curbuf;
8587 if (name[0] == '$' && name[1] == NUL)
8588 return lastbuf;
8589
8590 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8591 save_magic = p_magic;
8592 p_magic = TRUE;
8593 save_cpo = p_cpo;
8594 p_cpo = (char_u *)"";
8595
8596 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8597 TRUE, FALSE));
8598
8599 p_magic = save_magic;
8600 p_cpo = save_cpo;
8601
8602 /* If not found, try expanding the name, like done for bufexists(). */
8603 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008604 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605
8606 return buf;
8607}
8608
8609/*
8610 * "bufname(expr)" function
8611 */
8612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008613f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008614 typval_T *argvars;
8615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
8617 buf_T *buf;
8618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008619 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008621 buf = get_buf_tv(&argvars[0]);
8622 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008624 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008626 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 --emsg_off;
8628}
8629
8630/*
8631 * "bufnr(expr)" function
8632 */
8633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008634f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008635 typval_T *argvars;
8636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637{
8638 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008639 int error = FALSE;
8640 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008642 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008644 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008645 --emsg_off;
8646
8647 /* If the buffer isn't found and the second argument is not zero create a
8648 * new buffer. */
8649 if (buf == NULL
8650 && argvars[1].v_type != VAR_UNKNOWN
8651 && get_tv_number_chk(&argvars[1], &error) != 0
8652 && !error
8653 && (name = get_tv_string_chk(&argvars[0])) != NULL
8654 && !error)
8655 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8656
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008658 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008660 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661}
8662
8663/*
8664 * "bufwinnr(nr)" function
8665 */
8666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008667f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008668 typval_T *argvars;
8669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670{
8671#ifdef FEAT_WINDOWS
8672 win_T *wp;
8673 int winnr = 0;
8674#endif
8675 buf_T *buf;
8676
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008677 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008679 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680#ifdef FEAT_WINDOWS
8681 for (wp = firstwin; wp; wp = wp->w_next)
8682 {
8683 ++winnr;
8684 if (wp->w_buffer == buf)
8685 break;
8686 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008687 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008689 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690#endif
8691 --emsg_off;
8692}
8693
8694/*
8695 * "byte2line(byte)" function
8696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008698f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008699 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701{
8702#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008703 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704#else
8705 long boff = 0;
8706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008707 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008709 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008711 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 (linenr_T)0, &boff);
8713#endif
8714}
8715
8716/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008717 * "byteidx()" function
8718 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008720f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008721 typval_T *argvars;
8722 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008723{
8724#ifdef FEAT_MBYTE
8725 char_u *t;
8726#endif
8727 char_u *str;
8728 long idx;
8729
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008730 str = get_tv_string_chk(&argvars[0]);
8731 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008732 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008733 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008734 return;
8735
8736#ifdef FEAT_MBYTE
8737 t = str;
8738 for ( ; idx > 0; idx--)
8739 {
8740 if (*t == NUL) /* EOL reached */
8741 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008742 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008743 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008744 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008745#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008746 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008747 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008748#endif
8749}
8750
8751/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008752 * "call(func, arglist)" function
8753 */
8754 static void
8755f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008756 typval_T *argvars;
8757 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008758{
8759 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008760 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008761 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008762 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008763 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008764 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008765
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008766 if (argvars[1].v_type != VAR_LIST)
8767 {
8768 EMSG(_(e_listreq));
8769 return;
8770 }
8771 if (argvars[1].vval.v_list == NULL)
8772 return;
8773
8774 if (argvars[0].v_type == VAR_FUNC)
8775 func = argvars[0].vval.v_string;
8776 else
8777 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008778 if (*func == NUL)
8779 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008780
Bram Moolenaare9a41262005-01-15 22:18:47 +00008781 if (argvars[2].v_type != VAR_UNKNOWN)
8782 {
8783 if (argvars[2].v_type != VAR_DICT)
8784 {
8785 EMSG(_(e_dictreq));
8786 return;
8787 }
8788 selfdict = argvars[2].vval.v_dict;
8789 }
8790
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008791 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8792 item = item->li_next)
8793 {
8794 if (argc == MAX_FUNC_ARGS)
8795 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008796 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008797 break;
8798 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008799 /* Make a copy of each argument. This is needed to be able to set
8800 * v_lock to VAR_FIXED in the copy without changing the original list.
8801 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008802 copy_tv(&item->li_tv, &argv[argc++]);
8803 }
8804
8805 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008806 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008807 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8808 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008809
8810 /* Free the arguments. */
8811 while (argc > 0)
8812 clear_tv(&argv[--argc]);
8813}
8814
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008815#ifdef FEAT_FLOAT
8816/*
8817 * "ceil({float})" function
8818 */
8819 static void
8820f_ceil(argvars, rettv)
8821 typval_T *argvars;
8822 typval_T *rettv;
8823{
8824 float_T f;
8825
8826 rettv->v_type = VAR_FLOAT;
8827 if (get_float_arg(argvars, &f) == OK)
8828 rettv->vval.v_float = ceil(f);
8829 else
8830 rettv->vval.v_float = 0.0;
8831}
8832#endif
8833
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008834/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008835 * "changenr()" function
8836 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008837 static void
8838f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008839 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008840 typval_T *rettv;
8841{
8842 rettv->vval.v_number = curbuf->b_u_seq_cur;
8843}
8844
8845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 * "char2nr(string)" function
8847 */
8848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008849f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008850 typval_T *argvars;
8851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852{
8853#ifdef FEAT_MBYTE
8854 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008855 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 else
8857#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008858 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859}
8860
8861/*
8862 * "cindent(lnum)" function
8863 */
8864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008865f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008866 typval_T *argvars;
8867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868{
8869#ifdef FEAT_CINDENT
8870 pos_T pos;
8871 linenr_T lnum;
8872
8873 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008874 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8876 {
8877 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008878 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 curwin->w_cursor = pos;
8880 }
8881 else
8882#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008883 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884}
8885
8886/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008887 * "clearmatches()" function
8888 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008889 static void
8890f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008891 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008892 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008893{
8894#ifdef FEAT_SEARCH_EXTRA
8895 clear_matches(curwin);
8896#endif
8897}
8898
8899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 * "col(string)" function
8901 */
8902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008903f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008904 typval_T *argvars;
8905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906{
8907 colnr_T col = 0;
8908 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008909 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008911 fp = var2fpos(&argvars[0], FALSE, &fnum);
8912 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 {
8914 if (fp->col == MAXCOL)
8915 {
8916 /* '> can be MAXCOL, get the length of the line then */
8917 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008918 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 else
8920 col = MAXCOL;
8921 }
8922 else
8923 {
8924 col = fp->col + 1;
8925#ifdef FEAT_VIRTUALEDIT
8926 /* col(".") when the cursor is on the NUL at the end of the line
8927 * because of "coladd" can be seen as an extra column. */
8928 if (virtual_active() && fp == &curwin->w_cursor)
8929 {
8930 char_u *p = ml_get_cursor();
8931
8932 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8933 curwin->w_virtcol - curwin->w_cursor.coladd))
8934 {
8935# ifdef FEAT_MBYTE
8936 int l;
8937
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008938 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 col += l;
8940# else
8941 if (*p != NUL && p[1] == NUL)
8942 ++col;
8943# endif
8944 }
8945 }
8946#endif
8947 }
8948 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950}
8951
Bram Moolenaar572cb562005-08-05 21:35:02 +00008952#if defined(FEAT_INS_EXPAND)
8953/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008954 * "complete()" function
8955 */
Bram Moolenaarade00832006-03-10 21:46:58 +00008956 static void
8957f_complete(argvars, rettv)
8958 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008959 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00008960{
8961 int startcol;
8962
8963 if ((State & INSERT) == 0)
8964 {
8965 EMSG(_("E785: complete() can only be used in Insert mode"));
8966 return;
8967 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008968
8969 /* Check for undo allowed here, because if something was already inserted
8970 * the line was already saved for undo and this check isn't done. */
8971 if (!undo_allowed())
8972 return;
8973
Bram Moolenaarade00832006-03-10 21:46:58 +00008974 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8975 {
8976 EMSG(_(e_invarg));
8977 return;
8978 }
8979
8980 startcol = get_tv_number_chk(&argvars[0], NULL);
8981 if (startcol <= 0)
8982 return;
8983
8984 set_completion(startcol - 1, argvars[1].vval.v_list);
8985}
8986
8987/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008988 * "complete_add()" function
8989 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00008990 static void
8991f_complete_add(argvars, rettv)
8992 typval_T *argvars;
8993 typval_T *rettv;
8994{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008995 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008996}
8997
8998/*
8999 * "complete_check()" function
9000 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009001 static void
9002f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009003 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009004 typval_T *rettv;
9005{
9006 int saved = RedrawingDisabled;
9007
9008 RedrawingDisabled = 0;
9009 ins_compl_check_keys(0);
9010 rettv->vval.v_number = compl_interrupted;
9011 RedrawingDisabled = saved;
9012}
9013#endif
9014
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015/*
9016 * "confirm(message, buttons[, default [, type]])" function
9017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009019f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009020 typval_T *argvars UNUSED;
9021 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022{
9023#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9024 char_u *message;
9025 char_u *buttons = NULL;
9026 char_u buf[NUMBUFLEN];
9027 char_u buf2[NUMBUFLEN];
9028 int def = 1;
9029 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009030 char_u *typestr;
9031 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009033 message = get_tv_string_chk(&argvars[0]);
9034 if (message == NULL)
9035 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009036 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009038 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9039 if (buttons == NULL)
9040 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009041 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009043 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009044 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009046 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9047 if (typestr == NULL)
9048 error = TRUE;
9049 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009051 switch (TOUPPER_ASC(*typestr))
9052 {
9053 case 'E': type = VIM_ERROR; break;
9054 case 'Q': type = VIM_QUESTION; break;
9055 case 'I': type = VIM_INFO; break;
9056 case 'W': type = VIM_WARNING; break;
9057 case 'G': type = VIM_GENERIC; break;
9058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 }
9060 }
9061 }
9062 }
9063
9064 if (buttons == NULL || *buttons == NUL)
9065 buttons = (char_u *)_("&Ok");
9066
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009067 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009068 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070#endif
9071}
9072
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009073/*
9074 * "copy()" function
9075 */
9076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009078 typval_T *argvars;
9079 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009080{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009081 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009082}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009084#ifdef FEAT_FLOAT
9085/*
9086 * "cos()" function
9087 */
9088 static void
9089f_cos(argvars, rettv)
9090 typval_T *argvars;
9091 typval_T *rettv;
9092{
9093 float_T f;
9094
9095 rettv->v_type = VAR_FLOAT;
9096 if (get_float_arg(argvars, &f) == OK)
9097 rettv->vval.v_float = cos(f);
9098 else
9099 rettv->vval.v_float = 0.0;
9100}
9101#endif
9102
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009104 * "count()" function
9105 */
9106 static void
9107f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009108 typval_T *argvars;
9109 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009110{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009111 long n = 0;
9112 int ic = FALSE;
9113
Bram Moolenaare9a41262005-01-15 22:18:47 +00009114 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009115 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009116 listitem_T *li;
9117 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009118 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009119
Bram Moolenaare9a41262005-01-15 22:18:47 +00009120 if ((l = argvars[0].vval.v_list) != NULL)
9121 {
9122 li = l->lv_first;
9123 if (argvars[2].v_type != VAR_UNKNOWN)
9124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009125 int error = FALSE;
9126
9127 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009128 if (argvars[3].v_type != VAR_UNKNOWN)
9129 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009130 idx = get_tv_number_chk(&argvars[3], &error);
9131 if (!error)
9132 {
9133 li = list_find(l, idx);
9134 if (li == NULL)
9135 EMSGN(_(e_listidx), idx);
9136 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009137 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009138 if (error)
9139 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009140 }
9141
9142 for ( ; li != NULL; li = li->li_next)
9143 if (tv_equal(&li->li_tv, &argvars[1], ic))
9144 ++n;
9145 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009146 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009147 else if (argvars[0].v_type == VAR_DICT)
9148 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009149 int todo;
9150 dict_T *d;
9151 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009152
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009153 if ((d = argvars[0].vval.v_dict) != NULL)
9154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009155 int error = FALSE;
9156
Bram Moolenaare9a41262005-01-15 22:18:47 +00009157 if (argvars[2].v_type != VAR_UNKNOWN)
9158 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009159 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009160 if (argvars[3].v_type != VAR_UNKNOWN)
9161 EMSG(_(e_invarg));
9162 }
9163
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009164 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009165 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009166 {
9167 if (!HASHITEM_EMPTY(hi))
9168 {
9169 --todo;
9170 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9171 ++n;
9172 }
9173 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009174 }
9175 }
9176 else
9177 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009178 rettv->vval.v_number = n;
9179}
9180
9181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9183 *
9184 * Checks the existence of a cscope connection.
9185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009187f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009188 typval_T *argvars UNUSED;
9189 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190{
9191#ifdef FEAT_CSCOPE
9192 int num = 0;
9193 char_u *dbpath = NULL;
9194 char_u *prepend = NULL;
9195 char_u buf[NUMBUFLEN];
9196
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009197 if (argvars[0].v_type != VAR_UNKNOWN
9198 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009200 num = (int)get_tv_number(&argvars[0]);
9201 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009202 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009203 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 }
9205
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009206 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207#endif
9208}
9209
9210/*
9211 * "cursor(lnum, col)" function
9212 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009213 * Moves the cursor to the specified line and column.
9214 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009217f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009218 typval_T *argvars;
9219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220{
9221 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009222#ifdef FEAT_VIRTUALEDIT
9223 long coladd = 0;
9224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009226 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009227 if (argvars[1].v_type == VAR_UNKNOWN)
9228 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009229 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009230
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009231 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009232 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009233 line = pos.lnum;
9234 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009235#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009236 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009237#endif
9238 }
9239 else
9240 {
9241 line = get_tv_lnum(argvars);
9242 col = get_tv_number_chk(&argvars[1], NULL);
9243#ifdef FEAT_VIRTUALEDIT
9244 if (argvars[2].v_type != VAR_UNKNOWN)
9245 coladd = get_tv_number_chk(&argvars[2], NULL);
9246#endif
9247 }
9248 if (line < 0 || col < 0
9249#ifdef FEAT_VIRTUALEDIT
9250 || coladd < 0
9251#endif
9252 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009253 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 if (line > 0)
9255 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 if (col > 0)
9257 curwin->w_cursor.col = col - 1;
9258#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009259 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260#endif
9261
9262 /* Make sure the cursor is in a valid position. */
9263 check_cursor();
9264#ifdef FEAT_MBYTE
9265 /* Correct cursor for multi-byte character. */
9266 if (has_mbyte)
9267 mb_adjust_cursor();
9268#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009269
9270 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009271 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272}
9273
9274/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009275 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 */
9277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009278f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009279 typval_T *argvars;
9280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009282 int noref = 0;
9283
9284 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009285 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009286 if (noref < 0 || noref > 1)
9287 EMSG(_(e_invarg));
9288 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00009289 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290}
9291
9292/*
9293 * "delete()" function
9294 */
9295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009296f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009297 typval_T *argvars;
9298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299{
9300 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009301 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304}
9305
9306/*
9307 * "did_filetype()" function
9308 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009310f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009311 typval_T *argvars UNUSED;
9312 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313{
9314#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009315 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316#endif
9317}
9318
9319/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009320 * "diff_filler()" function
9321 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009323f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009324 typval_T *argvars UNUSED;
9325 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009326{
9327#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009328 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009329#endif
9330}
9331
9332/*
9333 * "diff_hlID()" function
9334 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009336f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009337 typval_T *argvars UNUSED;
9338 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009339{
9340#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009341 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009342 static linenr_T prev_lnum = 0;
9343 static int changedtick = 0;
9344 static int fnum = 0;
9345 static int change_start = 0;
9346 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009347 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009348 int filler_lines;
9349 int col;
9350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009351 if (lnum < 0) /* ignore type error in {lnum} arg */
9352 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009353 if (lnum != prev_lnum
9354 || changedtick != curbuf->b_changedtick
9355 || fnum != curbuf->b_fnum)
9356 {
9357 /* New line, buffer, change: need to get the values. */
9358 filler_lines = diff_check(curwin, lnum);
9359 if (filler_lines < 0)
9360 {
9361 if (filler_lines == -1)
9362 {
9363 change_start = MAXCOL;
9364 change_end = -1;
9365 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9366 hlID = HLF_ADD; /* added line */
9367 else
9368 hlID = HLF_CHD; /* changed line */
9369 }
9370 else
9371 hlID = HLF_ADD; /* added line */
9372 }
9373 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009374 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009375 prev_lnum = lnum;
9376 changedtick = curbuf->b_changedtick;
9377 fnum = curbuf->b_fnum;
9378 }
9379
9380 if (hlID == HLF_CHD || hlID == HLF_TXD)
9381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009382 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009383 if (col >= change_start && col <= change_end)
9384 hlID = HLF_TXD; /* changed text */
9385 else
9386 hlID = HLF_CHD; /* changed line */
9387 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009388 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009389#endif
9390}
9391
9392/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009393 * "empty({expr})" function
9394 */
9395 static void
9396f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009397 typval_T *argvars;
9398 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009399{
9400 int n;
9401
9402 switch (argvars[0].v_type)
9403 {
9404 case VAR_STRING:
9405 case VAR_FUNC:
9406 n = argvars[0].vval.v_string == NULL
9407 || *argvars[0].vval.v_string == NUL;
9408 break;
9409 case VAR_NUMBER:
9410 n = argvars[0].vval.v_number == 0;
9411 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009412#ifdef FEAT_FLOAT
9413 case VAR_FLOAT:
9414 n = argvars[0].vval.v_float == 0.0;
9415 break;
9416#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009417 case VAR_LIST:
9418 n = argvars[0].vval.v_list == NULL
9419 || argvars[0].vval.v_list->lv_first == NULL;
9420 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009421 case VAR_DICT:
9422 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009423 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009424 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009425 default:
9426 EMSG2(_(e_intern2), "f_empty()");
9427 n = 0;
9428 }
9429
9430 rettv->vval.v_number = n;
9431}
9432
9433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 * "escape({string}, {chars})" function
9435 */
9436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009437f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009438 typval_T *argvars;
9439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440{
9441 char_u buf[NUMBUFLEN];
9442
Bram Moolenaar758711c2005-02-02 23:11:38 +00009443 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9444 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446}
9447
9448/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009449 * "eval()" function
9450 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009451 static void
9452f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009453 typval_T *argvars;
9454 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009455{
9456 char_u *s;
9457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009458 s = get_tv_string_chk(&argvars[0]);
9459 if (s != NULL)
9460 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009462 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9463 {
9464 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009465 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009466 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009467 else if (*s != NUL)
9468 EMSG(_(e_trailing));
9469}
9470
9471/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 * "eventhandler()" function
9473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009475f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009476 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009479 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480}
9481
9482/*
9483 * "executable()" function
9484 */
9485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009486f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009487 typval_T *argvars;
9488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491}
9492
9493/*
9494 * "exists()" function
9495 */
9496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009498 typval_T *argvars;
9499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500{
9501 char_u *p;
9502 char_u *name;
9503 int n = FALSE;
9504 int len = 0;
9505
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 if (*p == '$') /* environment variable */
9508 {
9509 /* first try "normal" environment variables (fast) */
9510 if (mch_getenv(p + 1) != NULL)
9511 n = TRUE;
9512 else
9513 {
9514 /* try expanding things like $VIM and ${HOME} */
9515 p = expand_env_save(p);
9516 if (p != NULL && *p != '$')
9517 n = TRUE;
9518 vim_free(p);
9519 }
9520 }
9521 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009522 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009523 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009524 if (*skipwhite(p) != NUL)
9525 n = FALSE; /* trailing garbage */
9526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 else if (*p == '*') /* internal or user defined function */
9528 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009529 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 }
9531 else if (*p == ':')
9532 {
9533 n = cmd_exists(p + 1);
9534 }
9535 else if (*p == '#')
9536 {
9537#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009538 if (p[1] == '#')
9539 n = autocmd_supported(p + 2);
9540 else
9541 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542#endif
9543 }
9544 else /* internal variable */
9545 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009546 char_u *tofree;
9547 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009549 /* get_name_len() takes care of expanding curly braces */
9550 name = p;
9551 len = get_name_len(&p, &tofree, TRUE, FALSE);
9552 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009554 if (tofree != NULL)
9555 name = tofree;
9556 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9557 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009559 /* handle d.key, l[idx], f(expr) */
9560 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9561 if (n)
9562 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 }
9564 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009565 if (*p != NUL)
9566 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009568 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 }
9570
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009571 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572}
9573
9574/*
9575 * "expand()" function
9576 */
9577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009578f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009579 typval_T *argvars;
9580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581{
9582 char_u *s;
9583 int len;
9584 char_u *errormsg;
9585 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9586 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009587 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009589 rettv->v_type = VAR_STRING;
9590 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 if (*s == '%' || *s == '#' || *s == '<')
9592 {
9593 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009594 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 --emsg_off;
9596 }
9597 else
9598 {
9599 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009600 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009601 if (argvars[1].v_type != VAR_UNKNOWN
9602 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009604 if (!error)
9605 {
9606 ExpandInit(&xpc);
9607 xpc.xp_context = EXPAND_FILES;
9608 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009609 }
9610 else
9611 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 }
9613}
9614
9615/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009616 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009617 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009618 */
9619 static void
9620f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009621 typval_T *argvars;
9622 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009623{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009624 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009625 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009626 list_T *l1, *l2;
9627 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009628 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009629 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009630
Bram Moolenaare9a41262005-01-15 22:18:47 +00009631 l1 = argvars[0].vval.v_list;
9632 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009633 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9634 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009635 {
9636 if (argvars[2].v_type != VAR_UNKNOWN)
9637 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009638 before = get_tv_number_chk(&argvars[2], &error);
9639 if (error)
9640 return; /* type error; errmsg already given */
9641
Bram Moolenaar758711c2005-02-02 23:11:38 +00009642 if (before == l1->lv_len)
9643 item = NULL;
9644 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009645 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009646 item = list_find(l1, before);
9647 if (item == NULL)
9648 {
9649 EMSGN(_(e_listidx), before);
9650 return;
9651 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009652 }
9653 }
9654 else
9655 item = NULL;
9656 list_extend(l1, l2, item);
9657
Bram Moolenaare9a41262005-01-15 22:18:47 +00009658 copy_tv(&argvars[0], rettv);
9659 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009660 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009661 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9662 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009663 dict_T *d1, *d2;
9664 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009665 char_u *action;
9666 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009667 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009668 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009669
9670 d1 = argvars[0].vval.v_dict;
9671 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009672 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9673 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009674 {
9675 /* Check the third argument. */
9676 if (argvars[2].v_type != VAR_UNKNOWN)
9677 {
9678 static char *(av[]) = {"keep", "force", "error"};
9679
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009680 action = get_tv_string_chk(&argvars[2]);
9681 if (action == NULL)
9682 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009683 for (i = 0; i < 3; ++i)
9684 if (STRCMP(action, av[i]) == 0)
9685 break;
9686 if (i == 3)
9687 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009688 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009689 return;
9690 }
9691 }
9692 else
9693 action = (char_u *)"force";
9694
9695 /* Go over all entries in the second dict and add them to the
9696 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009697 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009698 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009699 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009700 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009701 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009702 --todo;
9703 di1 = dict_find(d1, hi2->hi_key, -1);
9704 if (di1 == NULL)
9705 {
9706 di1 = dictitem_copy(HI2DI(hi2));
9707 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9708 dictitem_free(di1);
9709 }
9710 else if (*action == 'e')
9711 {
9712 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9713 break;
9714 }
9715 else if (*action == 'f')
9716 {
9717 clear_tv(&di1->di_tv);
9718 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9719 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009720 }
9721 }
9722
Bram Moolenaare9a41262005-01-15 22:18:47 +00009723 copy_tv(&argvars[0], rettv);
9724 }
9725 }
9726 else
9727 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009728}
9729
9730/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009731 * "feedkeys()" function
9732 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009733 static void
9734f_feedkeys(argvars, rettv)
9735 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009736 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009737{
9738 int remap = TRUE;
9739 char_u *keys, *flags;
9740 char_u nbuf[NUMBUFLEN];
9741 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009742 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009743
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009744 /* This is not allowed in the sandbox. If the commands would still be
9745 * executed in the sandbox it would be OK, but it probably happens later,
9746 * when "sandbox" is no longer set. */
9747 if (check_secure())
9748 return;
9749
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009750 keys = get_tv_string(&argvars[0]);
9751 if (*keys != NUL)
9752 {
9753 if (argvars[1].v_type != VAR_UNKNOWN)
9754 {
9755 flags = get_tv_string_buf(&argvars[1], nbuf);
9756 for ( ; *flags != NUL; ++flags)
9757 {
9758 switch (*flags)
9759 {
9760 case 'n': remap = FALSE; break;
9761 case 'm': remap = TRUE; break;
9762 case 't': typed = TRUE; break;
9763 }
9764 }
9765 }
9766
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009767 /* Need to escape K_SPECIAL and CSI before putting the string in the
9768 * typeahead buffer. */
9769 keys_esc = vim_strsave_escape_csi(keys);
9770 if (keys_esc != NULL)
9771 {
9772 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009773 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009774 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009775 if (vgetc_busy)
9776 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009777 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009778 }
9779}
9780
9781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 * "filereadable()" function
9783 */
9784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009785f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009786 typval_T *argvars;
9787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788{
Bram Moolenaarc236c162008-07-13 17:41:49 +00009789 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 char_u *p;
9791 int n;
9792
Bram Moolenaarc236c162008-07-13 17:41:49 +00009793#ifndef O_NONBLOCK
9794# define O_NONBLOCK 0
9795#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009796 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +00009797 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
9798 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 {
9800 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009801 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 }
9803 else
9804 n = FALSE;
9805
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009806 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807}
9808
9809/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009810 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 * rights to write into.
9812 */
9813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009814f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009815 typval_T *argvars;
9816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009818 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819}
9820
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009821static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009822
9823 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009824findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009825 typval_T *argvars;
9826 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009827 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009828{
9829#ifdef FEAT_SEARCHPATH
9830 char_u *fname;
9831 char_u *fresult = NULL;
9832 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9833 char_u *p;
9834 char_u pathbuf[NUMBUFLEN];
9835 int count = 1;
9836 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009837 int error = FALSE;
9838#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009839
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009840 rettv->vval.v_string = NULL;
9841 rettv->v_type = VAR_STRING;
9842
9843#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009844 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009845
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009846 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009847 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009848 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9849 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009850 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009851 else
9852 {
9853 if (*p != NUL)
9854 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009856 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009857 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009858 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009859 }
9860
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009861 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9862 error = TRUE;
9863
9864 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009866 do
9867 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009868 if (rettv->v_type == VAR_STRING)
9869 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009870 fresult = find_file_in_path_option(first ? fname : NULL,
9871 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009872 0, first, path,
9873 find_what,
9874 curbuf->b_ffname,
9875 find_what == FINDFILE_DIR
9876 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009877 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009878
9879 if (fresult != NULL && rettv->v_type == VAR_LIST)
9880 list_append_string(rettv->vval.v_list, fresult, -1);
9881
9882 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009883 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009884
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009885 if (rettv->v_type == VAR_STRING)
9886 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009887#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009888}
9889
Bram Moolenaar33570922005-01-25 22:26:29 +00009890static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9891static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009892
9893/*
9894 * Implementation of map() and filter().
9895 */
9896 static void
9897filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009898 typval_T *argvars;
9899 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009900 int map;
9901{
9902 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009903 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009904 listitem_T *li, *nli;
9905 list_T *l = NULL;
9906 dictitem_T *di;
9907 hashtab_T *ht;
9908 hashitem_T *hi;
9909 dict_T *d = NULL;
9910 typval_T save_val;
9911 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009912 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009913 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009914 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009915 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009916
Bram Moolenaare9a41262005-01-15 22:18:47 +00009917 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009918 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009919 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009920 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009921 return;
9922 }
9923 else if (argvars[0].v_type == VAR_DICT)
9924 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009925 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009926 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009927 return;
9928 }
9929 else
9930 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009931 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009932 return;
9933 }
9934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009935 expr = get_tv_string_buf_chk(&argvars[1], buf);
9936 /* On type errors, the preceding call has already displayed an error
9937 * message. Avoid a misleading error message for an empty string that
9938 * was not passed as argument. */
9939 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009940 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009941 prepare_vimvar(VV_VAL, &save_val);
9942 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009943
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009944 /* We reset "did_emsg" to be able to detect whether an error
9945 * occurred during evaluation of the expression. */
9946 save_did_emsg = did_emsg;
9947 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009949 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009950 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009951 prepare_vimvar(VV_KEY, &save_key);
9952 vimvars[VV_KEY].vv_type = VAR_STRING;
9953
9954 ht = &d->dv_hashtab;
9955 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009956 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009957 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009959 if (!HASHITEM_EMPTY(hi))
9960 {
9961 --todo;
9962 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009963 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009964 break;
9965 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009966 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009967 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009968 break;
9969 if (!map && rem)
9970 dictitem_remove(d, di);
9971 clear_tv(&vimvars[VV_KEY].vv_tv);
9972 }
9973 }
9974 hash_unlock(ht);
9975
9976 restore_vimvar(VV_KEY, &save_key);
9977 }
9978 else
9979 {
9980 for (li = l->lv_first; li != NULL; li = nli)
9981 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009982 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009983 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009984 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009985 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009986 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009987 break;
9988 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009989 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009990 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009991 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009992
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009993 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009994
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009995 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009996 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009997
9998 copy_tv(&argvars[0], rettv);
9999}
10000
10001 static int
10002filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010003 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010004 char_u *expr;
10005 int map;
10006 int *remp;
10007{
Bram Moolenaar33570922005-01-25 22:26:29 +000010008 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010009 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010010 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010011
Bram Moolenaar33570922005-01-25 22:26:29 +000010012 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010013 s = expr;
10014 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010015 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010016 if (*s != NUL) /* check for trailing chars after expr */
10017 {
10018 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010019 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010020 }
10021 if (map)
10022 {
10023 /* map(): replace the list item value */
10024 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010025 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010026 *tv = rettv;
10027 }
10028 else
10029 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010030 int error = FALSE;
10031
Bram Moolenaare9a41262005-01-15 22:18:47 +000010032 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010033 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010034 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010035 /* On type error, nothing has been removed; return FAIL to stop the
10036 * loop. The error message was given by get_tv_number_chk(). */
10037 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010038 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010039 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010040 retval = OK;
10041theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010042 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010043 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010044}
10045
10046/*
10047 * "filter()" function
10048 */
10049 static void
10050f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010051 typval_T *argvars;
10052 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010053{
10054 filter_map(argvars, rettv, FALSE);
10055}
10056
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010057/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010058 * "finddir({fname}[, {path}[, {count}]])" function
10059 */
10060 static void
10061f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010062 typval_T *argvars;
10063 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010064{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010065 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010066}
10067
10068/*
10069 * "findfile({fname}[, {path}[, {count}]])" function
10070 */
10071 static void
10072f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010073 typval_T *argvars;
10074 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010075{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010076 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010077}
10078
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010079#ifdef FEAT_FLOAT
10080/*
10081 * "float2nr({float})" function
10082 */
10083 static void
10084f_float2nr(argvars, rettv)
10085 typval_T *argvars;
10086 typval_T *rettv;
10087{
10088 float_T f;
10089
10090 if (get_float_arg(argvars, &f) == OK)
10091 {
10092 if (f < -0x7fffffff)
10093 rettv->vval.v_number = -0x7fffffff;
10094 else if (f > 0x7fffffff)
10095 rettv->vval.v_number = 0x7fffffff;
10096 else
10097 rettv->vval.v_number = (varnumber_T)f;
10098 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010099}
10100
10101/*
10102 * "floor({float})" function
10103 */
10104 static void
10105f_floor(argvars, rettv)
10106 typval_T *argvars;
10107 typval_T *rettv;
10108{
10109 float_T f;
10110
10111 rettv->v_type = VAR_FLOAT;
10112 if (get_float_arg(argvars, &f) == OK)
10113 rettv->vval.v_float = floor(f);
10114 else
10115 rettv->vval.v_float = 0.0;
10116}
10117#endif
10118
Bram Moolenaar0d660222005-01-07 21:51:51 +000010119/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010120 * "fnameescape({string})" function
10121 */
10122 static void
10123f_fnameescape(argvars, rettv)
10124 typval_T *argvars;
10125 typval_T *rettv;
10126{
10127 rettv->vval.v_string = vim_strsave_fnameescape(
10128 get_tv_string(&argvars[0]), FALSE);
10129 rettv->v_type = VAR_STRING;
10130}
10131
10132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 * "fnamemodify({fname}, {mods})" function
10134 */
10135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010136f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010137 typval_T *argvars;
10138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139{
10140 char_u *fname;
10141 char_u *mods;
10142 int usedlen = 0;
10143 int len;
10144 char_u *fbuf = NULL;
10145 char_u buf[NUMBUFLEN];
10146
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010147 fname = get_tv_string_chk(&argvars[0]);
10148 mods = get_tv_string_buf_chk(&argvars[1], buf);
10149 if (fname == NULL || mods == NULL)
10150 fname = NULL;
10151 else
10152 {
10153 len = (int)STRLEN(fname);
10154 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010157 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010159 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 vim_free(fbuf);
10163}
10164
Bram Moolenaar33570922005-01-25 22:26:29 +000010165static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166
10167/*
10168 * "foldclosed()" function
10169 */
10170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010171foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010172 typval_T *argvars;
10173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 int end;
10175{
10176#ifdef FEAT_FOLDING
10177 linenr_T lnum;
10178 linenr_T first, last;
10179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010180 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10182 {
10183 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10184 {
10185 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010186 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010188 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 return;
10190 }
10191 }
10192#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010193 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194}
10195
10196/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010197 * "foldclosed()" function
10198 */
10199 static void
10200f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010201 typval_T *argvars;
10202 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010203{
10204 foldclosed_both(argvars, rettv, FALSE);
10205}
10206
10207/*
10208 * "foldclosedend()" function
10209 */
10210 static void
10211f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010212 typval_T *argvars;
10213 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010214{
10215 foldclosed_both(argvars, rettv, TRUE);
10216}
10217
10218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 * "foldlevel()" function
10220 */
10221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010222f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010223 typval_T *argvars;
10224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225{
10226#ifdef FEAT_FOLDING
10227 linenr_T lnum;
10228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010229 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010231 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233}
10234
10235/*
10236 * "foldtext()" function
10237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010239f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010240 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242{
10243#ifdef FEAT_FOLDING
10244 linenr_T lnum;
10245 char_u *s;
10246 char_u *r;
10247 int len;
10248 char *txt;
10249#endif
10250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010251 rettv->v_type = VAR_STRING;
10252 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010254 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10255 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10256 <= curbuf->b_ml.ml_line_count
10257 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 {
10259 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010260 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10261 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 {
10263 if (!linewhite(lnum))
10264 break;
10265 ++lnum;
10266 }
10267
10268 /* Find interesting text in this line. */
10269 s = skipwhite(ml_get(lnum));
10270 /* skip C comment-start */
10271 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010274 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010275 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010276 {
10277 s = skipwhite(ml_get(lnum + 1));
10278 if (*s == '*')
10279 s = skipwhite(s + 1);
10280 }
10281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 txt = _("+-%s%3ld lines: ");
10283 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010284 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 + 20 /* for %3ld */
10286 + STRLEN(s))); /* concatenated */
10287 if (r != NULL)
10288 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010289 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10290 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10291 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 len = (int)STRLEN(r);
10293 STRCAT(r, s);
10294 /* remove 'foldmarker' and 'commentstring' */
10295 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010296 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 }
10298 }
10299#endif
10300}
10301
10302/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010303 * "foldtextresult(lnum)" function
10304 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010306f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010307 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010308 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010309{
10310#ifdef FEAT_FOLDING
10311 linenr_T lnum;
10312 char_u *text;
10313 char_u buf[51];
10314 foldinfo_T foldinfo;
10315 int fold_count;
10316#endif
10317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010318 rettv->v_type = VAR_STRING;
10319 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010320#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010321 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010322 /* treat illegal types and illegal string values for {lnum} the same */
10323 if (lnum < 0)
10324 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010325 fold_count = foldedCount(curwin, lnum, &foldinfo);
10326 if (fold_count > 0)
10327 {
10328 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10329 &foldinfo, buf);
10330 if (text == buf)
10331 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010332 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010333 }
10334#endif
10335}
10336
10337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 * "foreground()" function
10339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010341f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010342 typval_T *argvars UNUSED;
10343 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345#ifdef FEAT_GUI
10346 if (gui.in_use)
10347 gui_mch_set_foreground();
10348#else
10349# ifdef WIN32
10350 win32_set_foreground();
10351# endif
10352#endif
10353}
10354
10355/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010356 * "function()" function
10357 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010359f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010360 typval_T *argvars;
10361 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010362{
10363 char_u *s;
10364
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010365 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010366 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010367 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010368 /* Don't check an autoload name for existence here. */
10369 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010370 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010371 else
10372 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010373 rettv->vval.v_string = vim_strsave(s);
10374 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010375 }
10376}
10377
10378/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010379 * "garbagecollect()" function
10380 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010381 static void
10382f_garbagecollect(argvars, rettv)
10383 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010384 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010385{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010386 /* This is postponed until we are back at the toplevel, because we may be
10387 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10388 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010389
10390 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10391 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010392}
10393
10394/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010395 * "get()" function
10396 */
10397 static void
10398f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010399 typval_T *argvars;
10400 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010401{
Bram Moolenaar33570922005-01-25 22:26:29 +000010402 listitem_T *li;
10403 list_T *l;
10404 dictitem_T *di;
10405 dict_T *d;
10406 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010407
Bram Moolenaare9a41262005-01-15 22:18:47 +000010408 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010409 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010410 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010411 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010412 int error = FALSE;
10413
10414 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10415 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010416 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010417 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010418 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010419 else if (argvars[0].v_type == VAR_DICT)
10420 {
10421 if ((d = argvars[0].vval.v_dict) != NULL)
10422 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010423 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010424 if (di != NULL)
10425 tv = &di->di_tv;
10426 }
10427 }
10428 else
10429 EMSG2(_(e_listdictarg), "get()");
10430
10431 if (tv == NULL)
10432 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010433 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010434 copy_tv(&argvars[2], rettv);
10435 }
10436 else
10437 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010438}
10439
Bram Moolenaar342337a2005-07-21 21:11:17 +000010440static 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 +000010441
10442/*
10443 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010444 * Return a range (from start to end) of lines in rettv from the specified
10445 * buffer.
10446 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010447 */
10448 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010449get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010450 buf_T *buf;
10451 linenr_T start;
10452 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010453 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010454 typval_T *rettv;
10455{
10456 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010457
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010458 if (retlist && rettv_list_alloc(rettv) == FAIL)
10459 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010460
10461 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10462 return;
10463
10464 if (!retlist)
10465 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010466 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10467 p = ml_get_buf(buf, start, FALSE);
10468 else
10469 p = (char_u *)"";
10470
10471 rettv->v_type = VAR_STRING;
10472 rettv->vval.v_string = vim_strsave(p);
10473 }
10474 else
10475 {
10476 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010477 return;
10478
10479 if (start < 1)
10480 start = 1;
10481 if (end > buf->b_ml.ml_line_count)
10482 end = buf->b_ml.ml_line_count;
10483 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010484 if (list_append_string(rettv->vval.v_list,
10485 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010486 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010487 }
10488}
10489
10490/*
10491 * "getbufline()" function
10492 */
10493 static void
10494f_getbufline(argvars, rettv)
10495 typval_T *argvars;
10496 typval_T *rettv;
10497{
10498 linenr_T lnum;
10499 linenr_T end;
10500 buf_T *buf;
10501
10502 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10503 ++emsg_off;
10504 buf = get_buf_tv(&argvars[0]);
10505 --emsg_off;
10506
Bram Moolenaar661b1822005-07-28 22:36:45 +000010507 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010508 if (argvars[2].v_type == VAR_UNKNOWN)
10509 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010510 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010511 end = get_tv_lnum_buf(&argvars[2], buf);
10512
Bram Moolenaar342337a2005-07-21 21:11:17 +000010513 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010514}
10515
Bram Moolenaar0d660222005-01-07 21:51:51 +000010516/*
10517 * "getbufvar()" function
10518 */
10519 static void
10520f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010521 typval_T *argvars;
10522 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010523{
10524 buf_T *buf;
10525 buf_T *save_curbuf;
10526 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010527 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010528
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010529 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10530 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010531 ++emsg_off;
10532 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010533
10534 rettv->v_type = VAR_STRING;
10535 rettv->vval.v_string = NULL;
10536
10537 if (buf != NULL && varname != NULL)
10538 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010539 /* set curbuf to be our buf, temporarily */
10540 save_curbuf = curbuf;
10541 curbuf = buf;
10542
Bram Moolenaar0d660222005-01-07 21:51:51 +000010543 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010544 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010545 else
10546 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010547 if (*varname == NUL)
10548 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10549 * scope prefix before the NUL byte is required by
10550 * find_var_in_ht(). */
10551 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010552 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010553 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010554 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010555 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010556 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010557
10558 /* restore previous notion of curbuf */
10559 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010560 }
10561
10562 --emsg_off;
10563}
10564
10565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 * "getchar()" function
10567 */
10568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010569f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010570 typval_T *argvars;
10571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572{
10573 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010574 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010576 /* Position the cursor. Needed after a message that ends in a space. */
10577 windgoto(msg_row, msg_col);
10578
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 ++no_mapping;
10580 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010581 for (;;)
10582 {
10583 if (argvars[0].v_type == VAR_UNKNOWN)
10584 /* getchar(): blocking wait. */
10585 n = safe_vgetc();
10586 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10587 /* getchar(1): only check if char avail */
10588 n = vpeekc();
10589 else if (error || vpeekc() == NUL)
10590 /* illegal argument or getchar(0) and no char avail: return zero */
10591 n = 0;
10592 else
10593 /* getchar(0) and char avail: return char */
10594 n = safe_vgetc();
10595 if (n == K_IGNORE)
10596 continue;
10597 break;
10598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 --no_mapping;
10600 --allow_keys;
10601
Bram Moolenaar219b8702006-11-01 14:32:36 +000010602 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10603 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10604 vimvars[VV_MOUSE_COL].vv_nr = 0;
10605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010606 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607 if (IS_SPECIAL(n) || mod_mask != 0)
10608 {
10609 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10610 int i = 0;
10611
10612 /* Turn a special key into three bytes, plus modifier. */
10613 if (mod_mask != 0)
10614 {
10615 temp[i++] = K_SPECIAL;
10616 temp[i++] = KS_MODIFIER;
10617 temp[i++] = mod_mask;
10618 }
10619 if (IS_SPECIAL(n))
10620 {
10621 temp[i++] = K_SPECIAL;
10622 temp[i++] = K_SECOND(n);
10623 temp[i++] = K_THIRD(n);
10624 }
10625#ifdef FEAT_MBYTE
10626 else if (has_mbyte)
10627 i += (*mb_char2bytes)(n, temp + i);
10628#endif
10629 else
10630 temp[i++] = n;
10631 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010632 rettv->v_type = VAR_STRING;
10633 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010634
10635#ifdef FEAT_MOUSE
10636 if (n == K_LEFTMOUSE
10637 || n == K_LEFTMOUSE_NM
10638 || n == K_LEFTDRAG
10639 || n == K_LEFTRELEASE
10640 || n == K_LEFTRELEASE_NM
10641 || n == K_MIDDLEMOUSE
10642 || n == K_MIDDLEDRAG
10643 || n == K_MIDDLERELEASE
10644 || n == K_RIGHTMOUSE
10645 || n == K_RIGHTDRAG
10646 || n == K_RIGHTRELEASE
10647 || n == K_X1MOUSE
10648 || n == K_X1DRAG
10649 || n == K_X1RELEASE
10650 || n == K_X2MOUSE
10651 || n == K_X2DRAG
10652 || n == K_X2RELEASE
10653 || n == K_MOUSEDOWN
10654 || n == K_MOUSEUP)
10655 {
10656 int row = mouse_row;
10657 int col = mouse_col;
10658 win_T *win;
10659 linenr_T lnum;
10660# ifdef FEAT_WINDOWS
10661 win_T *wp;
10662# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010663 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010664
10665 if (row >= 0 && col >= 0)
10666 {
10667 /* Find the window at the mouse coordinates and compute the
10668 * text position. */
10669 win = mouse_find_win(&row, &col);
10670 (void)mouse_comp_pos(win, &row, &col, &lnum);
10671# ifdef FEAT_WINDOWS
10672 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010673 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010674# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010675 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010676 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10677 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10678 }
10679 }
10680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 }
10682}
10683
10684/*
10685 * "getcharmod()" function
10686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010688f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010689 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010692 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693}
10694
10695/*
10696 * "getcmdline()" function
10697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010699f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010700 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010703 rettv->v_type = VAR_STRING;
10704 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705}
10706
10707/*
10708 * "getcmdpos()" function
10709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010711f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010712 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010715 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716}
10717
10718/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010719 * "getcmdtype()" function
10720 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010721 static void
10722f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010723 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010724 typval_T *rettv;
10725{
10726 rettv->v_type = VAR_STRING;
10727 rettv->vval.v_string = alloc(2);
10728 if (rettv->vval.v_string != NULL)
10729 {
10730 rettv->vval.v_string[0] = get_cmdline_type();
10731 rettv->vval.v_string[1] = NUL;
10732 }
10733}
10734
10735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 * "getcwd()" function
10737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010739f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010740 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742{
10743 char_u cwd[MAXPATHL];
10744
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010745 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010747 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 else
10749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010750 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010752 if (rettv->vval.v_string != NULL)
10753 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754#endif
10755 }
10756}
10757
10758/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010759 * "getfontname()" function
10760 */
10761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010762f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010763 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010764 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010765{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010766 rettv->v_type = VAR_STRING;
10767 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010768#ifdef FEAT_GUI
10769 if (gui.in_use)
10770 {
10771 GuiFont font;
10772 char_u *name = NULL;
10773
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010774 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010775 {
10776 /* Get the "Normal" font. Either the name saved by
10777 * hl_set_font_name() or from the font ID. */
10778 font = gui.norm_font;
10779 name = hl_get_font_name();
10780 }
10781 else
10782 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010783 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010784 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10785 return;
10786 font = gui_mch_get_font(name, FALSE);
10787 if (font == NOFONT)
10788 return; /* Invalid font name, return empty string. */
10789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010790 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010791 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010792 gui_mch_free_font(font);
10793 }
10794#endif
10795}
10796
10797/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010798 * "getfperm({fname})" function
10799 */
10800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010801f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010802 typval_T *argvars;
10803 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010804{
10805 char_u *fname;
10806 struct stat st;
10807 char_u *perm = NULL;
10808 char_u flags[] = "rwx";
10809 int i;
10810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010811 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010812
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010813 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010814 if (mch_stat((char *)fname, &st) >= 0)
10815 {
10816 perm = vim_strsave((char_u *)"---------");
10817 if (perm != NULL)
10818 {
10819 for (i = 0; i < 9; i++)
10820 {
10821 if (st.st_mode & (1 << (8 - i)))
10822 perm[i] = flags[i % 3];
10823 }
10824 }
10825 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010826 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010827}
10828
10829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830 * "getfsize({fname})" function
10831 */
10832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010833f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010834 typval_T *argvars;
10835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836{
10837 char_u *fname;
10838 struct stat st;
10839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010842 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843
10844 if (mch_stat((char *)fname, &st) >= 0)
10845 {
10846 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010847 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010849 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010850 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010851
10852 /* non-perfect check for overflow */
10853 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10854 rettv->vval.v_number = -2;
10855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 }
10857 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010858 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859}
10860
10861/*
10862 * "getftime({fname})" function
10863 */
10864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010865f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010866 typval_T *argvars;
10867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868{
10869 char_u *fname;
10870 struct stat st;
10871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873
10874 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010875 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010877 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878}
10879
10880/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010881 * "getftype({fname})" function
10882 */
10883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010884f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010885 typval_T *argvars;
10886 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010887{
10888 char_u *fname;
10889 struct stat st;
10890 char_u *type = NULL;
10891 char *t;
10892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010893 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010894
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010895 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010896 if (mch_lstat((char *)fname, &st) >= 0)
10897 {
10898#ifdef S_ISREG
10899 if (S_ISREG(st.st_mode))
10900 t = "file";
10901 else if (S_ISDIR(st.st_mode))
10902 t = "dir";
10903# ifdef S_ISLNK
10904 else if (S_ISLNK(st.st_mode))
10905 t = "link";
10906# endif
10907# ifdef S_ISBLK
10908 else if (S_ISBLK(st.st_mode))
10909 t = "bdev";
10910# endif
10911# ifdef S_ISCHR
10912 else if (S_ISCHR(st.st_mode))
10913 t = "cdev";
10914# endif
10915# ifdef S_ISFIFO
10916 else if (S_ISFIFO(st.st_mode))
10917 t = "fifo";
10918# endif
10919# ifdef S_ISSOCK
10920 else if (S_ISSOCK(st.st_mode))
10921 t = "fifo";
10922# endif
10923 else
10924 t = "other";
10925#else
10926# ifdef S_IFMT
10927 switch (st.st_mode & S_IFMT)
10928 {
10929 case S_IFREG: t = "file"; break;
10930 case S_IFDIR: t = "dir"; break;
10931# ifdef S_IFLNK
10932 case S_IFLNK: t = "link"; break;
10933# endif
10934# ifdef S_IFBLK
10935 case S_IFBLK: t = "bdev"; break;
10936# endif
10937# ifdef S_IFCHR
10938 case S_IFCHR: t = "cdev"; break;
10939# endif
10940# ifdef S_IFIFO
10941 case S_IFIFO: t = "fifo"; break;
10942# endif
10943# ifdef S_IFSOCK
10944 case S_IFSOCK: t = "socket"; break;
10945# endif
10946 default: t = "other";
10947 }
10948# else
10949 if (mch_isdir(fname))
10950 t = "dir";
10951 else
10952 t = "file";
10953# endif
10954#endif
10955 type = vim_strsave((char_u *)t);
10956 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010957 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010958}
10959
10960/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010961 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010962 */
10963 static void
10964f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010965 typval_T *argvars;
10966 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010967{
10968 linenr_T lnum;
10969 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010970 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010971
10972 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010973 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010974 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010975 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010976 retlist = FALSE;
10977 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010978 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010979 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010980 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010981 retlist = TRUE;
10982 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010983
Bram Moolenaar342337a2005-07-21 21:11:17 +000010984 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010985}
10986
10987/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010988 * "getmatches()" function
10989 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010990 static void
10991f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010992 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010993 typval_T *rettv;
10994{
10995#ifdef FEAT_SEARCH_EXTRA
10996 dict_T *dict;
10997 matchitem_T *cur = curwin->w_match_head;
10998
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010999 if (rettv_list_alloc(rettv) == OK)
11000 {
11001 while (cur != NULL)
11002 {
11003 dict = dict_alloc();
11004 if (dict == NULL)
11005 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011006 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11007 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11008 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11009 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11010 list_append_dict(rettv->vval.v_list, dict);
11011 cur = cur->next;
11012 }
11013 }
11014#endif
11015}
11016
11017/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011018 * "getpid()" function
11019 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011020 static void
11021f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011022 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011023 typval_T *rettv;
11024{
11025 rettv->vval.v_number = mch_get_pid();
11026}
11027
11028/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011029 * "getpos(string)" function
11030 */
11031 static void
11032f_getpos(argvars, rettv)
11033 typval_T *argvars;
11034 typval_T *rettv;
11035{
11036 pos_T *fp;
11037 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011038 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011039
11040 if (rettv_list_alloc(rettv) == OK)
11041 {
11042 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011043 fp = var2fpos(&argvars[0], TRUE, &fnum);
11044 if (fnum != -1)
11045 list_append_number(l, (varnumber_T)fnum);
11046 else
11047 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011048 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11049 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011050 list_append_number(l, (fp != NULL)
11051 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011052 : (varnumber_T)0);
11053 list_append_number(l,
11054#ifdef FEAT_VIRTUALEDIT
11055 (fp != NULL) ? (varnumber_T)fp->coladd :
11056#endif
11057 (varnumber_T)0);
11058 }
11059 else
11060 rettv->vval.v_number = FALSE;
11061}
11062
11063/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011064 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011065 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011066 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011067f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011068 typval_T *argvars UNUSED;
11069 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011070{
11071#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011072 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011073#endif
11074
Bram Moolenaar2641f772005-03-25 21:58:17 +000011075#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011076 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011077 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011078 wp = NULL;
11079 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11080 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011081 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011082 if (wp == NULL)
11083 return;
11084 }
11085
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011086 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011087 }
11088#endif
11089}
11090
11091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 * "getreg()" function
11093 */
11094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011095f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011096 typval_T *argvars;
11097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098{
11099 char_u *strregname;
11100 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011101 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011102 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011104 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011106 strregname = get_tv_string_chk(&argvars[0]);
11107 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011108 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011109 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011112 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 regname = (strregname == NULL ? '"' : *strregname);
11114 if (regname == 0)
11115 regname = '"';
11116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011117 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011118 rettv->vval.v_string = error ? NULL :
11119 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120}
11121
11122/*
11123 * "getregtype()" function
11124 */
11125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011126f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011127 typval_T *argvars;
11128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129{
11130 char_u *strregname;
11131 int regname;
11132 char_u buf[NUMBUFLEN + 2];
11133 long reglen = 0;
11134
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011135 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011136 {
11137 strregname = get_tv_string_chk(&argvars[0]);
11138 if (strregname == NULL) /* type error; errmsg already given */
11139 {
11140 rettv->v_type = VAR_STRING;
11141 rettv->vval.v_string = NULL;
11142 return;
11143 }
11144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145 else
11146 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011147 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148
11149 regname = (strregname == NULL ? '"' : *strregname);
11150 if (regname == 0)
11151 regname = '"';
11152
11153 buf[0] = NUL;
11154 buf[1] = NUL;
11155 switch (get_reg_type(regname, &reglen))
11156 {
11157 case MLINE: buf[0] = 'V'; break;
11158 case MCHAR: buf[0] = 'v'; break;
11159#ifdef FEAT_VISUAL
11160 case MBLOCK:
11161 buf[0] = Ctrl_V;
11162 sprintf((char *)buf + 1, "%ld", reglen + 1);
11163 break;
11164#endif
11165 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011166 rettv->v_type = VAR_STRING;
11167 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168}
11169
11170/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011171 * "gettabwinvar()" function
11172 */
11173 static void
11174f_gettabwinvar(argvars, rettv)
11175 typval_T *argvars;
11176 typval_T *rettv;
11177{
11178 getwinvar(argvars, rettv, 1);
11179}
11180
11181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 * "getwinposx()" function
11183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011185f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011186 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011189 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190#ifdef FEAT_GUI
11191 if (gui.in_use)
11192 {
11193 int x, y;
11194
11195 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011196 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 }
11198#endif
11199}
11200
11201/*
11202 * "getwinposy()" function
11203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011205f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011206 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011209 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210#ifdef FEAT_GUI
11211 if (gui.in_use)
11212 {
11213 int x, y;
11214
11215 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011216 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 }
11218#endif
11219}
11220
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011221/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011222 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011223 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011224 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011225find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011226 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011227 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011228{
11229#ifdef FEAT_WINDOWS
11230 win_T *wp;
11231#endif
11232 int nr;
11233
11234 nr = get_tv_number_chk(vp, NULL);
11235
11236#ifdef FEAT_WINDOWS
11237 if (nr < 0)
11238 return NULL;
11239 if (nr == 0)
11240 return curwin;
11241
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011242 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11243 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011244 if (--nr <= 0)
11245 break;
11246 return wp;
11247#else
11248 if (nr == 0 || nr == 1)
11249 return curwin;
11250 return NULL;
11251#endif
11252}
11253
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254/*
11255 * "getwinvar()" function
11256 */
11257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011258f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011259 typval_T *argvars;
11260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011262 getwinvar(argvars, rettv, 0);
11263}
11264
11265/*
11266 * getwinvar() and gettabwinvar()
11267 */
11268 static void
11269getwinvar(argvars, rettv, off)
11270 typval_T *argvars;
11271 typval_T *rettv;
11272 int off; /* 1 for gettabwinvar() */
11273{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274 win_T *win, *oldcurwin;
11275 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011276 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011277 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011279#ifdef FEAT_WINDOWS
11280 if (off == 1)
11281 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11282 else
11283 tp = curtab;
11284#endif
11285 win = find_win_by_nr(&argvars[off], tp);
11286 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011287 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011289 rettv->v_type = VAR_STRING;
11290 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291
11292 if (win != NULL && varname != NULL)
11293 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011294 /* Set curwin to be our win, temporarily. Also set curbuf, so
11295 * that we can get buffer-local options. */
11296 oldcurwin = curwin;
11297 curwin = win;
11298 curbuf = win->w_buffer;
11299
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011301 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 else
11303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011304 if (*varname == NUL)
11305 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11306 * scope prefix before the NUL byte is required by
11307 * find_var_in_ht(). */
11308 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011310 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011312 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011314
11315 /* restore previous notion of curwin */
11316 curwin = oldcurwin;
11317 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 }
11319
11320 --emsg_off;
11321}
11322
11323/*
11324 * "glob()" function
11325 */
11326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011327f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011328 typval_T *argvars;
11329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011331 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011333 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011335 /* When the optional second argument is non-zero, don't remove matches
11336 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11337 if (argvars[1].v_type != VAR_UNKNOWN
11338 && get_tv_number_chk(&argvars[1], &error))
11339 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011340 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011341 if (!error)
11342 {
11343 ExpandInit(&xpc);
11344 xpc.xp_context = EXPAND_FILES;
11345 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11346 NULL, flags, WILD_ALL);
11347 }
11348 else
11349 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350}
11351
11352/*
11353 * "globpath()" function
11354 */
11355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011356f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011357 typval_T *argvars;
11358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011360 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011362 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011363 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011365 /* When the optional second argument is non-zero, don't remove matches
11366 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11367 if (argvars[2].v_type != VAR_UNKNOWN
11368 && get_tv_number_chk(&argvars[2], &error))
11369 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011370 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011371 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011372 rettv->vval.v_string = NULL;
11373 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011374 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11375 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376}
11377
11378/*
11379 * "has()" function
11380 */
11381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011382f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011383 typval_T *argvars;
11384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385{
11386 int i;
11387 char_u *name;
11388 int n = FALSE;
11389 static char *(has_list[]) =
11390 {
11391#ifdef AMIGA
11392 "amiga",
11393# ifdef FEAT_ARP
11394 "arp",
11395# endif
11396#endif
11397#ifdef __BEOS__
11398 "beos",
11399#endif
11400#ifdef MSDOS
11401# ifdef DJGPP
11402 "dos32",
11403# else
11404 "dos16",
11405# endif
11406#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011407#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 "mac",
11409#endif
11410#if defined(MACOS_X_UNIX)
11411 "macunix",
11412#endif
11413#ifdef OS2
11414 "os2",
11415#endif
11416#ifdef __QNX__
11417 "qnx",
11418#endif
11419#ifdef RISCOS
11420 "riscos",
11421#endif
11422#ifdef UNIX
11423 "unix",
11424#endif
11425#ifdef VMS
11426 "vms",
11427#endif
11428#ifdef WIN16
11429 "win16",
11430#endif
11431#ifdef WIN32
11432 "win32",
11433#endif
11434#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11435 "win32unix",
11436#endif
11437#ifdef WIN64
11438 "win64",
11439#endif
11440#ifdef EBCDIC
11441 "ebcdic",
11442#endif
11443#ifndef CASE_INSENSITIVE_FILENAME
11444 "fname_case",
11445#endif
11446#ifdef FEAT_ARABIC
11447 "arabic",
11448#endif
11449#ifdef FEAT_AUTOCMD
11450 "autocmd",
11451#endif
11452#ifdef FEAT_BEVAL
11453 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011454# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11455 "balloon_multiline",
11456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457#endif
11458#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11459 "builtin_terms",
11460# ifdef ALL_BUILTIN_TCAPS
11461 "all_builtin_terms",
11462# endif
11463#endif
11464#ifdef FEAT_BYTEOFF
11465 "byte_offset",
11466#endif
11467#ifdef FEAT_CINDENT
11468 "cindent",
11469#endif
11470#ifdef FEAT_CLIENTSERVER
11471 "clientserver",
11472#endif
11473#ifdef FEAT_CLIPBOARD
11474 "clipboard",
11475#endif
11476#ifdef FEAT_CMDL_COMPL
11477 "cmdline_compl",
11478#endif
11479#ifdef FEAT_CMDHIST
11480 "cmdline_hist",
11481#endif
11482#ifdef FEAT_COMMENTS
11483 "comments",
11484#endif
11485#ifdef FEAT_CRYPT
11486 "cryptv",
11487#endif
11488#ifdef FEAT_CSCOPE
11489 "cscope",
11490#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011491#ifdef CURSOR_SHAPE
11492 "cursorshape",
11493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494#ifdef DEBUG
11495 "debug",
11496#endif
11497#ifdef FEAT_CON_DIALOG
11498 "dialog_con",
11499#endif
11500#ifdef FEAT_GUI_DIALOG
11501 "dialog_gui",
11502#endif
11503#ifdef FEAT_DIFF
11504 "diff",
11505#endif
11506#ifdef FEAT_DIGRAPHS
11507 "digraphs",
11508#endif
11509#ifdef FEAT_DND
11510 "dnd",
11511#endif
11512#ifdef FEAT_EMACS_TAGS
11513 "emacs_tags",
11514#endif
11515 "eval", /* always present, of course! */
11516#ifdef FEAT_EX_EXTRA
11517 "ex_extra",
11518#endif
11519#ifdef FEAT_SEARCH_EXTRA
11520 "extra_search",
11521#endif
11522#ifdef FEAT_FKMAP
11523 "farsi",
11524#endif
11525#ifdef FEAT_SEARCHPATH
11526 "file_in_path",
11527#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011528#if defined(UNIX) && !defined(USE_SYSTEM)
11529 "filterpipe",
11530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531#ifdef FEAT_FIND_ID
11532 "find_in_path",
11533#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011534#ifdef FEAT_FLOAT
11535 "float",
11536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537#ifdef FEAT_FOLDING
11538 "folding",
11539#endif
11540#ifdef FEAT_FOOTER
11541 "footer",
11542#endif
11543#if !defined(USE_SYSTEM) && defined(UNIX)
11544 "fork",
11545#endif
11546#ifdef FEAT_GETTEXT
11547 "gettext",
11548#endif
11549#ifdef FEAT_GUI
11550 "gui",
11551#endif
11552#ifdef FEAT_GUI_ATHENA
11553# ifdef FEAT_GUI_NEXTAW
11554 "gui_neXtaw",
11555# else
11556 "gui_athena",
11557# endif
11558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559#ifdef FEAT_GUI_GTK
11560 "gui_gtk",
11561# ifdef HAVE_GTK2
11562 "gui_gtk2",
11563# endif
11564#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011565#ifdef FEAT_GUI_GNOME
11566 "gui_gnome",
11567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568#ifdef FEAT_GUI_MAC
11569 "gui_mac",
11570#endif
11571#ifdef FEAT_GUI_MOTIF
11572 "gui_motif",
11573#endif
11574#ifdef FEAT_GUI_PHOTON
11575 "gui_photon",
11576#endif
11577#ifdef FEAT_GUI_W16
11578 "gui_win16",
11579#endif
11580#ifdef FEAT_GUI_W32
11581 "gui_win32",
11582#endif
11583#ifdef FEAT_HANGULIN
11584 "hangul_input",
11585#endif
11586#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11587 "iconv",
11588#endif
11589#ifdef FEAT_INS_EXPAND
11590 "insert_expand",
11591#endif
11592#ifdef FEAT_JUMPLIST
11593 "jumplist",
11594#endif
11595#ifdef FEAT_KEYMAP
11596 "keymap",
11597#endif
11598#ifdef FEAT_LANGMAP
11599 "langmap",
11600#endif
11601#ifdef FEAT_LIBCALL
11602 "libcall",
11603#endif
11604#ifdef FEAT_LINEBREAK
11605 "linebreak",
11606#endif
11607#ifdef FEAT_LISP
11608 "lispindent",
11609#endif
11610#ifdef FEAT_LISTCMDS
11611 "listcmds",
11612#endif
11613#ifdef FEAT_LOCALMAP
11614 "localmap",
11615#endif
11616#ifdef FEAT_MENU
11617 "menu",
11618#endif
11619#ifdef FEAT_SESSION
11620 "mksession",
11621#endif
11622#ifdef FEAT_MODIFY_FNAME
11623 "modify_fname",
11624#endif
11625#ifdef FEAT_MOUSE
11626 "mouse",
11627#endif
11628#ifdef FEAT_MOUSESHAPE
11629 "mouseshape",
11630#endif
11631#if defined(UNIX) || defined(VMS)
11632# ifdef FEAT_MOUSE_DEC
11633 "mouse_dec",
11634# endif
11635# ifdef FEAT_MOUSE_GPM
11636 "mouse_gpm",
11637# endif
11638# ifdef FEAT_MOUSE_JSB
11639 "mouse_jsbterm",
11640# endif
11641# ifdef FEAT_MOUSE_NET
11642 "mouse_netterm",
11643# endif
11644# ifdef FEAT_MOUSE_PTERM
11645 "mouse_pterm",
11646# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011647# ifdef FEAT_SYSMOUSE
11648 "mouse_sysmouse",
11649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650# ifdef FEAT_MOUSE_XTERM
11651 "mouse_xterm",
11652# endif
11653#endif
11654#ifdef FEAT_MBYTE
11655 "multi_byte",
11656#endif
11657#ifdef FEAT_MBYTE_IME
11658 "multi_byte_ime",
11659#endif
11660#ifdef FEAT_MULTI_LANG
11661 "multi_lang",
11662#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011663#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011664#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011665 "mzscheme",
11666#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668#ifdef FEAT_OLE
11669 "ole",
11670#endif
11671#ifdef FEAT_OSFILETYPE
11672 "osfiletype",
11673#endif
11674#ifdef FEAT_PATH_EXTRA
11675 "path_extra",
11676#endif
11677#ifdef FEAT_PERL
11678#ifndef DYNAMIC_PERL
11679 "perl",
11680#endif
11681#endif
11682#ifdef FEAT_PYTHON
11683#ifndef DYNAMIC_PYTHON
11684 "python",
11685#endif
11686#endif
11687#ifdef FEAT_POSTSCRIPT
11688 "postscript",
11689#endif
11690#ifdef FEAT_PRINTER
11691 "printer",
11692#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011693#ifdef FEAT_PROFILE
11694 "profile",
11695#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011696#ifdef FEAT_RELTIME
11697 "reltime",
11698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699#ifdef FEAT_QUICKFIX
11700 "quickfix",
11701#endif
11702#ifdef FEAT_RIGHTLEFT
11703 "rightleft",
11704#endif
11705#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11706 "ruby",
11707#endif
11708#ifdef FEAT_SCROLLBIND
11709 "scrollbind",
11710#endif
11711#ifdef FEAT_CMDL_INFO
11712 "showcmd",
11713 "cmdline_info",
11714#endif
11715#ifdef FEAT_SIGNS
11716 "signs",
11717#endif
11718#ifdef FEAT_SMARTINDENT
11719 "smartindent",
11720#endif
11721#ifdef FEAT_SNIFF
11722 "sniff",
11723#endif
11724#ifdef FEAT_STL_OPT
11725 "statusline",
11726#endif
11727#ifdef FEAT_SUN_WORKSHOP
11728 "sun_workshop",
11729#endif
11730#ifdef FEAT_NETBEANS_INTG
11731 "netbeans_intg",
11732#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011733#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011734 "spell",
11735#endif
11736#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 "syntax",
11738#endif
11739#if defined(USE_SYSTEM) || !defined(UNIX)
11740 "system",
11741#endif
11742#ifdef FEAT_TAG_BINS
11743 "tag_binary",
11744#endif
11745#ifdef FEAT_TAG_OLDSTATIC
11746 "tag_old_static",
11747#endif
11748#ifdef FEAT_TAG_ANYWHITE
11749 "tag_any_white",
11750#endif
11751#ifdef FEAT_TCL
11752# ifndef DYNAMIC_TCL
11753 "tcl",
11754# endif
11755#endif
11756#ifdef TERMINFO
11757 "terminfo",
11758#endif
11759#ifdef FEAT_TERMRESPONSE
11760 "termresponse",
11761#endif
11762#ifdef FEAT_TEXTOBJ
11763 "textobjects",
11764#endif
11765#ifdef HAVE_TGETENT
11766 "tgetent",
11767#endif
11768#ifdef FEAT_TITLE
11769 "title",
11770#endif
11771#ifdef FEAT_TOOLBAR
11772 "toolbar",
11773#endif
11774#ifdef FEAT_USR_CMDS
11775 "user-commands", /* was accidentally included in 5.4 */
11776 "user_commands",
11777#endif
11778#ifdef FEAT_VIMINFO
11779 "viminfo",
11780#endif
11781#ifdef FEAT_VERTSPLIT
11782 "vertsplit",
11783#endif
11784#ifdef FEAT_VIRTUALEDIT
11785 "virtualedit",
11786#endif
11787#ifdef FEAT_VISUAL
11788 "visual",
11789#endif
11790#ifdef FEAT_VISUALEXTRA
11791 "visualextra",
11792#endif
11793#ifdef FEAT_VREPLACE
11794 "vreplace",
11795#endif
11796#ifdef FEAT_WILDIGN
11797 "wildignore",
11798#endif
11799#ifdef FEAT_WILDMENU
11800 "wildmenu",
11801#endif
11802#ifdef FEAT_WINDOWS
11803 "windows",
11804#endif
11805#ifdef FEAT_WAK
11806 "winaltkeys",
11807#endif
11808#ifdef FEAT_WRITEBACKUP
11809 "writebackup",
11810#endif
11811#ifdef FEAT_XIM
11812 "xim",
11813#endif
11814#ifdef FEAT_XFONTSET
11815 "xfontset",
11816#endif
11817#ifdef USE_XSMP
11818 "xsmp",
11819#endif
11820#ifdef USE_XSMP_INTERACT
11821 "xsmp_interact",
11822#endif
11823#ifdef FEAT_XCLIPBOARD
11824 "xterm_clipboard",
11825#endif
11826#ifdef FEAT_XTERM_SAVE
11827 "xterm_save",
11828#endif
11829#if defined(UNIX) && defined(FEAT_X11)
11830 "X11",
11831#endif
11832 NULL
11833 };
11834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011835 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 for (i = 0; has_list[i] != NULL; ++i)
11837 if (STRICMP(name, has_list[i]) == 0)
11838 {
11839 n = TRUE;
11840 break;
11841 }
11842
11843 if (n == FALSE)
11844 {
11845 if (STRNICMP(name, "patch", 5) == 0)
11846 n = has_patch(atoi((char *)name + 5));
11847 else if (STRICMP(name, "vim_starting") == 0)
11848 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000011849#ifdef FEAT_MBYTE
11850 else if (STRICMP(name, "multi_byte_encoding") == 0)
11851 n = has_mbyte;
11852#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000011853#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11854 else if (STRICMP(name, "balloon_multiline") == 0)
11855 n = multiline_balloon_available();
11856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857#ifdef DYNAMIC_TCL
11858 else if (STRICMP(name, "tcl") == 0)
11859 n = tcl_enabled(FALSE);
11860#endif
11861#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11862 else if (STRICMP(name, "iconv") == 0)
11863 n = iconv_enabled(FALSE);
11864#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011865#ifdef DYNAMIC_MZSCHEME
11866 else if (STRICMP(name, "mzscheme") == 0)
11867 n = mzscheme_enabled(FALSE);
11868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869#ifdef DYNAMIC_RUBY
11870 else if (STRICMP(name, "ruby") == 0)
11871 n = ruby_enabled(FALSE);
11872#endif
11873#ifdef DYNAMIC_PYTHON
11874 else if (STRICMP(name, "python") == 0)
11875 n = python_enabled(FALSE);
11876#endif
11877#ifdef DYNAMIC_PERL
11878 else if (STRICMP(name, "perl") == 0)
11879 n = perl_enabled(FALSE);
11880#endif
11881#ifdef FEAT_GUI
11882 else if (STRICMP(name, "gui_running") == 0)
11883 n = (gui.in_use || gui.starting);
11884# ifdef FEAT_GUI_W32
11885 else if (STRICMP(name, "gui_win32s") == 0)
11886 n = gui_is_win32s();
11887# endif
11888# ifdef FEAT_BROWSE
11889 else if (STRICMP(name, "browse") == 0)
11890 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11891# endif
11892#endif
11893#ifdef FEAT_SYN_HL
11894 else if (STRICMP(name, "syntax_items") == 0)
11895 n = syntax_present(curbuf);
11896#endif
11897#if defined(WIN3264)
11898 else if (STRICMP(name, "win95") == 0)
11899 n = mch_windows95();
11900#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011901#ifdef FEAT_NETBEANS_INTG
11902 else if (STRICMP(name, "netbeans_enabled") == 0)
11903 n = usingNetbeans;
11904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 }
11906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011907 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908}
11909
11910/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011911 * "has_key()" function
11912 */
11913 static void
11914f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011915 typval_T *argvars;
11916 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011917{
Bram Moolenaare9a41262005-01-15 22:18:47 +000011918 if (argvars[0].v_type != VAR_DICT)
11919 {
11920 EMSG(_(e_dictreq));
11921 return;
11922 }
11923 if (argvars[0].vval.v_dict == NULL)
11924 return;
11925
11926 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011927 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011928}
11929
11930/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011931 * "haslocaldir()" function
11932 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011933 static void
11934f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011935 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011936 typval_T *rettv;
11937{
11938 rettv->vval.v_number = (curwin->w_localdir != NULL);
11939}
11940
11941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942 * "hasmapto()" function
11943 */
11944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011945f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011946 typval_T *argvars;
11947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948{
11949 char_u *name;
11950 char_u *mode;
11951 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011952 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011954 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011955 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956 mode = (char_u *)"nvo";
11957 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011958 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011959 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011960 if (argvars[2].v_type != VAR_UNKNOWN)
11961 abbr = get_tv_number(&argvars[2]);
11962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963
Bram Moolenaar2c932302006-03-18 21:42:09 +000011964 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011965 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011967 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968}
11969
11970/*
11971 * "histadd()" function
11972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011974f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011975 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977{
11978#ifdef FEAT_CMDHIST
11979 int histype;
11980 char_u *str;
11981 char_u buf[NUMBUFLEN];
11982#endif
11983
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011984 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985 if (check_restricted() || check_secure())
11986 return;
11987#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011988 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11989 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990 if (histype >= 0)
11991 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011992 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 if (*str != NUL)
11994 {
11995 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011996 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 return;
11998 }
11999 }
12000#endif
12001}
12002
12003/*
12004 * "histdel()" function
12005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012008 typval_T *argvars UNUSED;
12009 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010{
12011#ifdef FEAT_CMDHIST
12012 int n;
12013 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012014 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012016 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12017 if (str == NULL)
12018 n = 0;
12019 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012021 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012022 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012024 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012025 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026 else
12027 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012028 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012029 get_tv_string_buf(&argvars[1], buf));
12030 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031#endif
12032}
12033
12034/*
12035 * "histget()" function
12036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012038f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012039 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041{
12042#ifdef FEAT_CMDHIST
12043 int type;
12044 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012045 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012047 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12048 if (str == NULL)
12049 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012051 {
12052 type = get_histtype(str);
12053 if (argvars[1].v_type == VAR_UNKNOWN)
12054 idx = get_history_idx(type);
12055 else
12056 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12057 /* -1 on type error */
12058 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012061 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012063 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064}
12065
12066/*
12067 * "histnr()" function
12068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012070f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012071 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073{
12074 int i;
12075
12076#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012077 char_u *history = get_tv_string_chk(&argvars[0]);
12078
12079 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080 if (i >= HIST_CMD && i < HIST_COUNT)
12081 i = get_history_idx(i);
12082 else
12083#endif
12084 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012085 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086}
12087
12088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 * "highlightID(name)" function
12090 */
12091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012092f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012093 typval_T *argvars;
12094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012096 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097}
12098
12099/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012100 * "highlight_exists()" function
12101 */
12102 static void
12103f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012104 typval_T *argvars;
12105 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012106{
12107 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12108}
12109
12110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111 * "hostname()" function
12112 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012114f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012115 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012116 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117{
12118 char_u hostname[256];
12119
12120 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012121 rettv->v_type = VAR_STRING;
12122 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123}
12124
12125/*
12126 * iconv() function
12127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012129f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012130 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132{
12133#ifdef FEAT_MBYTE
12134 char_u buf1[NUMBUFLEN];
12135 char_u buf2[NUMBUFLEN];
12136 char_u *from, *to, *str;
12137 vimconv_T vimconv;
12138#endif
12139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012140 rettv->v_type = VAR_STRING;
12141 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142
12143#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012144 str = get_tv_string(&argvars[0]);
12145 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12146 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147 vimconv.vc_type = CONV_NONE;
12148 convert_setup(&vimconv, from, to);
12149
12150 /* If the encodings are equal, no conversion needed. */
12151 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012152 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012154 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155
12156 convert_setup(&vimconv, NULL, NULL);
12157 vim_free(from);
12158 vim_free(to);
12159#endif
12160}
12161
12162/*
12163 * "indent()" function
12164 */
12165 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012166f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012167 typval_T *argvars;
12168 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169{
12170 linenr_T lnum;
12171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012172 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012174 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012176 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177}
12178
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012179/*
12180 * "index()" function
12181 */
12182 static void
12183f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012184 typval_T *argvars;
12185 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012186{
Bram Moolenaar33570922005-01-25 22:26:29 +000012187 list_T *l;
12188 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012189 long idx = 0;
12190 int ic = FALSE;
12191
12192 rettv->vval.v_number = -1;
12193 if (argvars[0].v_type != VAR_LIST)
12194 {
12195 EMSG(_(e_listreq));
12196 return;
12197 }
12198 l = argvars[0].vval.v_list;
12199 if (l != NULL)
12200 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012201 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012202 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012204 int error = FALSE;
12205
Bram Moolenaar758711c2005-02-02 23:11:38 +000012206 /* Start at specified item. Use the cached index that list_find()
12207 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012208 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012209 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012210 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012211 ic = get_tv_number_chk(&argvars[3], &error);
12212 if (error)
12213 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012214 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012215
Bram Moolenaar758711c2005-02-02 23:11:38 +000012216 for ( ; item != NULL; item = item->li_next, ++idx)
12217 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012218 {
12219 rettv->vval.v_number = idx;
12220 break;
12221 }
12222 }
12223}
12224
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225static int inputsecret_flag = 0;
12226
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012227static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12228
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012230 * This function is used by f_input() and f_inputdialog() functions. The third
12231 * argument to f_input() specifies the type of completion to use at the
12232 * prompt. The third argument to f_inputdialog() specifies the value to return
12233 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234 */
12235 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012236get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012237 typval_T *argvars;
12238 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012239 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012241 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242 char_u *p = NULL;
12243 int c;
12244 char_u buf[NUMBUFLEN];
12245 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012246 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012247 int xp_type = EXPAND_NOTHING;
12248 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012250 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012251 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252
12253#ifdef NO_CONSOLE_INPUT
12254 /* While starting up, there is no place to enter text. */
12255 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257#endif
12258
12259 cmd_silent = FALSE; /* Want to see the prompt. */
12260 if (prompt != NULL)
12261 {
12262 /* Only the part of the message after the last NL is considered as
12263 * prompt for the command line */
12264 p = vim_strrchr(prompt, '\n');
12265 if (p == NULL)
12266 p = prompt;
12267 else
12268 {
12269 ++p;
12270 c = *p;
12271 *p = NUL;
12272 msg_start();
12273 msg_clr_eos();
12274 msg_puts_attr(prompt, echo_attr);
12275 msg_didout = FALSE;
12276 msg_starthere();
12277 *p = c;
12278 }
12279 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012281 if (argvars[1].v_type != VAR_UNKNOWN)
12282 {
12283 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12284 if (defstr != NULL)
12285 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012287 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012288 {
12289 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012290 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012291 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012292
Bram Moolenaar4463f292005-09-25 22:20:24 +000012293 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012294
Bram Moolenaar4463f292005-09-25 22:20:24 +000012295 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12296 if (xp_name == NULL)
12297 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012298
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012299 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012300
Bram Moolenaar4463f292005-09-25 22:20:24 +000012301 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12302 &xp_arg) == FAIL)
12303 return;
12304 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012305 }
12306
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012307 if (defstr != NULL)
12308 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012309 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12310 xp_type, xp_arg);
12311
12312 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012314 /* since the user typed this, no need to wait for return */
12315 need_wait_return = FALSE;
12316 msg_didout = FALSE;
12317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318 cmd_silent = cmd_silent_save;
12319}
12320
12321/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012322 * "input()" function
12323 * Also handles inputsecret() when inputsecret is set.
12324 */
12325 static void
12326f_input(argvars, rettv)
12327 typval_T *argvars;
12328 typval_T *rettv;
12329{
12330 get_user_input(argvars, rettv, FALSE);
12331}
12332
12333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334 * "inputdialog()" function
12335 */
12336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012337f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012338 typval_T *argvars;
12339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340{
12341#if defined(FEAT_GUI_TEXTDIALOG)
12342 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12343 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12344 {
12345 char_u *message;
12346 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012347 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012349 message = get_tv_string_chk(&argvars[0]);
12350 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012351 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012352 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353 else
12354 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012355 if (message != NULL && defstr != NULL
12356 && do_dialog(VIM_QUESTION, NULL, message,
12357 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012358 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359 else
12360 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012361 if (message != NULL && defstr != NULL
12362 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012363 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012364 rettv->vval.v_string = vim_strsave(
12365 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012367 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012369 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370 }
12371 else
12372#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012373 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374}
12375
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012376/*
12377 * "inputlist()" function
12378 */
12379 static void
12380f_inputlist(argvars, rettv)
12381 typval_T *argvars;
12382 typval_T *rettv;
12383{
12384 listitem_T *li;
12385 int selected;
12386 int mouse_used;
12387
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012388#ifdef NO_CONSOLE_INPUT
12389 /* While starting up, there is no place to enter text. */
12390 if (no_console_input())
12391 return;
12392#endif
12393 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12394 {
12395 EMSG2(_(e_listarg), "inputlist()");
12396 return;
12397 }
12398
12399 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012400 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012401 lines_left = Rows; /* avoid more prompt */
12402 msg_scroll = TRUE;
12403 msg_clr_eos();
12404
12405 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12406 {
12407 msg_puts(get_tv_string(&li->li_tv));
12408 msg_putchar('\n');
12409 }
12410
12411 /* Ask for choice. */
12412 selected = prompt_for_number(&mouse_used);
12413 if (mouse_used)
12414 selected -= lines_left;
12415
12416 rettv->vval.v_number = selected;
12417}
12418
12419
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12421
12422/*
12423 * "inputrestore()" function
12424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012426f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012427 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429{
12430 if (ga_userinput.ga_len > 0)
12431 {
12432 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12434 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012435 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436 }
12437 else if (p_verbose > 1)
12438 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012439 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012440 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441 }
12442}
12443
12444/*
12445 * "inputsave()" function
12446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012448f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012449 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012452 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453 if (ga_grow(&ga_userinput, 1) == OK)
12454 {
12455 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12456 + ga_userinput.ga_len);
12457 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012458 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459 }
12460 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012461 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462}
12463
12464/*
12465 * "inputsecret()" function
12466 */
12467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012468f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012469 typval_T *argvars;
12470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471{
12472 ++cmdline_star;
12473 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012474 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475 --cmdline_star;
12476 --inputsecret_flag;
12477}
12478
12479/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012480 * "insert()" function
12481 */
12482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012483f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012484 typval_T *argvars;
12485 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012486{
12487 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012488 listitem_T *item;
12489 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012490 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012491
12492 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012493 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012494 else if ((l = argvars[0].vval.v_list) != NULL
12495 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012496 {
12497 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012498 before = get_tv_number_chk(&argvars[2], &error);
12499 if (error)
12500 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012501
Bram Moolenaar758711c2005-02-02 23:11:38 +000012502 if (before == l->lv_len)
12503 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012504 else
12505 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012506 item = list_find(l, before);
12507 if (item == NULL)
12508 {
12509 EMSGN(_(e_listidx), before);
12510 l = NULL;
12511 }
12512 }
12513 if (l != NULL)
12514 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012515 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012516 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012517 }
12518 }
12519}
12520
12521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522 * "isdirectory()" function
12523 */
12524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012525f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012526 typval_T *argvars;
12527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012529 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012530}
12531
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012532/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012533 * "islocked()" function
12534 */
12535 static void
12536f_islocked(argvars, rettv)
12537 typval_T *argvars;
12538 typval_T *rettv;
12539{
12540 lval_T lv;
12541 char_u *end;
12542 dictitem_T *di;
12543
12544 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012545 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12546 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012547 if (end != NULL && lv.ll_name != NULL)
12548 {
12549 if (*end != NUL)
12550 EMSG(_(e_trailing));
12551 else
12552 {
12553 if (lv.ll_tv == NULL)
12554 {
12555 if (check_changedtick(lv.ll_name))
12556 rettv->vval.v_number = 1; /* always locked */
12557 else
12558 {
12559 di = find_var(lv.ll_name, NULL);
12560 if (di != NULL)
12561 {
12562 /* Consider a variable locked when:
12563 * 1. the variable itself is locked
12564 * 2. the value of the variable is locked.
12565 * 3. the List or Dict value is locked.
12566 */
12567 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12568 || tv_islocked(&di->di_tv));
12569 }
12570 }
12571 }
12572 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012573 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012574 else if (lv.ll_newkey != NULL)
12575 EMSG2(_(e_dictkey), lv.ll_newkey);
12576 else if (lv.ll_list != NULL)
12577 /* List item. */
12578 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12579 else
12580 /* Dictionary item. */
12581 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12582 }
12583 }
12584
12585 clear_lval(&lv);
12586}
12587
Bram Moolenaar33570922005-01-25 22:26:29 +000012588static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012589
12590/*
12591 * Turn a dict into a list:
12592 * "what" == 0: list of keys
12593 * "what" == 1: list of values
12594 * "what" == 2: list of items
12595 */
12596 static void
12597dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012598 typval_T *argvars;
12599 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012600 int what;
12601{
Bram Moolenaar33570922005-01-25 22:26:29 +000012602 list_T *l2;
12603 dictitem_T *di;
12604 hashitem_T *hi;
12605 listitem_T *li;
12606 listitem_T *li2;
12607 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012608 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012609
Bram Moolenaar8c711452005-01-14 21:53:12 +000012610 if (argvars[0].v_type != VAR_DICT)
12611 {
12612 EMSG(_(e_dictreq));
12613 return;
12614 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012615 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012616 return;
12617
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012618 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012619 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012620
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012621 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012622 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012623 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012624 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012625 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012626 --todo;
12627 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012628
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012629 li = listitem_alloc();
12630 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012631 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012632 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012633
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012634 if (what == 0)
12635 {
12636 /* keys() */
12637 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012638 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012639 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12640 }
12641 else if (what == 1)
12642 {
12643 /* values() */
12644 copy_tv(&di->di_tv, &li->li_tv);
12645 }
12646 else
12647 {
12648 /* items() */
12649 l2 = list_alloc();
12650 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012651 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012652 li->li_tv.vval.v_list = l2;
12653 if (l2 == NULL)
12654 break;
12655 ++l2->lv_refcount;
12656
12657 li2 = listitem_alloc();
12658 if (li2 == NULL)
12659 break;
12660 list_append(l2, li2);
12661 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012662 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012663 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12664
12665 li2 = listitem_alloc();
12666 if (li2 == NULL)
12667 break;
12668 list_append(l2, li2);
12669 copy_tv(&di->di_tv, &li2->li_tv);
12670 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012671 }
12672 }
12673}
12674
12675/*
12676 * "items(dict)" function
12677 */
12678 static void
12679f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012680 typval_T *argvars;
12681 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012682{
12683 dict_list(argvars, rettv, 2);
12684}
12685
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012687 * "join()" function
12688 */
12689 static void
12690f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012691 typval_T *argvars;
12692 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012693{
12694 garray_T ga;
12695 char_u *sep;
12696
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012697 if (argvars[0].v_type != VAR_LIST)
12698 {
12699 EMSG(_(e_listreq));
12700 return;
12701 }
12702 if (argvars[0].vval.v_list == NULL)
12703 return;
12704 if (argvars[1].v_type == VAR_UNKNOWN)
12705 sep = (char_u *)" ";
12706 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012707 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012708
12709 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012710
12711 if (sep != NULL)
12712 {
12713 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012714 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012715 ga_append(&ga, NUL);
12716 rettv->vval.v_string = (char_u *)ga.ga_data;
12717 }
12718 else
12719 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012720}
12721
12722/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012723 * "keys()" function
12724 */
12725 static void
12726f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012727 typval_T *argvars;
12728 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012729{
12730 dict_list(argvars, rettv, 0);
12731}
12732
12733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734 * "last_buffer_nr()" function.
12735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012737f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012738 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740{
12741 int n = 0;
12742 buf_T *buf;
12743
12744 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12745 if (n < buf->b_fnum)
12746 n = buf->b_fnum;
12747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012748 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012749}
12750
12751/*
12752 * "len()" function
12753 */
12754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012755f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012756 typval_T *argvars;
12757 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012758{
12759 switch (argvars[0].v_type)
12760 {
12761 case VAR_STRING:
12762 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012763 rettv->vval.v_number = (varnumber_T)STRLEN(
12764 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012765 break;
12766 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012767 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012768 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012769 case VAR_DICT:
12770 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12771 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012772 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012773 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012774 break;
12775 }
12776}
12777
Bram Moolenaar33570922005-01-25 22:26:29 +000012778static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012779
12780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012781libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012782 typval_T *argvars;
12783 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012784 int type;
12785{
12786#ifdef FEAT_LIBCALL
12787 char_u *string_in;
12788 char_u **string_result;
12789 int nr_result;
12790#endif
12791
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012792 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012793 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012794 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012795
12796 if (check_restricted() || check_secure())
12797 return;
12798
12799#ifdef FEAT_LIBCALL
12800 /* The first two args must be strings, otherwise its meaningless */
12801 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12802 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012803 string_in = NULL;
12804 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012805 string_in = argvars[2].vval.v_string;
12806 if (type == VAR_NUMBER)
12807 string_result = NULL;
12808 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012810 if (mch_libcall(argvars[0].vval.v_string,
12811 argvars[1].vval.v_string,
12812 string_in,
12813 argvars[2].vval.v_number,
12814 string_result,
12815 &nr_result) == OK
12816 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012818 }
12819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820}
12821
12822/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012823 * "libcall()" function
12824 */
12825 static void
12826f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012827 typval_T *argvars;
12828 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012829{
12830 libcall_common(argvars, rettv, VAR_STRING);
12831}
12832
12833/*
12834 * "libcallnr()" function
12835 */
12836 static void
12837f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012838 typval_T *argvars;
12839 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012840{
12841 libcall_common(argvars, rettv, VAR_NUMBER);
12842}
12843
12844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845 * "line(string)" function
12846 */
12847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012848f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012849 typval_T *argvars;
12850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851{
12852 linenr_T lnum = 0;
12853 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012854 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012856 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857 if (fp != NULL)
12858 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860}
12861
12862/*
12863 * "line2byte(lnum)" function
12864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012866f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012867 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869{
12870#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872#else
12873 linenr_T lnum;
12874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012877 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12880 if (rettv->vval.v_number >= 0)
12881 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882#endif
12883}
12884
12885/*
12886 * "lispindent(lnum)" function
12887 */
12888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012890 typval_T *argvars;
12891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892{
12893#ifdef FEAT_LISP
12894 pos_T pos;
12895 linenr_T lnum;
12896
12897 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012898 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12900 {
12901 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012902 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903 curwin->w_cursor = pos;
12904 }
12905 else
12906#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012907 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908}
12909
12910/*
12911 * "localtime()" function
12912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012914f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012915 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012918 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919}
12920
Bram Moolenaar33570922005-01-25 22:26:29 +000012921static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922
12923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012924get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012925 typval_T *argvars;
12926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927 int exact;
12928{
12929 char_u *keys;
12930 char_u *which;
12931 char_u buf[NUMBUFLEN];
12932 char_u *keys_buf = NULL;
12933 char_u *rhs;
12934 int mode;
12935 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012936 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937
12938 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939 rettv->v_type = VAR_STRING;
12940 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012942 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943 if (*keys == NUL)
12944 return;
12945
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012946 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012948 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012949 if (argvars[2].v_type != VAR_UNKNOWN)
12950 abbr = get_tv_number(&argvars[2]);
12951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952 else
12953 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012954 if (which == NULL)
12955 return;
12956
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957 mode = get_map_mode(&which, 0);
12958
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012959 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012960 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 vim_free(keys_buf);
12962 if (rhs != NULL)
12963 {
12964 ga_init(&ga);
12965 ga.ga_itemsize = 1;
12966 ga.ga_growsize = 40;
12967
12968 while (*rhs != NUL)
12969 ga_concat(&ga, str2special(&rhs, FALSE));
12970
12971 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012972 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973 }
12974}
12975
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012976#ifdef FEAT_FLOAT
12977/*
12978 * "log10()" function
12979 */
12980 static void
12981f_log10(argvars, rettv)
12982 typval_T *argvars;
12983 typval_T *rettv;
12984{
12985 float_T f;
12986
12987 rettv->v_type = VAR_FLOAT;
12988 if (get_float_arg(argvars, &f) == OK)
12989 rettv->vval.v_float = log10(f);
12990 else
12991 rettv->vval.v_float = 0.0;
12992}
12993#endif
12994
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012996 * "map()" function
12997 */
12998 static void
12999f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013000 typval_T *argvars;
13001 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013002{
13003 filter_map(argvars, rettv, TRUE);
13004}
13005
13006/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013007 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013008 */
13009 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013010f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013011 typval_T *argvars;
13012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013014 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013015}
13016
13017/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013018 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019 */
13020 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013021f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013022 typval_T *argvars;
13023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013025 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026}
13027
Bram Moolenaar33570922005-01-25 22:26:29 +000013028static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029
13030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013031find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013032 typval_T *argvars;
13033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034 int type;
13035{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013036 char_u *str = NULL;
13037 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038 char_u *pat;
13039 regmatch_T regmatch;
13040 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013041 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042 char_u *save_cpo;
13043 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013044 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013045 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013046 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013047 list_T *l = NULL;
13048 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013049 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013050 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051
13052 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13053 save_cpo = p_cpo;
13054 p_cpo = (char_u *)"";
13055
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013056 rettv->vval.v_number = -1;
13057 if (type == 3)
13058 {
13059 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013060 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013061 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013062 }
13063 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013065 rettv->v_type = VAR_STRING;
13066 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013069 if (argvars[0].v_type == VAR_LIST)
13070 {
13071 if ((l = argvars[0].vval.v_list) == NULL)
13072 goto theend;
13073 li = l->lv_first;
13074 }
13075 else
13076 expr = str = get_tv_string(&argvars[0]);
13077
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013078 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13079 if (pat == NULL)
13080 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013081
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013082 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013084 int error = FALSE;
13085
13086 start = get_tv_number_chk(&argvars[2], &error);
13087 if (error)
13088 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013089 if (l != NULL)
13090 {
13091 li = list_find(l, start);
13092 if (li == NULL)
13093 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013094 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013095 }
13096 else
13097 {
13098 if (start < 0)
13099 start = 0;
13100 if (start > (long)STRLEN(str))
13101 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013102 /* When "count" argument is there ignore matches before "start",
13103 * otherwise skip part of the string. Differs when pattern is "^"
13104 * or "\<". */
13105 if (argvars[3].v_type != VAR_UNKNOWN)
13106 startcol = start;
13107 else
13108 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013109 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013110
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013111 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013112 nth = get_tv_number_chk(&argvars[3], &error);
13113 if (error)
13114 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115 }
13116
13117 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13118 if (regmatch.regprog != NULL)
13119 {
13120 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013121
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013122 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013123 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013124 if (l != NULL)
13125 {
13126 if (li == NULL)
13127 {
13128 match = FALSE;
13129 break;
13130 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013131 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013132 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013133 if (str == NULL)
13134 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013135 }
13136
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013137 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013138
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013139 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013140 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013141 if (l == NULL && !match)
13142 break;
13143
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013144 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013145 if (l != NULL)
13146 {
13147 li = li->li_next;
13148 ++idx;
13149 }
13150 else
13151 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013152#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013153 startcol = (colnr_T)(regmatch.startp[0]
13154 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013155#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013156 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013157#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013158 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013159 }
13160
13161 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013163 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013164 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013165 int i;
13166
13167 /* return list with matched string and submatches */
13168 for (i = 0; i < NSUBEXP; ++i)
13169 {
13170 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013171 {
13172 if (list_append_string(rettv->vval.v_list,
13173 (char_u *)"", 0) == FAIL)
13174 break;
13175 }
13176 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013177 regmatch.startp[i],
13178 (int)(regmatch.endp[i] - regmatch.startp[i]))
13179 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013180 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013181 }
13182 }
13183 else if (type == 2)
13184 {
13185 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013186 if (l != NULL)
13187 copy_tv(&li->li_tv, rettv);
13188 else
13189 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013191 }
13192 else if (l != NULL)
13193 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194 else
13195 {
13196 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013197 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198 (varnumber_T)(regmatch.startp[0] - str);
13199 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013200 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013202 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203 }
13204 }
13205 vim_free(regmatch.regprog);
13206 }
13207
13208theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013209 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210 p_cpo = save_cpo;
13211}
13212
13213/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013214 * "match()" function
13215 */
13216 static void
13217f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013218 typval_T *argvars;
13219 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013220{
13221 find_some_match(argvars, rettv, 1);
13222}
13223
13224/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013225 * "matchadd()" function
13226 */
13227 static void
13228f_matchadd(argvars, rettv)
13229 typval_T *argvars;
13230 typval_T *rettv;
13231{
13232#ifdef FEAT_SEARCH_EXTRA
13233 char_u buf[NUMBUFLEN];
13234 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13235 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13236 int prio = 10; /* default priority */
13237 int id = -1;
13238 int error = FALSE;
13239
13240 rettv->vval.v_number = -1;
13241
13242 if (grp == NULL || pat == NULL)
13243 return;
13244 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013245 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013246 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013247 if (argvars[3].v_type != VAR_UNKNOWN)
13248 id = get_tv_number_chk(&argvars[3], &error);
13249 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013250 if (error == TRUE)
13251 return;
13252 if (id >= 1 && id <= 3)
13253 {
13254 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13255 return;
13256 }
13257
13258 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13259#endif
13260}
13261
13262/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013263 * "matcharg()" function
13264 */
13265 static void
13266f_matcharg(argvars, rettv)
13267 typval_T *argvars;
13268 typval_T *rettv;
13269{
13270 if (rettv_list_alloc(rettv) == OK)
13271 {
13272#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013273 int id = get_tv_number(&argvars[0]);
13274 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013275
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013276 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013277 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013278 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13279 {
13280 list_append_string(rettv->vval.v_list,
13281 syn_id2name(m->hlg_id), -1);
13282 list_append_string(rettv->vval.v_list, m->pattern, -1);
13283 }
13284 else
13285 {
13286 list_append_string(rettv->vval.v_list, NUL, -1);
13287 list_append_string(rettv->vval.v_list, NUL, -1);
13288 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013289 }
13290#endif
13291 }
13292}
13293
13294/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013295 * "matchdelete()" function
13296 */
13297 static void
13298f_matchdelete(argvars, rettv)
13299 typval_T *argvars;
13300 typval_T *rettv;
13301{
13302#ifdef FEAT_SEARCH_EXTRA
13303 rettv->vval.v_number = match_delete(curwin,
13304 (int)get_tv_number(&argvars[0]), TRUE);
13305#endif
13306}
13307
13308/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013309 * "matchend()" function
13310 */
13311 static void
13312f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013313 typval_T *argvars;
13314 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013315{
13316 find_some_match(argvars, rettv, 0);
13317}
13318
13319/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013320 * "matchlist()" function
13321 */
13322 static void
13323f_matchlist(argvars, rettv)
13324 typval_T *argvars;
13325 typval_T *rettv;
13326{
13327 find_some_match(argvars, rettv, 3);
13328}
13329
13330/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013331 * "matchstr()" function
13332 */
13333 static void
13334f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013335 typval_T *argvars;
13336 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013337{
13338 find_some_match(argvars, rettv, 2);
13339}
13340
Bram Moolenaar33570922005-01-25 22:26:29 +000013341static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013342
13343 static void
13344max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013345 typval_T *argvars;
13346 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013347 int domax;
13348{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013349 long n = 0;
13350 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013351 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013352
13353 if (argvars[0].v_type == VAR_LIST)
13354 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013355 list_T *l;
13356 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013357
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013358 l = argvars[0].vval.v_list;
13359 if (l != NULL)
13360 {
13361 li = l->lv_first;
13362 if (li != NULL)
13363 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013364 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013365 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013366 {
13367 li = li->li_next;
13368 if (li == NULL)
13369 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013370 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013371 if (domax ? i > n : i < n)
13372 n = i;
13373 }
13374 }
13375 }
13376 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013377 else if (argvars[0].v_type == VAR_DICT)
13378 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013379 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013380 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013381 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013382 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013383
13384 d = argvars[0].vval.v_dict;
13385 if (d != NULL)
13386 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013387 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013388 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013389 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013390 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013391 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013392 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013393 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013394 if (first)
13395 {
13396 n = i;
13397 first = FALSE;
13398 }
13399 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013400 n = i;
13401 }
13402 }
13403 }
13404 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013405 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013406 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013407 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013408}
13409
13410/*
13411 * "max()" function
13412 */
13413 static void
13414f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013415 typval_T *argvars;
13416 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013417{
13418 max_min(argvars, rettv, TRUE);
13419}
13420
13421/*
13422 * "min()" function
13423 */
13424 static void
13425f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013426 typval_T *argvars;
13427 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013428{
13429 max_min(argvars, rettv, FALSE);
13430}
13431
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013432static int mkdir_recurse __ARGS((char_u *dir, int prot));
13433
13434/*
13435 * Create the directory in which "dir" is located, and higher levels when
13436 * needed.
13437 */
13438 static int
13439mkdir_recurse(dir, prot)
13440 char_u *dir;
13441 int prot;
13442{
13443 char_u *p;
13444 char_u *updir;
13445 int r = FAIL;
13446
13447 /* Get end of directory name in "dir".
13448 * We're done when it's "/" or "c:/". */
13449 p = gettail_sep(dir);
13450 if (p <= get_past_head(dir))
13451 return OK;
13452
13453 /* If the directory exists we're done. Otherwise: create it.*/
13454 updir = vim_strnsave(dir, (int)(p - dir));
13455 if (updir == NULL)
13456 return FAIL;
13457 if (mch_isdir(updir))
13458 r = OK;
13459 else if (mkdir_recurse(updir, prot) == OK)
13460 r = vim_mkdir_emsg(updir, prot);
13461 vim_free(updir);
13462 return r;
13463}
13464
13465#ifdef vim_mkdir
13466/*
13467 * "mkdir()" function
13468 */
13469 static void
13470f_mkdir(argvars, rettv)
13471 typval_T *argvars;
13472 typval_T *rettv;
13473{
13474 char_u *dir;
13475 char_u buf[NUMBUFLEN];
13476 int prot = 0755;
13477
13478 rettv->vval.v_number = FAIL;
13479 if (check_restricted() || check_secure())
13480 return;
13481
13482 dir = get_tv_string_buf(&argvars[0], buf);
13483 if (argvars[1].v_type != VAR_UNKNOWN)
13484 {
13485 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013486 prot = get_tv_number_chk(&argvars[2], NULL);
13487 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013488 mkdir_recurse(dir, prot);
13489 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013490 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013491}
13492#endif
13493
Bram Moolenaar0d660222005-01-07 21:51:51 +000013494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495 * "mode()" function
13496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013498f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013499 typval_T *argvars;
13500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013502 char_u buf[3];
13503
13504 buf[1] = NUL;
13505 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506
13507#ifdef FEAT_VISUAL
13508 if (VIsual_active)
13509 {
13510 if (VIsual_select)
13511 buf[0] = VIsual_mode + 's' - 'v';
13512 else
13513 buf[0] = VIsual_mode;
13514 }
13515 else
13516#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013517 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13518 || State == CONFIRM)
13519 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013521 if (State == ASKMORE)
13522 buf[1] = 'm';
13523 else if (State == CONFIRM)
13524 buf[1] = '?';
13525 }
13526 else if (State == EXTERNCMD)
13527 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528 else if (State & INSERT)
13529 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013530#ifdef FEAT_VREPLACE
13531 if (State & VREPLACE_FLAG)
13532 {
13533 buf[0] = 'R';
13534 buf[1] = 'v';
13535 }
13536 else
13537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538 if (State & REPLACE_FLAG)
13539 buf[0] = 'R';
13540 else
13541 buf[0] = 'i';
13542 }
13543 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013544 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013546 if (exmode_active)
13547 buf[1] = 'v';
13548 }
13549 else if (exmode_active)
13550 {
13551 buf[0] = 'c';
13552 buf[1] = 'e';
13553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013555 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013557 if (finish_op)
13558 buf[1] = 'o';
13559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013561 /* Clear out the minor mode when the argument is not a non-zero number or
13562 * non-empty string. */
13563 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013564 buf[1] = NUL;
13565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013566 rettv->vval.v_string = vim_strsave(buf);
13567 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568}
13569
13570/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013571 * "nextnonblank()" function
13572 */
13573 static void
13574f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013575 typval_T *argvars;
13576 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013577{
13578 linenr_T lnum;
13579
13580 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13581 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013582 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013583 {
13584 lnum = 0;
13585 break;
13586 }
13587 if (*skipwhite(ml_get(lnum)) != NUL)
13588 break;
13589 }
13590 rettv->vval.v_number = lnum;
13591}
13592
13593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594 * "nr2char()" function
13595 */
13596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013598 typval_T *argvars;
13599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600{
13601 char_u buf[NUMBUFLEN];
13602
13603#ifdef FEAT_MBYTE
13604 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606 else
13607#endif
13608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013609 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610 buf[1] = NUL;
13611 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013612 rettv->v_type = VAR_STRING;
13613 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013614}
13615
13616/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013617 * "pathshorten()" function
13618 */
13619 static void
13620f_pathshorten(argvars, rettv)
13621 typval_T *argvars;
13622 typval_T *rettv;
13623{
13624 char_u *p;
13625
13626 rettv->v_type = VAR_STRING;
13627 p = get_tv_string_chk(&argvars[0]);
13628 if (p == NULL)
13629 rettv->vval.v_string = NULL;
13630 else
13631 {
13632 p = vim_strsave(p);
13633 rettv->vval.v_string = p;
13634 if (p != NULL)
13635 shorten_dir(p);
13636 }
13637}
13638
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013639#ifdef FEAT_FLOAT
13640/*
13641 * "pow()" function
13642 */
13643 static void
13644f_pow(argvars, rettv)
13645 typval_T *argvars;
13646 typval_T *rettv;
13647{
13648 float_T fx, fy;
13649
13650 rettv->v_type = VAR_FLOAT;
13651 if (get_float_arg(argvars, &fx) == OK
13652 && get_float_arg(&argvars[1], &fy) == OK)
13653 rettv->vval.v_float = pow(fx, fy);
13654 else
13655 rettv->vval.v_float = 0.0;
13656}
13657#endif
13658
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013659/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013660 * "prevnonblank()" function
13661 */
13662 static void
13663f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013664 typval_T *argvars;
13665 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013666{
13667 linenr_T lnum;
13668
13669 lnum = get_tv_lnum(argvars);
13670 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13671 lnum = 0;
13672 else
13673 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13674 --lnum;
13675 rettv->vval.v_number = lnum;
13676}
13677
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013678#ifdef HAVE_STDARG_H
13679/* This dummy va_list is here because:
13680 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13681 * - locally in the function results in a "used before set" warning
13682 * - using va_start() to initialize it gives "function with fixed args" error */
13683static va_list ap;
13684#endif
13685
Bram Moolenaar8c711452005-01-14 21:53:12 +000013686/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013687 * "printf()" function
13688 */
13689 static void
13690f_printf(argvars, rettv)
13691 typval_T *argvars;
13692 typval_T *rettv;
13693{
13694 rettv->v_type = VAR_STRING;
13695 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013696#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013697 {
13698 char_u buf[NUMBUFLEN];
13699 int len;
13700 char_u *s;
13701 int saved_did_emsg = did_emsg;
13702 char *fmt;
13703
13704 /* Get the required length, allocate the buffer and do it for real. */
13705 did_emsg = FALSE;
13706 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013707 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013708 if (!did_emsg)
13709 {
13710 s = alloc(len + 1);
13711 if (s != NULL)
13712 {
13713 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013714 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013715 }
13716 }
13717 did_emsg |= saved_did_emsg;
13718 }
13719#endif
13720}
13721
13722/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013723 * "pumvisible()" function
13724 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013725 static void
13726f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013727 typval_T *argvars UNUSED;
13728 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013729{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013730#ifdef FEAT_INS_EXPAND
13731 if (pum_visible())
13732 rettv->vval.v_number = 1;
13733#endif
13734}
13735
13736/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013737 * "range()" function
13738 */
13739 static void
13740f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013741 typval_T *argvars;
13742 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013743{
13744 long start;
13745 long end;
13746 long stride = 1;
13747 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013748 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013750 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013751 if (argvars[1].v_type == VAR_UNKNOWN)
13752 {
13753 end = start - 1;
13754 start = 0;
13755 }
13756 else
13757 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013758 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013759 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013760 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013761 }
13762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013763 if (error)
13764 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013765 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013766 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013767 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013768 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013769 else
13770 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013771 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013772 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013773 if (list_append_number(rettv->vval.v_list,
13774 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013775 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013776 }
13777}
13778
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013779/*
13780 * "readfile()" function
13781 */
13782 static void
13783f_readfile(argvars, rettv)
13784 typval_T *argvars;
13785 typval_T *rettv;
13786{
13787 int binary = FALSE;
13788 char_u *fname;
13789 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013790 listitem_T *li;
13791#define FREAD_SIZE 200 /* optimized for text lines */
13792 char_u buf[FREAD_SIZE];
13793 int readlen; /* size of last fread() */
13794 int buflen; /* nr of valid chars in buf[] */
13795 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13796 int tolist; /* first byte in buf[] still to be put in list */
13797 int chop; /* how many CR to chop off */
13798 char_u *prev = NULL; /* previously read bytes, if any */
13799 int prevlen = 0; /* length of "prev" if not NULL */
13800 char_u *s;
13801 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013802 long maxline = MAXLNUM;
13803 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013804
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013805 if (argvars[1].v_type != VAR_UNKNOWN)
13806 {
13807 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13808 binary = TRUE;
13809 if (argvars[2].v_type != VAR_UNKNOWN)
13810 maxline = get_tv_number(&argvars[2]);
13811 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013812
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013813 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013814 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013815
13816 /* Always open the file in binary mode, library functions have a mind of
13817 * their own about CR-LF conversion. */
13818 fname = get_tv_string(&argvars[0]);
13819 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13820 {
13821 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13822 return;
13823 }
13824
13825 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013826 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013827 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013828 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013829 buflen = filtd + readlen;
13830 tolist = 0;
13831 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13832 {
13833 if (buf[filtd] == '\n' || readlen <= 0)
13834 {
13835 /* Only when in binary mode add an empty list item when the
13836 * last line ends in a '\n'. */
13837 if (!binary && readlen == 0 && filtd == 0)
13838 break;
13839
13840 /* Found end-of-line or end-of-file: add a text line to the
13841 * list. */
13842 chop = 0;
13843 if (!binary)
13844 while (filtd - chop - 1 >= tolist
13845 && buf[filtd - chop - 1] == '\r')
13846 ++chop;
13847 len = filtd - tolist - chop;
13848 if (prev == NULL)
13849 s = vim_strnsave(buf + tolist, len);
13850 else
13851 {
13852 s = alloc((unsigned)(prevlen + len + 1));
13853 if (s != NULL)
13854 {
13855 mch_memmove(s, prev, prevlen);
13856 vim_free(prev);
13857 prev = NULL;
13858 mch_memmove(s + prevlen, buf + tolist, len);
13859 s[prevlen + len] = NUL;
13860 }
13861 }
13862 tolist = filtd + 1;
13863
13864 li = listitem_alloc();
13865 if (li == NULL)
13866 {
13867 vim_free(s);
13868 break;
13869 }
13870 li->li_tv.v_type = VAR_STRING;
13871 li->li_tv.v_lock = 0;
13872 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013873 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013874
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013875 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013876 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013877 if (readlen <= 0)
13878 break;
13879 }
13880 else if (buf[filtd] == NUL)
13881 buf[filtd] = '\n';
13882 }
13883 if (readlen <= 0)
13884 break;
13885
13886 if (tolist == 0)
13887 {
13888 /* "buf" is full, need to move text to an allocated buffer */
13889 if (prev == NULL)
13890 {
13891 prev = vim_strnsave(buf, buflen);
13892 prevlen = buflen;
13893 }
13894 else
13895 {
13896 s = alloc((unsigned)(prevlen + buflen));
13897 if (s != NULL)
13898 {
13899 mch_memmove(s, prev, prevlen);
13900 mch_memmove(s + prevlen, buf, buflen);
13901 vim_free(prev);
13902 prev = s;
13903 prevlen += buflen;
13904 }
13905 }
13906 filtd = 0;
13907 }
13908 else
13909 {
13910 mch_memmove(buf, buf + tolist, buflen - tolist);
13911 filtd -= tolist;
13912 }
13913 }
13914
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013915 /*
13916 * For a negative line count use only the lines at the end of the file,
13917 * free the rest.
13918 */
13919 if (maxline < 0)
13920 while (cnt > -maxline)
13921 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013922 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013923 --cnt;
13924 }
13925
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013926 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013927 fclose(fd);
13928}
13929
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013930#if defined(FEAT_RELTIME)
13931static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13932
13933/*
13934 * Convert a List to proftime_T.
13935 * Return FAIL when there is something wrong.
13936 */
13937 static int
13938list2proftime(arg, tm)
13939 typval_T *arg;
13940 proftime_T *tm;
13941{
13942 long n1, n2;
13943 int error = FALSE;
13944
13945 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13946 || arg->vval.v_list->lv_len != 2)
13947 return FAIL;
13948 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13949 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13950# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013951 tm->HighPart = n1;
13952 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013953# else
13954 tm->tv_sec = n1;
13955 tm->tv_usec = n2;
13956# endif
13957 return error ? FAIL : OK;
13958}
13959#endif /* FEAT_RELTIME */
13960
13961/*
13962 * "reltime()" function
13963 */
13964 static void
13965f_reltime(argvars, rettv)
13966 typval_T *argvars;
13967 typval_T *rettv;
13968{
13969#ifdef FEAT_RELTIME
13970 proftime_T res;
13971 proftime_T start;
13972
13973 if (argvars[0].v_type == VAR_UNKNOWN)
13974 {
13975 /* No arguments: get current time. */
13976 profile_start(&res);
13977 }
13978 else if (argvars[1].v_type == VAR_UNKNOWN)
13979 {
13980 if (list2proftime(&argvars[0], &res) == FAIL)
13981 return;
13982 profile_end(&res);
13983 }
13984 else
13985 {
13986 /* Two arguments: compute the difference. */
13987 if (list2proftime(&argvars[0], &start) == FAIL
13988 || list2proftime(&argvars[1], &res) == FAIL)
13989 return;
13990 profile_sub(&res, &start);
13991 }
13992
13993 if (rettv_list_alloc(rettv) == OK)
13994 {
13995 long n1, n2;
13996
13997# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013998 n1 = res.HighPart;
13999 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014000# else
14001 n1 = res.tv_sec;
14002 n2 = res.tv_usec;
14003# endif
14004 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14005 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14006 }
14007#endif
14008}
14009
14010/*
14011 * "reltimestr()" function
14012 */
14013 static void
14014f_reltimestr(argvars, rettv)
14015 typval_T *argvars;
14016 typval_T *rettv;
14017{
14018#ifdef FEAT_RELTIME
14019 proftime_T tm;
14020#endif
14021
14022 rettv->v_type = VAR_STRING;
14023 rettv->vval.v_string = NULL;
14024#ifdef FEAT_RELTIME
14025 if (list2proftime(&argvars[0], &tm) == OK)
14026 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14027#endif
14028}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014029
Bram Moolenaar0d660222005-01-07 21:51:51 +000014030#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14031static void make_connection __ARGS((void));
14032static int check_connection __ARGS((void));
14033
14034 static void
14035make_connection()
14036{
14037 if (X_DISPLAY == NULL
14038# ifdef FEAT_GUI
14039 && !gui.in_use
14040# endif
14041 )
14042 {
14043 x_force_connect = TRUE;
14044 setup_term_clip();
14045 x_force_connect = FALSE;
14046 }
14047}
14048
14049 static int
14050check_connection()
14051{
14052 make_connection();
14053 if (X_DISPLAY == NULL)
14054 {
14055 EMSG(_("E240: No connection to Vim server"));
14056 return FAIL;
14057 }
14058 return OK;
14059}
14060#endif
14061
14062#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014063static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014064
14065 static void
14066remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014067 typval_T *argvars;
14068 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014069 int expr;
14070{
14071 char_u *server_name;
14072 char_u *keys;
14073 char_u *r = NULL;
14074 char_u buf[NUMBUFLEN];
14075# ifdef WIN32
14076 HWND w;
14077# else
14078 Window w;
14079# endif
14080
14081 if (check_restricted() || check_secure())
14082 return;
14083
14084# ifdef FEAT_X11
14085 if (check_connection() == FAIL)
14086 return;
14087# endif
14088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014089 server_name = get_tv_string_chk(&argvars[0]);
14090 if (server_name == NULL)
14091 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014092 keys = get_tv_string_buf(&argvars[1], buf);
14093# ifdef WIN32
14094 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14095# else
14096 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14097 < 0)
14098# endif
14099 {
14100 if (r != NULL)
14101 EMSG(r); /* sending worked but evaluation failed */
14102 else
14103 EMSG2(_("E241: Unable to send to %s"), server_name);
14104 return;
14105 }
14106
14107 rettv->vval.v_string = r;
14108
14109 if (argvars[2].v_type != VAR_UNKNOWN)
14110 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014111 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014112 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014113 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014114
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014115 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014116 v.di_tv.v_type = VAR_STRING;
14117 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014118 idvar = get_tv_string_chk(&argvars[2]);
14119 if (idvar != NULL)
14120 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014121 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014122 }
14123}
14124#endif
14125
14126/*
14127 * "remote_expr()" function
14128 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014129 static void
14130f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014131 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014132 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014133{
14134 rettv->v_type = VAR_STRING;
14135 rettv->vval.v_string = NULL;
14136#ifdef FEAT_CLIENTSERVER
14137 remote_common(argvars, rettv, TRUE);
14138#endif
14139}
14140
14141/*
14142 * "remote_foreground()" function
14143 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014144 static void
14145f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014146 typval_T *argvars UNUSED;
14147 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014148{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014149#ifdef FEAT_CLIENTSERVER
14150# ifdef WIN32
14151 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014152 {
14153 char_u *server_name = get_tv_string_chk(&argvars[0]);
14154
14155 if (server_name != NULL)
14156 serverForeground(server_name);
14157 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014158# else
14159 /* Send a foreground() expression to the server. */
14160 argvars[1].v_type = VAR_STRING;
14161 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14162 argvars[2].v_type = VAR_UNKNOWN;
14163 remote_common(argvars, rettv, TRUE);
14164 vim_free(argvars[1].vval.v_string);
14165# endif
14166#endif
14167}
14168
Bram Moolenaar0d660222005-01-07 21:51:51 +000014169 static void
14170f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014171 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014172 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014173{
14174#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014175 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014176 char_u *s = NULL;
14177# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014178 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014179# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014181
14182 if (check_restricted() || check_secure())
14183 {
14184 rettv->vval.v_number = -1;
14185 return;
14186 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014187 serverid = get_tv_string_chk(&argvars[0]);
14188 if (serverid == NULL)
14189 {
14190 rettv->vval.v_number = -1;
14191 return; /* type error; errmsg already given */
14192 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014193# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014194 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014195 if (n == 0)
14196 rettv->vval.v_number = -1;
14197 else
14198 {
14199 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14200 rettv->vval.v_number = (s != NULL);
14201 }
14202# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014203 if (check_connection() == FAIL)
14204 return;
14205
14206 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014207 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014208# endif
14209
14210 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14211 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014212 char_u *retvar;
14213
Bram Moolenaar33570922005-01-25 22:26:29 +000014214 v.di_tv.v_type = VAR_STRING;
14215 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014216 retvar = get_tv_string_chk(&argvars[1]);
14217 if (retvar != NULL)
14218 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014219 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014220 }
14221#else
14222 rettv->vval.v_number = -1;
14223#endif
14224}
14225
Bram Moolenaar0d660222005-01-07 21:51:51 +000014226 static void
14227f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014228 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014229 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014230{
14231 char_u *r = NULL;
14232
14233#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014234 char_u *serverid = get_tv_string_chk(&argvars[0]);
14235
14236 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014237 {
14238# ifdef WIN32
14239 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014240 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014241
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014242 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014243 if (n != 0)
14244 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14245 if (r == NULL)
14246# else
14247 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014249# endif
14250 EMSG(_("E277: Unable to read a server reply"));
14251 }
14252#endif
14253 rettv->v_type = VAR_STRING;
14254 rettv->vval.v_string = r;
14255}
14256
14257/*
14258 * "remote_send()" function
14259 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014260 static void
14261f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014262 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014263 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014264{
14265 rettv->v_type = VAR_STRING;
14266 rettv->vval.v_string = NULL;
14267#ifdef FEAT_CLIENTSERVER
14268 remote_common(argvars, rettv, FALSE);
14269#endif
14270}
14271
14272/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014273 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014274 */
14275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014276f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014277 typval_T *argvars;
14278 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014279{
Bram Moolenaar33570922005-01-25 22:26:29 +000014280 list_T *l;
14281 listitem_T *item, *item2;
14282 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014283 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014284 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014285 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014286 dict_T *d;
14287 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014288
Bram Moolenaar8c711452005-01-14 21:53:12 +000014289 if (argvars[0].v_type == VAR_DICT)
14290 {
14291 if (argvars[2].v_type != VAR_UNKNOWN)
14292 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014293 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014294 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014296 key = get_tv_string_chk(&argvars[1]);
14297 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014299 di = dict_find(d, key, -1);
14300 if (di == NULL)
14301 EMSG2(_(e_dictkey), key);
14302 else
14303 {
14304 *rettv = di->di_tv;
14305 init_tv(&di->di_tv);
14306 dictitem_remove(d, di);
14307 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014308 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014309 }
14310 }
14311 else if (argvars[0].v_type != VAR_LIST)
14312 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014313 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014314 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014315 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014316 int error = FALSE;
14317
14318 idx = get_tv_number_chk(&argvars[1], &error);
14319 if (error)
14320 ; /* type error: do nothing, errmsg already given */
14321 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014322 EMSGN(_(e_listidx), idx);
14323 else
14324 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014325 if (argvars[2].v_type == VAR_UNKNOWN)
14326 {
14327 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014328 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014329 *rettv = item->li_tv;
14330 vim_free(item);
14331 }
14332 else
14333 {
14334 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014335 end = get_tv_number_chk(&argvars[2], &error);
14336 if (error)
14337 ; /* type error: do nothing */
14338 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014339 EMSGN(_(e_listidx), end);
14340 else
14341 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014342 int cnt = 0;
14343
14344 for (li = item; li != NULL; li = li->li_next)
14345 {
14346 ++cnt;
14347 if (li == item2)
14348 break;
14349 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014350 if (li == NULL) /* didn't find "item2" after "item" */
14351 EMSG(_(e_invrange));
14352 else
14353 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014354 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014355 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014356 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014357 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014358 l->lv_first = item;
14359 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014360 item->li_prev = NULL;
14361 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014362 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014363 }
14364 }
14365 }
14366 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014367 }
14368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369}
14370
14371/*
14372 * "rename({from}, {to})" function
14373 */
14374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014375f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014376 typval_T *argvars;
14377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378{
14379 char_u buf[NUMBUFLEN];
14380
14381 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014382 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014384 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14385 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386}
14387
14388/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014389 * "repeat()" function
14390 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014391 static void
14392f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014393 typval_T *argvars;
14394 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014395{
14396 char_u *p;
14397 int n;
14398 int slen;
14399 int len;
14400 char_u *r;
14401 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014402
14403 n = get_tv_number(&argvars[1]);
14404 if (argvars[0].v_type == VAR_LIST)
14405 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014406 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014407 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014408 if (list_extend(rettv->vval.v_list,
14409 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014410 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014411 }
14412 else
14413 {
14414 p = get_tv_string(&argvars[0]);
14415 rettv->v_type = VAR_STRING;
14416 rettv->vval.v_string = NULL;
14417
14418 slen = (int)STRLEN(p);
14419 len = slen * n;
14420 if (len <= 0)
14421 return;
14422
14423 r = alloc(len + 1);
14424 if (r != NULL)
14425 {
14426 for (i = 0; i < n; i++)
14427 mch_memmove(r + i * slen, p, (size_t)slen);
14428 r[len] = NUL;
14429 }
14430
14431 rettv->vval.v_string = r;
14432 }
14433}
14434
14435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 * "resolve()" function
14437 */
14438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014439f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014440 typval_T *argvars;
14441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442{
14443 char_u *p;
14444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014445 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446#ifdef FEAT_SHORTCUT
14447 {
14448 char_u *v = NULL;
14449
14450 v = mch_resolve_shortcut(p);
14451 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014452 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014454 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455 }
14456#else
14457# ifdef HAVE_READLINK
14458 {
14459 char_u buf[MAXPATHL + 1];
14460 char_u *cpy;
14461 int len;
14462 char_u *remain = NULL;
14463 char_u *q;
14464 int is_relative_to_current = FALSE;
14465 int has_trailing_pathsep = FALSE;
14466 int limit = 100;
14467
14468 p = vim_strsave(p);
14469
14470 if (p[0] == '.' && (vim_ispathsep(p[1])
14471 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14472 is_relative_to_current = TRUE;
14473
14474 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014475 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476 has_trailing_pathsep = TRUE;
14477
14478 q = getnextcomp(p);
14479 if (*q != NUL)
14480 {
14481 /* Separate the first path component in "p", and keep the
14482 * remainder (beginning with the path separator). */
14483 remain = vim_strsave(q - 1);
14484 q[-1] = NUL;
14485 }
14486
14487 for (;;)
14488 {
14489 for (;;)
14490 {
14491 len = readlink((char *)p, (char *)buf, MAXPATHL);
14492 if (len <= 0)
14493 break;
14494 buf[len] = NUL;
14495
14496 if (limit-- == 0)
14497 {
14498 vim_free(p);
14499 vim_free(remain);
14500 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014501 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 goto fail;
14503 }
14504
14505 /* Ensure that the result will have a trailing path separator
14506 * if the argument has one. */
14507 if (remain == NULL && has_trailing_pathsep)
14508 add_pathsep(buf);
14509
14510 /* Separate the first path component in the link value and
14511 * concatenate the remainders. */
14512 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14513 if (*q != NUL)
14514 {
14515 if (remain == NULL)
14516 remain = vim_strsave(q - 1);
14517 else
14518 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014519 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520 if (cpy != NULL)
14521 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522 vim_free(remain);
14523 remain = cpy;
14524 }
14525 }
14526 q[-1] = NUL;
14527 }
14528
14529 q = gettail(p);
14530 if (q > p && *q == NUL)
14531 {
14532 /* Ignore trailing path separator. */
14533 q[-1] = NUL;
14534 q = gettail(p);
14535 }
14536 if (q > p && !mch_isFullName(buf))
14537 {
14538 /* symlink is relative to directory of argument */
14539 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14540 if (cpy != NULL)
14541 {
14542 STRCPY(cpy, p);
14543 STRCPY(gettail(cpy), buf);
14544 vim_free(p);
14545 p = cpy;
14546 }
14547 }
14548 else
14549 {
14550 vim_free(p);
14551 p = vim_strsave(buf);
14552 }
14553 }
14554
14555 if (remain == NULL)
14556 break;
14557
14558 /* Append the first path component of "remain" to "p". */
14559 q = getnextcomp(remain + 1);
14560 len = q - remain - (*q != NUL);
14561 cpy = vim_strnsave(p, STRLEN(p) + len);
14562 if (cpy != NULL)
14563 {
14564 STRNCAT(cpy, remain, len);
14565 vim_free(p);
14566 p = cpy;
14567 }
14568 /* Shorten "remain". */
14569 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014570 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014571 else
14572 {
14573 vim_free(remain);
14574 remain = NULL;
14575 }
14576 }
14577
14578 /* If the result is a relative path name, make it explicitly relative to
14579 * the current directory if and only if the argument had this form. */
14580 if (!vim_ispathsep(*p))
14581 {
14582 if (is_relative_to_current
14583 && *p != NUL
14584 && !(p[0] == '.'
14585 && (p[1] == NUL
14586 || vim_ispathsep(p[1])
14587 || (p[1] == '.'
14588 && (p[2] == NUL
14589 || vim_ispathsep(p[2]))))))
14590 {
14591 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014592 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593 if (cpy != NULL)
14594 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595 vim_free(p);
14596 p = cpy;
14597 }
14598 }
14599 else if (!is_relative_to_current)
14600 {
14601 /* Strip leading "./". */
14602 q = p;
14603 while (q[0] == '.' && vim_ispathsep(q[1]))
14604 q += 2;
14605 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014606 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607 }
14608 }
14609
14610 /* Ensure that the result will have no trailing path separator
14611 * if the argument had none. But keep "/" or "//". */
14612 if (!has_trailing_pathsep)
14613 {
14614 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014615 if (after_pathsep(p, q))
14616 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617 }
14618
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014619 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620 }
14621# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014622 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623# endif
14624#endif
14625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014626 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627
14628#ifdef HAVE_READLINK
14629fail:
14630#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014631 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632}
14633
14634/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014635 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636 */
14637 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014638f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014639 typval_T *argvars;
14640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641{
Bram Moolenaar33570922005-01-25 22:26:29 +000014642 list_T *l;
14643 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644
Bram Moolenaar0d660222005-01-07 21:51:51 +000014645 if (argvars[0].v_type != VAR_LIST)
14646 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014647 else if ((l = argvars[0].vval.v_list) != NULL
14648 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014649 {
14650 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014651 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014652 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014653 while (li != NULL)
14654 {
14655 ni = li->li_prev;
14656 list_append(l, li);
14657 li = ni;
14658 }
14659 rettv->vval.v_list = l;
14660 rettv->v_type = VAR_LIST;
14661 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014662 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664}
14665
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014666#define SP_NOMOVE 0x01 /* don't move cursor */
14667#define SP_REPEAT 0x02 /* repeat to find outer pair */
14668#define SP_RETCOUNT 0x04 /* return matchcount */
14669#define SP_SETPCMARK 0x08 /* set previous context mark */
14670#define SP_START 0x10 /* accept match at start position */
14671#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14672#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014673
Bram Moolenaar33570922005-01-25 22:26:29 +000014674static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014675
14676/*
14677 * Get flags for a search function.
14678 * Possibly sets "p_ws".
14679 * Returns BACKWARD, FORWARD or zero (for an error).
14680 */
14681 static int
14682get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014683 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014684 int *flagsp;
14685{
14686 int dir = FORWARD;
14687 char_u *flags;
14688 char_u nbuf[NUMBUFLEN];
14689 int mask;
14690
14691 if (varp->v_type != VAR_UNKNOWN)
14692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014693 flags = get_tv_string_buf_chk(varp, nbuf);
14694 if (flags == NULL)
14695 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014696 while (*flags != NUL)
14697 {
14698 switch (*flags)
14699 {
14700 case 'b': dir = BACKWARD; break;
14701 case 'w': p_ws = TRUE; break;
14702 case 'W': p_ws = FALSE; break;
14703 default: mask = 0;
14704 if (flagsp != NULL)
14705 switch (*flags)
14706 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014707 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014708 case 'e': mask = SP_END; break;
14709 case 'm': mask = SP_RETCOUNT; break;
14710 case 'n': mask = SP_NOMOVE; break;
14711 case 'p': mask = SP_SUBPAT; break;
14712 case 'r': mask = SP_REPEAT; break;
14713 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014714 }
14715 if (mask == 0)
14716 {
14717 EMSG2(_(e_invarg2), flags);
14718 dir = 0;
14719 }
14720 else
14721 *flagsp |= mask;
14722 }
14723 if (dir == 0)
14724 break;
14725 ++flags;
14726 }
14727 }
14728 return dir;
14729}
14730
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014732 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014734 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014735search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014736 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014737 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014738 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014740 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741 char_u *pat;
14742 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014743 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744 int save_p_ws = p_ws;
14745 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014746 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014747 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014748 proftime_T tm;
14749#ifdef FEAT_RELTIME
14750 long time_limit = 0;
14751#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014752 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014753 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014755 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014756 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014757 if (dir == 0)
14758 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014759 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014760 if (flags & SP_START)
14761 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014762 if (flags & SP_END)
14763 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014764
Bram Moolenaar76929292008-01-06 19:07:36 +000014765 /* Optional arguments: line number to stop searching and timeout. */
14766 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014767 {
14768 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14769 if (lnum_stop < 0)
14770 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014771#ifdef FEAT_RELTIME
14772 if (argvars[3].v_type != VAR_UNKNOWN)
14773 {
14774 time_limit = get_tv_number_chk(&argvars[3], NULL);
14775 if (time_limit < 0)
14776 goto theend;
14777 }
14778#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014779 }
14780
Bram Moolenaar76929292008-01-06 19:07:36 +000014781#ifdef FEAT_RELTIME
14782 /* Set the time limit, if there is one. */
14783 profile_setlimit(time_limit, &tm);
14784#endif
14785
Bram Moolenaar231334e2005-07-25 20:46:57 +000014786 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014787 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014788 * Check to make sure only those flags are set.
14789 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14790 * flags cannot be set. Check for that condition also.
14791 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014792 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014793 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014794 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014795 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014796 goto theend;
14797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014799 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014800 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014801 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014802 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014804 if (flags & SP_SUBPAT)
14805 retval = subpatnum;
14806 else
14807 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014808 if (flags & SP_SETPCMARK)
14809 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014811 if (match_pos != NULL)
14812 {
14813 /* Store the match cursor position */
14814 match_pos->lnum = pos.lnum;
14815 match_pos->col = pos.col + 1;
14816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817 /* "/$" will put the cursor after the end of the line, may need to
14818 * correct that here */
14819 check_cursor();
14820 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014821
14822 /* If 'n' flag is used: restore cursor position. */
14823 if (flags & SP_NOMOVE)
14824 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014825 else
14826 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014827theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014829
14830 return retval;
14831}
14832
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014833#ifdef FEAT_FLOAT
14834/*
14835 * "round({float})" function
14836 */
14837 static void
14838f_round(argvars, rettv)
14839 typval_T *argvars;
14840 typval_T *rettv;
14841{
14842 float_T f;
14843
14844 rettv->v_type = VAR_FLOAT;
14845 if (get_float_arg(argvars, &f) == OK)
14846 /* round() is not in C90, use ceil() or floor() instead. */
14847 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
14848 else
14849 rettv->vval.v_float = 0.0;
14850}
14851#endif
14852
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014853/*
14854 * "search()" function
14855 */
14856 static void
14857f_search(argvars, rettv)
14858 typval_T *argvars;
14859 typval_T *rettv;
14860{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014861 int flags = 0;
14862
14863 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864}
14865
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014867 * "searchdecl()" function
14868 */
14869 static void
14870f_searchdecl(argvars, rettv)
14871 typval_T *argvars;
14872 typval_T *rettv;
14873{
14874 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014875 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014876 int error = FALSE;
14877 char_u *name;
14878
14879 rettv->vval.v_number = 1; /* default: FAIL */
14880
14881 name = get_tv_string_chk(&argvars[0]);
14882 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014883 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014884 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014885 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14886 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14887 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014888 if (!error && name != NULL)
14889 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014890 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014891}
14892
14893/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014894 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014896 static int
14897searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014898 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014899 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014900{
14901 char_u *spat, *mpat, *epat;
14902 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014903 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904 int dir;
14905 int flags = 0;
14906 char_u nbuf1[NUMBUFLEN];
14907 char_u nbuf2[NUMBUFLEN];
14908 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014909 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014910 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014911 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912
Bram Moolenaar071d4272004-06-13 20:20:40 +000014913 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014914 spat = get_tv_string_chk(&argvars[0]);
14915 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14916 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14917 if (spat == NULL || mpat == NULL || epat == NULL)
14918 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919
Bram Moolenaar071d4272004-06-13 20:20:40 +000014920 /* Handle the optional fourth argument: flags */
14921 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014922 if (dir == 0)
14923 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014924
14925 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014926 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14927 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014928 if ((flags & (SP_END | SP_SUBPAT)) != 0
14929 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014930 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014931 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014932 goto theend;
14933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014934
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014935 /* Using 'r' implies 'W', otherwise it doesn't work. */
14936 if (flags & SP_REPEAT)
14937 p_ws = FALSE;
14938
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014939 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014940 if (argvars[3].v_type == VAR_UNKNOWN
14941 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942 skip = (char_u *)"";
14943 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014944 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014945 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014946 if (argvars[5].v_type != VAR_UNKNOWN)
14947 {
14948 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14949 if (lnum_stop < 0)
14950 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014951#ifdef FEAT_RELTIME
14952 if (argvars[6].v_type != VAR_UNKNOWN)
14953 {
14954 time_limit = get_tv_number_chk(&argvars[6], NULL);
14955 if (time_limit < 0)
14956 goto theend;
14957 }
14958#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014959 }
14960 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014961 if (skip == NULL)
14962 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014964 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000014965 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014966
14967theend:
14968 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014969
14970 return retval;
14971}
14972
14973/*
14974 * "searchpair()" function
14975 */
14976 static void
14977f_searchpair(argvars, rettv)
14978 typval_T *argvars;
14979 typval_T *rettv;
14980{
14981 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14982}
14983
14984/*
14985 * "searchpairpos()" function
14986 */
14987 static void
14988f_searchpairpos(argvars, rettv)
14989 typval_T *argvars;
14990 typval_T *rettv;
14991{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014992 pos_T match_pos;
14993 int lnum = 0;
14994 int col = 0;
14995
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014996 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014997 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014998
14999 if (searchpair_cmn(argvars, &match_pos) > 0)
15000 {
15001 lnum = match_pos.lnum;
15002 col = match_pos.col;
15003 }
15004
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015005 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15006 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015007}
15008
15009/*
15010 * Search for a start/middle/end thing.
15011 * Used by searchpair(), see its documentation for the details.
15012 * Returns 0 or -1 for no match,
15013 */
15014 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015015do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15016 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015017 char_u *spat; /* start pattern */
15018 char_u *mpat; /* middle pattern */
15019 char_u *epat; /* end pattern */
15020 int dir; /* BACKWARD or FORWARD */
15021 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015022 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015023 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015024 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015025 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015026{
15027 char_u *save_cpo;
15028 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15029 long retval = 0;
15030 pos_T pos;
15031 pos_T firstpos;
15032 pos_T foundpos;
15033 pos_T save_cursor;
15034 pos_T save_pos;
15035 int n;
15036 int r;
15037 int nest = 1;
15038 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015039 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015040 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015041
15042 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15043 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015044 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015045
Bram Moolenaar76929292008-01-06 19:07:36 +000015046#ifdef FEAT_RELTIME
15047 /* Set the time limit, if there is one. */
15048 profile_setlimit(time_limit, &tm);
15049#endif
15050
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015051 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15052 * start/middle/end (pat3, for the top pair). */
15053 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15054 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15055 if (pat2 == NULL || pat3 == NULL)
15056 goto theend;
15057 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15058 if (*mpat == NUL)
15059 STRCPY(pat3, pat2);
15060 else
15061 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15062 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015063 if (flags & SP_START)
15064 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015065
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066 save_cursor = curwin->w_cursor;
15067 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015068 clearpos(&firstpos);
15069 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070 pat = pat3;
15071 for (;;)
15072 {
15073 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015074 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15076 /* didn't find it or found the first match again: FAIL */
15077 break;
15078
15079 if (firstpos.lnum == 0)
15080 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015081 if (equalpos(pos, foundpos))
15082 {
15083 /* Found the same position again. Can happen with a pattern that
15084 * has "\zs" at the end and searching backwards. Advance one
15085 * character and try again. */
15086 if (dir == BACKWARD)
15087 decl(&pos);
15088 else
15089 incl(&pos);
15090 }
15091 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015092
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015093 /* clear the start flag to avoid getting stuck here */
15094 options &= ~SEARCH_START;
15095
Bram Moolenaar071d4272004-06-13 20:20:40 +000015096 /* If the skip pattern matches, ignore this match. */
15097 if (*skip != NUL)
15098 {
15099 save_pos = curwin->w_cursor;
15100 curwin->w_cursor = pos;
15101 r = eval_to_bool(skip, &err, NULL, FALSE);
15102 curwin->w_cursor = save_pos;
15103 if (err)
15104 {
15105 /* Evaluating {skip} caused an error, break here. */
15106 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015107 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108 break;
15109 }
15110 if (r)
15111 continue;
15112 }
15113
15114 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15115 {
15116 /* Found end when searching backwards or start when searching
15117 * forward: nested pair. */
15118 ++nest;
15119 pat = pat2; /* nested, don't search for middle */
15120 }
15121 else
15122 {
15123 /* Found end when searching forward or start when searching
15124 * backward: end of (nested) pair; or found middle in outer pair. */
15125 if (--nest == 1)
15126 pat = pat3; /* outer level, search for middle */
15127 }
15128
15129 if (nest == 0)
15130 {
15131 /* Found the match: return matchcount or line number. */
15132 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015133 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015134 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015135 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015136 if (flags & SP_SETPCMARK)
15137 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138 curwin->w_cursor = pos;
15139 if (!(flags & SP_REPEAT))
15140 break;
15141 nest = 1; /* search for next unmatched */
15142 }
15143 }
15144
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015145 if (match_pos != NULL)
15146 {
15147 /* Store the match cursor position */
15148 match_pos->lnum = curwin->w_cursor.lnum;
15149 match_pos->col = curwin->w_cursor.col + 1;
15150 }
15151
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015153 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015154 curwin->w_cursor = save_cursor;
15155
15156theend:
15157 vim_free(pat2);
15158 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015159 if (p_cpo == empty_option)
15160 p_cpo = save_cpo;
15161 else
15162 /* Darn, evaluating the {skip} expression changed the value. */
15163 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015164
15165 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015166}
15167
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015168/*
15169 * "searchpos()" function
15170 */
15171 static void
15172f_searchpos(argvars, rettv)
15173 typval_T *argvars;
15174 typval_T *rettv;
15175{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015176 pos_T match_pos;
15177 int lnum = 0;
15178 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015179 int n;
15180 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015181
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015182 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015183 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015184
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015185 n = search_cmn(argvars, &match_pos, &flags);
15186 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015187 {
15188 lnum = match_pos.lnum;
15189 col = match_pos.col;
15190 }
15191
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015192 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15193 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015194 if (flags & SP_SUBPAT)
15195 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015196}
15197
15198
Bram Moolenaar0d660222005-01-07 21:51:51 +000015199 static void
15200f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015201 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015204#ifdef FEAT_CLIENTSERVER
15205 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015206 char_u *server = get_tv_string_chk(&argvars[0]);
15207 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015208
Bram Moolenaar0d660222005-01-07 21:51:51 +000015209 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015210 if (server == NULL || reply == NULL)
15211 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015212 if (check_restricted() || check_secure())
15213 return;
15214# ifdef FEAT_X11
15215 if (check_connection() == FAIL)
15216 return;
15217# endif
15218
15219 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015220 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015221 EMSG(_("E258: Unable to send to client"));
15222 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015224 rettv->vval.v_number = 0;
15225#else
15226 rettv->vval.v_number = -1;
15227#endif
15228}
15229
Bram Moolenaar0d660222005-01-07 21:51:51 +000015230 static void
15231f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015232 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015233 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015234{
15235 char_u *r = NULL;
15236
15237#ifdef FEAT_CLIENTSERVER
15238# ifdef WIN32
15239 r = serverGetVimNames();
15240# else
15241 make_connection();
15242 if (X_DISPLAY != NULL)
15243 r = serverGetVimNames(X_DISPLAY);
15244# endif
15245#endif
15246 rettv->v_type = VAR_STRING;
15247 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015248}
15249
15250/*
15251 * "setbufvar()" function
15252 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015254f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015255 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015256 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257{
15258 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015261 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015262 char_u nbuf[NUMBUFLEN];
15263
15264 if (check_restricted() || check_secure())
15265 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015266 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15267 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015268 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015269 varp = &argvars[2];
15270
15271 if (buf != NULL && varname != NULL && varp != NULL)
15272 {
15273 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015275
15276 if (*varname == '&')
15277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015278 long numval;
15279 char_u *strval;
15280 int error = FALSE;
15281
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015283 numval = get_tv_number_chk(varp, &error);
15284 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015285 if (!error && strval != NULL)
15286 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287 }
15288 else
15289 {
15290 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15291 if (bufvarname != NULL)
15292 {
15293 STRCPY(bufvarname, "b:");
15294 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015295 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015296 vim_free(bufvarname);
15297 }
15298 }
15299
15300 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303}
15304
15305/*
15306 * "setcmdpos()" function
15307 */
15308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015309f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015310 typval_T *argvars;
15311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015313 int pos = (int)get_tv_number(&argvars[0]) - 1;
15314
15315 if (pos >= 0)
15316 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317}
15318
15319/*
15320 * "setline()" function
15321 */
15322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015323f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015324 typval_T *argvars;
15325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326{
15327 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015328 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015329 list_T *l = NULL;
15330 listitem_T *li = NULL;
15331 long added = 0;
15332 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015334 lnum = get_tv_lnum(&argvars[0]);
15335 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015337 l = argvars[1].vval.v_list;
15338 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015340 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015341 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015342
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015343 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015344 for (;;)
15345 {
15346 if (l != NULL)
15347 {
15348 /* list argument, get next string */
15349 if (li == NULL)
15350 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015351 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015352 li = li->li_next;
15353 }
15354
15355 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015356 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015357 break;
15358 if (lnum <= curbuf->b_ml.ml_line_count)
15359 {
15360 /* existing line, replace it */
15361 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15362 {
15363 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015364 if (lnum == curwin->w_cursor.lnum)
15365 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015366 rettv->vval.v_number = 0; /* OK */
15367 }
15368 }
15369 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15370 {
15371 /* lnum is one past the last line, append the line */
15372 ++added;
15373 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15374 rettv->vval.v_number = 0; /* OK */
15375 }
15376
15377 if (l == NULL) /* only one string argument */
15378 break;
15379 ++lnum;
15380 }
15381
15382 if (added > 0)
15383 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384}
15385
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015386static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15387
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015389 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015390 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015391 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015392set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015393 win_T *wp UNUSED;
15394 typval_T *list_arg UNUSED;
15395 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015396 typval_T *rettv;
15397{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015398#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015399 char_u *act;
15400 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015401#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015402
Bram Moolenaar2641f772005-03-25 21:58:17 +000015403 rettv->vval.v_number = -1;
15404
15405#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015406 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015407 EMSG(_(e_listreq));
15408 else
15409 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015410 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015411
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015412 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015413 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015414 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015415 if (act == NULL)
15416 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015417 if (*act == 'a' || *act == 'r')
15418 action = *act;
15419 }
15420
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015421 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015422 rettv->vval.v_number = 0;
15423 }
15424#endif
15425}
15426
15427/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015428 * "setloclist()" function
15429 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015430 static void
15431f_setloclist(argvars, rettv)
15432 typval_T *argvars;
15433 typval_T *rettv;
15434{
15435 win_T *win;
15436
15437 rettv->vval.v_number = -1;
15438
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015439 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015440 if (win != NULL)
15441 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15442}
15443
15444/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015445 * "setmatches()" function
15446 */
15447 static void
15448f_setmatches(argvars, rettv)
15449 typval_T *argvars;
15450 typval_T *rettv;
15451{
15452#ifdef FEAT_SEARCH_EXTRA
15453 list_T *l;
15454 listitem_T *li;
15455 dict_T *d;
15456
15457 rettv->vval.v_number = -1;
15458 if (argvars[0].v_type != VAR_LIST)
15459 {
15460 EMSG(_(e_listreq));
15461 return;
15462 }
15463 if ((l = argvars[0].vval.v_list) != NULL)
15464 {
15465
15466 /* To some extent make sure that we are dealing with a list from
15467 * "getmatches()". */
15468 li = l->lv_first;
15469 while (li != NULL)
15470 {
15471 if (li->li_tv.v_type != VAR_DICT
15472 || (d = li->li_tv.vval.v_dict) == NULL)
15473 {
15474 EMSG(_(e_invarg));
15475 return;
15476 }
15477 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15478 && dict_find(d, (char_u *)"pattern", -1) != NULL
15479 && dict_find(d, (char_u *)"priority", -1) != NULL
15480 && dict_find(d, (char_u *)"id", -1) != NULL))
15481 {
15482 EMSG(_(e_invarg));
15483 return;
15484 }
15485 li = li->li_next;
15486 }
15487
15488 clear_matches(curwin);
15489 li = l->lv_first;
15490 while (li != NULL)
15491 {
15492 d = li->li_tv.vval.v_dict;
15493 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15494 get_dict_string(d, (char_u *)"pattern", FALSE),
15495 (int)get_dict_number(d, (char_u *)"priority"),
15496 (int)get_dict_number(d, (char_u *)"id"));
15497 li = li->li_next;
15498 }
15499 rettv->vval.v_number = 0;
15500 }
15501#endif
15502}
15503
15504/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015505 * "setpos()" function
15506 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015507 static void
15508f_setpos(argvars, rettv)
15509 typval_T *argvars;
15510 typval_T *rettv;
15511{
15512 pos_T pos;
15513 int fnum;
15514 char_u *name;
15515
Bram Moolenaar08250432008-02-13 11:42:46 +000015516 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015517 name = get_tv_string_chk(argvars);
15518 if (name != NULL)
15519 {
15520 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15521 {
15522 --pos.col;
Bram Moolenaar08250432008-02-13 11:42:46 +000015523 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015524 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015525 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015526 if (fnum == curbuf->b_fnum)
15527 {
15528 curwin->w_cursor = pos;
15529 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015530 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015531 }
15532 else
15533 EMSG(_(e_invarg));
15534 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015535 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15536 {
15537 /* set mark */
15538 if (setmark_pos(name[1], &pos, fnum) == OK)
15539 rettv->vval.v_number = 0;
15540 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015541 else
15542 EMSG(_(e_invarg));
15543 }
15544 }
15545}
15546
15547/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015548 * "setqflist()" function
15549 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015550 static void
15551f_setqflist(argvars, rettv)
15552 typval_T *argvars;
15553 typval_T *rettv;
15554{
15555 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15556}
15557
15558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 * "setreg()" function
15560 */
15561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015562f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015563 typval_T *argvars;
15564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565{
15566 int regname;
15567 char_u *strregname;
15568 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015569 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570 int append;
15571 char_u yank_type;
15572 long block_len;
15573
15574 block_len = -1;
15575 yank_type = MAUTO;
15576 append = FALSE;
15577
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015578 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015579 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015581 if (strregname == NULL)
15582 return; /* type error; errmsg already given */
15583 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584 if (regname == 0 || regname == '@')
15585 regname = '"';
15586 else if (regname == '=')
15587 return;
15588
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015589 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015591 stropt = get_tv_string_chk(&argvars[2]);
15592 if (stropt == NULL)
15593 return; /* type error */
15594 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595 switch (*stropt)
15596 {
15597 case 'a': case 'A': /* append */
15598 append = TRUE;
15599 break;
15600 case 'v': case 'c': /* character-wise selection */
15601 yank_type = MCHAR;
15602 break;
15603 case 'V': case 'l': /* line-wise selection */
15604 yank_type = MLINE;
15605 break;
15606#ifdef FEAT_VISUAL
15607 case 'b': case Ctrl_V: /* block-wise selection */
15608 yank_type = MBLOCK;
15609 if (VIM_ISDIGIT(stropt[1]))
15610 {
15611 ++stropt;
15612 block_len = getdigits(&stropt) - 1;
15613 --stropt;
15614 }
15615 break;
15616#endif
15617 }
15618 }
15619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015620 strval = get_tv_string_chk(&argvars[1]);
15621 if (strval != NULL)
15622 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015624 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625}
15626
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015627/*
15628 * "settabwinvar()" function
15629 */
15630 static void
15631f_settabwinvar(argvars, rettv)
15632 typval_T *argvars;
15633 typval_T *rettv;
15634{
15635 setwinvar(argvars, rettv, 1);
15636}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637
15638/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015639 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015642f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015643 typval_T *argvars;
15644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015646 setwinvar(argvars, rettv, 0);
15647}
15648
15649/*
15650 * "setwinvar()" and "settabwinvar()" functions
15651 */
15652 static void
15653setwinvar(argvars, rettv, off)
15654 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015655 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015656 int off;
15657{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658 win_T *win;
15659#ifdef FEAT_WINDOWS
15660 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015661 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662#endif
15663 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015664 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015666 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667
15668 if (check_restricted() || check_secure())
15669 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015670
15671#ifdef FEAT_WINDOWS
15672 if (off == 1)
15673 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15674 else
15675 tp = curtab;
15676#endif
15677 win = find_win_by_nr(&argvars[off], tp);
15678 varname = get_tv_string_chk(&argvars[off + 1]);
15679 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680
15681 if (win != NULL && varname != NULL && varp != NULL)
15682 {
15683#ifdef FEAT_WINDOWS
15684 /* set curwin to be our win, temporarily */
15685 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015686 save_curtab = curtab;
15687 goto_tabpage_tp(tp);
15688 if (!win_valid(win))
15689 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 curwin = win;
15691 curbuf = curwin->w_buffer;
15692#endif
15693
15694 if (*varname == '&')
15695 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015696 long numval;
15697 char_u *strval;
15698 int error = FALSE;
15699
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015701 numval = get_tv_number_chk(varp, &error);
15702 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015703 if (!error && strval != NULL)
15704 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705 }
15706 else
15707 {
15708 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15709 if (winvarname != NULL)
15710 {
15711 STRCPY(winvarname, "w:");
15712 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015713 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714 vim_free(winvarname);
15715 }
15716 }
15717
15718#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015719 /* Restore current tabpage and window, if still valid (autocomands can
15720 * make them invalid). */
15721 if (valid_tabpage(save_curtab))
15722 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723 if (win_valid(save_curwin))
15724 {
15725 curwin = save_curwin;
15726 curbuf = curwin->w_buffer;
15727 }
15728#endif
15729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730}
15731
15732/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015733 * "shellescape({string})" function
15734 */
15735 static void
15736f_shellescape(argvars, rettv)
15737 typval_T *argvars;
15738 typval_T *rettv;
15739{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015740 rettv->vval.v_string = vim_strsave_shellescape(
15741 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015742 rettv->v_type = VAR_STRING;
15743}
15744
15745/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015746 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 */
15748 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015749f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015750 typval_T *argvars;
15751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015753 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754
Bram Moolenaar0d660222005-01-07 21:51:51 +000015755 p = get_tv_string(&argvars[0]);
15756 rettv->vval.v_string = vim_strsave(p);
15757 simplify_filename(rettv->vval.v_string); /* simplify in place */
15758 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759}
15760
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015761#ifdef FEAT_FLOAT
15762/*
15763 * "sin()" function
15764 */
15765 static void
15766f_sin(argvars, rettv)
15767 typval_T *argvars;
15768 typval_T *rettv;
15769{
15770 float_T f;
15771
15772 rettv->v_type = VAR_FLOAT;
15773 if (get_float_arg(argvars, &f) == OK)
15774 rettv->vval.v_float = sin(f);
15775 else
15776 rettv->vval.v_float = 0.0;
15777}
15778#endif
15779
Bram Moolenaar0d660222005-01-07 21:51:51 +000015780static int
15781#ifdef __BORLANDC__
15782 _RTLENTRYF
15783#endif
15784 item_compare __ARGS((const void *s1, const void *s2));
15785static int
15786#ifdef __BORLANDC__
15787 _RTLENTRYF
15788#endif
15789 item_compare2 __ARGS((const void *s1, const void *s2));
15790
15791static int item_compare_ic;
15792static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015793static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015794#define ITEM_COMPARE_FAIL 999
15795
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015797 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015799 static int
15800#ifdef __BORLANDC__
15801_RTLENTRYF
15802#endif
15803item_compare(s1, s2)
15804 const void *s1;
15805 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015807 char_u *p1, *p2;
15808 char_u *tofree1, *tofree2;
15809 int res;
15810 char_u numbuf1[NUMBUFLEN];
15811 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015813 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15814 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015815 if (p1 == NULL)
15816 p1 = (char_u *)"";
15817 if (p2 == NULL)
15818 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015819 if (item_compare_ic)
15820 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015822 res = STRCMP(p1, p2);
15823 vim_free(tofree1);
15824 vim_free(tofree2);
15825 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826}
15827
15828 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015829#ifdef __BORLANDC__
15830_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015832item_compare2(s1, s2)
15833 const void *s1;
15834 const void *s2;
15835{
15836 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015837 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015838 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015839 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015840
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015841 /* shortcut after failure in previous call; compare all items equal */
15842 if (item_compare_func_err)
15843 return 0;
15844
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015845 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15846 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015847 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15848 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015849
15850 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015851 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015852 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015853 clear_tv(&argv[0]);
15854 clear_tv(&argv[1]);
15855
15856 if (res == FAIL)
15857 res = ITEM_COMPARE_FAIL;
15858 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015859 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15860 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000015861 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015862 clear_tv(&rettv);
15863 return res;
15864}
15865
15866/*
15867 * "sort({list})" function
15868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015870f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015871 typval_T *argvars;
15872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873{
Bram Moolenaar33570922005-01-25 22:26:29 +000015874 list_T *l;
15875 listitem_T *li;
15876 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015877 long len;
15878 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015879
Bram Moolenaar0d660222005-01-07 21:51:51 +000015880 if (argvars[0].v_type != VAR_LIST)
15881 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882 else
15883 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015884 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015885 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015886 return;
15887 rettv->vval.v_list = l;
15888 rettv->v_type = VAR_LIST;
15889 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890
Bram Moolenaar0d660222005-01-07 21:51:51 +000015891 len = list_len(l);
15892 if (len <= 1)
15893 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015894
Bram Moolenaar0d660222005-01-07 21:51:51 +000015895 item_compare_ic = FALSE;
15896 item_compare_func = NULL;
15897 if (argvars[1].v_type != VAR_UNKNOWN)
15898 {
15899 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015900 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015901 else
15902 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015903 int error = FALSE;
15904
15905 i = get_tv_number_chk(&argvars[1], &error);
15906 if (error)
15907 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015908 if (i == 1)
15909 item_compare_ic = TRUE;
15910 else
15911 item_compare_func = get_tv_string(&argvars[1]);
15912 }
15913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914
Bram Moolenaar0d660222005-01-07 21:51:51 +000015915 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015916 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015917 if (ptrs == NULL)
15918 return;
15919 i = 0;
15920 for (li = l->lv_first; li != NULL; li = li->li_next)
15921 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015923 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015924 /* test the compare function */
15925 if (item_compare_func != NULL
15926 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15927 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015928 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015930 {
15931 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015932 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015933 item_compare_func == NULL ? item_compare : item_compare2);
15934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015935 if (!item_compare_func_err)
15936 {
15937 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000015938 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015939 l->lv_len = 0;
15940 for (i = 0; i < len; ++i)
15941 list_append(l, ptrs[i]);
15942 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015943 }
15944
15945 vim_free(ptrs);
15946 }
15947}
15948
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015949/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015950 * "soundfold({word})" function
15951 */
15952 static void
15953f_soundfold(argvars, rettv)
15954 typval_T *argvars;
15955 typval_T *rettv;
15956{
15957 char_u *s;
15958
15959 rettv->v_type = VAR_STRING;
15960 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015961#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015962 rettv->vval.v_string = eval_soundfold(s);
15963#else
15964 rettv->vval.v_string = vim_strsave(s);
15965#endif
15966}
15967
15968/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015969 * "spellbadword()" function
15970 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015971 static void
15972f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015973 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015974 typval_T *rettv;
15975{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015976 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015977 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015978 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015979
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015980 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015981 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015982
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015983#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015984 if (argvars[0].v_type == VAR_UNKNOWN)
15985 {
15986 /* Find the start and length of the badly spelled word. */
15987 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15988 if (len != 0)
15989 word = ml_get_cursor();
15990 }
15991 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15992 {
15993 char_u *str = get_tv_string_chk(&argvars[0]);
15994 int capcol = -1;
15995
15996 if (str != NULL)
15997 {
15998 /* Check the argument for spelling. */
15999 while (*str != NUL)
16000 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016001 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016002 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016003 {
16004 word = str;
16005 break;
16006 }
16007 str += len;
16008 }
16009 }
16010 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016011#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016012
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016013 list_append_string(rettv->vval.v_list, word, len);
16014 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016015 attr == HLF_SPB ? "bad" :
16016 attr == HLF_SPR ? "rare" :
16017 attr == HLF_SPL ? "local" :
16018 attr == HLF_SPC ? "caps" :
16019 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016020}
16021
16022/*
16023 * "spellsuggest()" function
16024 */
16025 static void
16026f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016027 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016028 typval_T *rettv;
16029{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016030#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016031 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016032 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016033 int maxcount;
16034 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016035 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016036 listitem_T *li;
16037 int need_capital = FALSE;
16038#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016039
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016040 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016041 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016042
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016043#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016044 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16045 {
16046 str = get_tv_string(&argvars[0]);
16047 if (argvars[1].v_type != VAR_UNKNOWN)
16048 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016049 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016050 if (maxcount <= 0)
16051 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016052 if (argvars[2].v_type != VAR_UNKNOWN)
16053 {
16054 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16055 if (typeerr)
16056 return;
16057 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016058 }
16059 else
16060 maxcount = 25;
16061
Bram Moolenaar4770d092006-01-12 23:22:24 +000016062 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016063
16064 for (i = 0; i < ga.ga_len; ++i)
16065 {
16066 str = ((char_u **)ga.ga_data)[i];
16067
16068 li = listitem_alloc();
16069 if (li == NULL)
16070 vim_free(str);
16071 else
16072 {
16073 li->li_tv.v_type = VAR_STRING;
16074 li->li_tv.v_lock = 0;
16075 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016076 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016077 }
16078 }
16079 ga_clear(&ga);
16080 }
16081#endif
16082}
16083
Bram Moolenaar0d660222005-01-07 21:51:51 +000016084 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016085f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016086 typval_T *argvars;
16087 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016088{
16089 char_u *str;
16090 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016091 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016092 regmatch_T regmatch;
16093 char_u patbuf[NUMBUFLEN];
16094 char_u *save_cpo;
16095 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016096 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016097 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016098 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016099
16100 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16101 save_cpo = p_cpo;
16102 p_cpo = (char_u *)"";
16103
16104 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016105 if (argvars[1].v_type != VAR_UNKNOWN)
16106 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016107 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16108 if (pat == NULL)
16109 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016110 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016111 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016112 }
16113 if (pat == NULL || *pat == NUL)
16114 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016115
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016116 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016117 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016118 if (typeerr)
16119 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120
Bram Moolenaar0d660222005-01-07 21:51:51 +000016121 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16122 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016124 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016125 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016126 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016127 if (*str == NUL)
16128 match = FALSE; /* empty item at the end */
16129 else
16130 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016131 if (match)
16132 end = regmatch.startp[0];
16133 else
16134 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016135 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16136 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016137 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016138 if (list_append_string(rettv->vval.v_list, str,
16139 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016140 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016141 }
16142 if (!match)
16143 break;
16144 /* Advance to just after the match. */
16145 if (regmatch.endp[0] > str)
16146 col = 0;
16147 else
16148 {
16149 /* Don't get stuck at the same match. */
16150#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016151 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016152#else
16153 col = 1;
16154#endif
16155 }
16156 str = regmatch.endp[0];
16157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158
Bram Moolenaar0d660222005-01-07 21:51:51 +000016159 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161
Bram Moolenaar0d660222005-01-07 21:51:51 +000016162 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163}
16164
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016165#ifdef FEAT_FLOAT
16166/*
16167 * "sqrt()" function
16168 */
16169 static void
16170f_sqrt(argvars, rettv)
16171 typval_T *argvars;
16172 typval_T *rettv;
16173{
16174 float_T f;
16175
16176 rettv->v_type = VAR_FLOAT;
16177 if (get_float_arg(argvars, &f) == OK)
16178 rettv->vval.v_float = sqrt(f);
16179 else
16180 rettv->vval.v_float = 0.0;
16181}
16182
16183/*
16184 * "str2float()" function
16185 */
16186 static void
16187f_str2float(argvars, rettv)
16188 typval_T *argvars;
16189 typval_T *rettv;
16190{
16191 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16192
16193 if (*p == '+')
16194 p = skipwhite(p + 1);
16195 (void)string2float(p, &rettv->vval.v_float);
16196 rettv->v_type = VAR_FLOAT;
16197}
16198#endif
16199
Bram Moolenaar2c932302006-03-18 21:42:09 +000016200/*
16201 * "str2nr()" function
16202 */
16203 static void
16204f_str2nr(argvars, rettv)
16205 typval_T *argvars;
16206 typval_T *rettv;
16207{
16208 int base = 10;
16209 char_u *p;
16210 long n;
16211
16212 if (argvars[1].v_type != VAR_UNKNOWN)
16213 {
16214 base = get_tv_number(&argvars[1]);
16215 if (base != 8 && base != 10 && base != 16)
16216 {
16217 EMSG(_(e_invarg));
16218 return;
16219 }
16220 }
16221
16222 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016223 if (*p == '+')
16224 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016225 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16226 rettv->vval.v_number = n;
16227}
16228
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229#ifdef HAVE_STRFTIME
16230/*
16231 * "strftime({format}[, {time}])" function
16232 */
16233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016234f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016235 typval_T *argvars;
16236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237{
16238 char_u result_buf[256];
16239 struct tm *curtime;
16240 time_t seconds;
16241 char_u *p;
16242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016243 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016245 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016246 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247 seconds = time(NULL);
16248 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016249 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250 curtime = localtime(&seconds);
16251 /* MSVC returns NULL for an invalid value of seconds. */
16252 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016253 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254 else
16255 {
16256# ifdef FEAT_MBYTE
16257 vimconv_T conv;
16258 char_u *enc;
16259
16260 conv.vc_type = CONV_NONE;
16261 enc = enc_locale();
16262 convert_setup(&conv, p_enc, enc);
16263 if (conv.vc_type != CONV_NONE)
16264 p = string_convert(&conv, p, NULL);
16265# endif
16266 if (p != NULL)
16267 (void)strftime((char *)result_buf, sizeof(result_buf),
16268 (char *)p, curtime);
16269 else
16270 result_buf[0] = NUL;
16271
16272# ifdef FEAT_MBYTE
16273 if (conv.vc_type != CONV_NONE)
16274 vim_free(p);
16275 convert_setup(&conv, enc, p_enc);
16276 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016277 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278 else
16279# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016280 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281
16282# ifdef FEAT_MBYTE
16283 /* Release conversion descriptors */
16284 convert_setup(&conv, NULL, NULL);
16285 vim_free(enc);
16286# endif
16287 }
16288}
16289#endif
16290
16291/*
16292 * "stridx()" function
16293 */
16294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016295f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016296 typval_T *argvars;
16297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298{
16299 char_u buf[NUMBUFLEN];
16300 char_u *needle;
16301 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016302 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016304 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016306 needle = get_tv_string_chk(&argvars[1]);
16307 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016308 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016309 if (needle == NULL || haystack == NULL)
16310 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311
Bram Moolenaar33570922005-01-25 22:26:29 +000016312 if (argvars[2].v_type != VAR_UNKNOWN)
16313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016314 int error = FALSE;
16315
16316 start_idx = get_tv_number_chk(&argvars[2], &error);
16317 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016318 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016319 if (start_idx >= 0)
16320 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016321 }
16322
16323 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16324 if (pos != NULL)
16325 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326}
16327
16328/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016329 * "string()" function
16330 */
16331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016332f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016333 typval_T *argvars;
16334 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016335{
16336 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016337 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016339 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016340 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016341 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016342 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016343 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344}
16345
16346/*
16347 * "strlen()" function
16348 */
16349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016350f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016351 typval_T *argvars;
16352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016354 rettv->vval.v_number = (varnumber_T)(STRLEN(
16355 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016356}
16357
16358/*
16359 * "strpart()" function
16360 */
16361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016362f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016363 typval_T *argvars;
16364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365{
16366 char_u *p;
16367 int n;
16368 int len;
16369 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016370 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016372 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016373 slen = (int)STRLEN(p);
16374
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016375 n = get_tv_number_chk(&argvars[1], &error);
16376 if (error)
16377 len = 0;
16378 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016379 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380 else
16381 len = slen - n; /* default len: all bytes that are available. */
16382
16383 /*
16384 * Only return the overlap between the specified part and the actual
16385 * string.
16386 */
16387 if (n < 0)
16388 {
16389 len += n;
16390 n = 0;
16391 }
16392 else if (n > slen)
16393 n = slen;
16394 if (len < 0)
16395 len = 0;
16396 else if (n + len > slen)
16397 len = slen - n;
16398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016399 rettv->v_type = VAR_STRING;
16400 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401}
16402
16403/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016404 * "strridx()" function
16405 */
16406 static void
16407f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016408 typval_T *argvars;
16409 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016410{
16411 char_u buf[NUMBUFLEN];
16412 char_u *needle;
16413 char_u *haystack;
16414 char_u *rest;
16415 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016416 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016417
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016418 needle = get_tv_string_chk(&argvars[1]);
16419 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016420
16421 rettv->vval.v_number = -1;
16422 if (needle == NULL || haystack == NULL)
16423 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016424
16425 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016426 if (argvars[2].v_type != VAR_UNKNOWN)
16427 {
16428 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016429 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016430 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016431 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016432 }
16433 else
16434 end_idx = haystack_len;
16435
Bram Moolenaar0d660222005-01-07 21:51:51 +000016436 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016437 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016438 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016439 lastmatch = haystack + end_idx;
16440 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016441 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016442 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016443 for (rest = haystack; *rest != '\0'; ++rest)
16444 {
16445 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016446 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016447 break;
16448 lastmatch = rest;
16449 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016450 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016451
16452 if (lastmatch == NULL)
16453 rettv->vval.v_number = -1;
16454 else
16455 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16456}
16457
16458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016459 * "strtrans()" function
16460 */
16461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016462f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016463 typval_T *argvars;
16464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016466 rettv->v_type = VAR_STRING;
16467 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468}
16469
16470/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016471 * "submatch()" function
16472 */
16473 static void
16474f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016475 typval_T *argvars;
16476 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016477{
16478 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016479 rettv->vval.v_string =
16480 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016481}
16482
16483/*
16484 * "substitute()" function
16485 */
16486 static void
16487f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016488 typval_T *argvars;
16489 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016490{
16491 char_u patbuf[NUMBUFLEN];
16492 char_u subbuf[NUMBUFLEN];
16493 char_u flagsbuf[NUMBUFLEN];
16494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016495 char_u *str = get_tv_string_chk(&argvars[0]);
16496 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16497 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16498 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16499
Bram Moolenaar0d660222005-01-07 21:51:51 +000016500 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016501 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16502 rettv->vval.v_string = NULL;
16503 else
16504 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016505}
16506
16507/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016508 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016511f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016512 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016514{
16515 int id = 0;
16516#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016517 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518 long col;
16519 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016520 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016522 lnum = get_tv_lnum(argvars); /* -1 on type error */
16523 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16524 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016526 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016527 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016528 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529#endif
16530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016531 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532}
16533
16534/*
16535 * "synIDattr(id, what [, mode])" function
16536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016538f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016539 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016541{
16542 char_u *p = NULL;
16543#ifdef FEAT_SYN_HL
16544 int id;
16545 char_u *what;
16546 char_u *mode;
16547 char_u modebuf[NUMBUFLEN];
16548 int modec;
16549
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016550 id = get_tv_number(&argvars[0]);
16551 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016552 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016554 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 modec = TOLOWER_ASC(mode[0]);
16556 if (modec != 't' && modec != 'c'
16557#ifdef FEAT_GUI
16558 && modec != 'g'
16559#endif
16560 )
16561 modec = 0; /* replace invalid with current */
16562 }
16563 else
16564 {
16565#ifdef FEAT_GUI
16566 if (gui.in_use)
16567 modec = 'g';
16568 else
16569#endif
16570 if (t_colors > 1)
16571 modec = 'c';
16572 else
16573 modec = 't';
16574 }
16575
16576
16577 switch (TOLOWER_ASC(what[0]))
16578 {
16579 case 'b':
16580 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16581 p = highlight_color(id, what, modec);
16582 else /* bold */
16583 p = highlight_has_attr(id, HL_BOLD, modec);
16584 break;
16585
16586 case 'f': /* fg[#] */
16587 p = highlight_color(id, what, modec);
16588 break;
16589
16590 case 'i':
16591 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16592 p = highlight_has_attr(id, HL_INVERSE, modec);
16593 else /* italic */
16594 p = highlight_has_attr(id, HL_ITALIC, modec);
16595 break;
16596
16597 case 'n': /* name */
16598 p = get_highlight_name(NULL, id - 1);
16599 break;
16600
16601 case 'r': /* reverse */
16602 p = highlight_has_attr(id, HL_INVERSE, modec);
16603 break;
16604
Bram Moolenaar6f507d62008-11-28 10:16:05 +000016605 case 's':
16606 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
16607 p = highlight_color(id, what, modec);
16608 else /* standout */
16609 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016610 break;
16611
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016612 case 'u':
16613 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16614 /* underline */
16615 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16616 else
16617 /* undercurl */
16618 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619 break;
16620 }
16621
16622 if (p != NULL)
16623 p = vim_strsave(p);
16624#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016625 rettv->v_type = VAR_STRING;
16626 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627}
16628
16629/*
16630 * "synIDtrans(id)" function
16631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016633f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016634 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636{
16637 int id;
16638
16639#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016640 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641
16642 if (id > 0)
16643 id = syn_get_final_id(id);
16644 else
16645#endif
16646 id = 0;
16647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016648 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649}
16650
16651/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016652 * "synstack(lnum, col)" function
16653 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016654 static void
16655f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016656 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016657 typval_T *rettv;
16658{
16659#ifdef FEAT_SYN_HL
16660 long lnum;
16661 long col;
16662 int i;
16663 int id;
16664#endif
16665
16666 rettv->v_type = VAR_LIST;
16667 rettv->vval.v_list = NULL;
16668
16669#ifdef FEAT_SYN_HL
16670 lnum = get_tv_lnum(argvars); /* -1 on type error */
16671 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16672
16673 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar6cad8bd2008-09-10 13:39:10 +000016674 && col >= 0 && (col == 0 || col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016675 && rettv_list_alloc(rettv) != FAIL)
16676 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016677 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016678 for (i = 0; ; ++i)
16679 {
16680 id = syn_get_stack_item(i);
16681 if (id < 0)
16682 break;
16683 if (list_append_number(rettv->vval.v_list, id) == FAIL)
16684 break;
16685 }
16686 }
16687#endif
16688}
16689
16690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691 * "system()" function
16692 */
16693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016694f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016695 typval_T *argvars;
16696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016698 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016700 char_u *infile = NULL;
16701 char_u buf[NUMBUFLEN];
16702 int err = FALSE;
16703 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016705 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016706 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016707
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016708 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016709 {
16710 /*
16711 * Write the string to a temp file, to be used for input of the shell
16712 * command.
16713 */
16714 if ((infile = vim_tempname('i')) == NULL)
16715 {
16716 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016717 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016718 }
16719
16720 fd = mch_fopen((char *)infile, WRITEBIN);
16721 if (fd == NULL)
16722 {
16723 EMSG2(_(e_notopen), infile);
16724 goto done;
16725 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016726 p = get_tv_string_buf_chk(&argvars[1], buf);
16727 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016728 {
16729 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016730 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016731 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016732 if (fwrite(p, STRLEN(p), 1, fd) != 1)
16733 err = TRUE;
16734 if (fclose(fd) != 0)
16735 err = TRUE;
16736 if (err)
16737 {
16738 EMSG(_("E677: Error writing temp file"));
16739 goto done;
16740 }
16741 }
16742
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016743 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
16744 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016745
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746#ifdef USE_CR
16747 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016748 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749 {
16750 char_u *s;
16751
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016752 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753 {
16754 if (*s == CAR)
16755 *s = NL;
16756 }
16757 }
16758#else
16759# ifdef USE_CRNL
16760 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016761 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016762 {
16763 char_u *s, *d;
16764
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016765 d = res;
16766 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767 {
16768 if (s[0] == CAR && s[1] == NL)
16769 ++s;
16770 *d++ = *s;
16771 }
16772 *d = NUL;
16773 }
16774# endif
16775#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016776
16777done:
16778 if (infile != NULL)
16779 {
16780 mch_remove(infile);
16781 vim_free(infile);
16782 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016783 rettv->v_type = VAR_STRING;
16784 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016785}
16786
16787/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016788 * "tabpagebuflist()" function
16789 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016790 static void
16791f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016792 typval_T *argvars UNUSED;
16793 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016794{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016795#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016796 tabpage_T *tp;
16797 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016798
16799 if (argvars[0].v_type == VAR_UNKNOWN)
16800 wp = firstwin;
16801 else
16802 {
16803 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16804 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016805 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016806 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016807 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016808 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016809 for (; wp != NULL; wp = wp->w_next)
16810 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016811 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016812 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016813 }
16814#endif
16815}
16816
16817
16818/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016819 * "tabpagenr()" function
16820 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016821 static void
16822f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016823 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016824 typval_T *rettv;
16825{
16826 int nr = 1;
16827#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016828 char_u *arg;
16829
16830 if (argvars[0].v_type != VAR_UNKNOWN)
16831 {
16832 arg = get_tv_string_chk(&argvars[0]);
16833 nr = 0;
16834 if (arg != NULL)
16835 {
16836 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016837 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016838 else
16839 EMSG2(_(e_invexpr2), arg);
16840 }
16841 }
16842 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016843 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016844#endif
16845 rettv->vval.v_number = nr;
16846}
16847
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016848
16849#ifdef FEAT_WINDOWS
16850static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16851
16852/*
16853 * Common code for tabpagewinnr() and winnr().
16854 */
16855 static int
16856get_winnr(tp, argvar)
16857 tabpage_T *tp;
16858 typval_T *argvar;
16859{
16860 win_T *twin;
16861 int nr = 1;
16862 win_T *wp;
16863 char_u *arg;
16864
16865 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16866 if (argvar->v_type != VAR_UNKNOWN)
16867 {
16868 arg = get_tv_string_chk(argvar);
16869 if (arg == NULL)
16870 nr = 0; /* type error; errmsg already given */
16871 else if (STRCMP(arg, "$") == 0)
16872 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16873 else if (STRCMP(arg, "#") == 0)
16874 {
16875 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16876 if (twin == NULL)
16877 nr = 0;
16878 }
16879 else
16880 {
16881 EMSG2(_(e_invexpr2), arg);
16882 nr = 0;
16883 }
16884 }
16885
16886 if (nr > 0)
16887 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16888 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016889 {
16890 if (wp == NULL)
16891 {
16892 /* didn't find it in this tabpage */
16893 nr = 0;
16894 break;
16895 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016896 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016897 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016898 return nr;
16899}
16900#endif
16901
16902/*
16903 * "tabpagewinnr()" function
16904 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016905 static void
16906f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016907 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016908 typval_T *rettv;
16909{
16910 int nr = 1;
16911#ifdef FEAT_WINDOWS
16912 tabpage_T *tp;
16913
16914 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16915 if (tp == NULL)
16916 nr = 0;
16917 else
16918 nr = get_winnr(tp, &argvars[1]);
16919#endif
16920 rettv->vval.v_number = nr;
16921}
16922
16923
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016924/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016925 * "tagfiles()" function
16926 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016927 static void
16928f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016929 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016930 typval_T *rettv;
16931{
16932 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016933 tagname_T tn;
16934 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016935
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016936 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016937 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016938
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016939 for (first = TRUE; ; first = FALSE)
16940 if (get_tagfname(&tn, first, fname) == FAIL
16941 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016942 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016943 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016944}
16945
16946/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016947 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016948 */
16949 static void
16950f_taglist(argvars, rettv)
16951 typval_T *argvars;
16952 typval_T *rettv;
16953{
16954 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016955
16956 tag_pattern = get_tv_string(&argvars[0]);
16957
16958 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016959 if (*tag_pattern == NUL)
16960 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016961
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016962 if (rettv_list_alloc(rettv) == OK)
16963 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016964}
16965
16966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967 * "tempname()" function
16968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016970f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016971 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973{
16974 static int x = 'A';
16975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016976 rettv->v_type = VAR_STRING;
16977 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978
16979 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16980 * names. Skip 'I' and 'O', they are used for shell redirection. */
16981 do
16982 {
16983 if (x == 'Z')
16984 x = '0';
16985 else if (x == '9')
16986 x = 'A';
16987 else
16988 {
16989#ifdef EBCDIC
16990 if (x == 'I')
16991 x = 'J';
16992 else if (x == 'R')
16993 x = 'S';
16994 else
16995#endif
16996 ++x;
16997 }
16998 } while (x == 'I' || x == 'O');
16999}
17000
17001/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017002 * "test(list)" function: Just checking the walls...
17003 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017004 static void
17005f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017006 typval_T *argvars UNUSED;
17007 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017008{
17009 /* Used for unit testing. Change the code below to your liking. */
17010#if 0
17011 listitem_T *li;
17012 list_T *l;
17013 char_u *bad, *good;
17014
17015 if (argvars[0].v_type != VAR_LIST)
17016 return;
17017 l = argvars[0].vval.v_list;
17018 if (l == NULL)
17019 return;
17020 li = l->lv_first;
17021 if (li == NULL)
17022 return;
17023 bad = get_tv_string(&li->li_tv);
17024 li = li->li_next;
17025 if (li == NULL)
17026 return;
17027 good = get_tv_string(&li->li_tv);
17028 rettv->vval.v_number = test_edit_score(bad, good);
17029#endif
17030}
17031
17032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033 * "tolower(string)" function
17034 */
17035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017036f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017037 typval_T *argvars;
17038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039{
17040 char_u *p;
17041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017042 p = vim_strsave(get_tv_string(&argvars[0]));
17043 rettv->v_type = VAR_STRING;
17044 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045
17046 if (p != NULL)
17047 while (*p != NUL)
17048 {
17049#ifdef FEAT_MBYTE
17050 int l;
17051
17052 if (enc_utf8)
17053 {
17054 int c, lc;
17055
17056 c = utf_ptr2char(p);
17057 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017058 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059 /* TODO: reallocate string when byte count changes. */
17060 if (utf_char2len(lc) == l)
17061 utf_char2bytes(lc, p);
17062 p += l;
17063 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017064 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017065 p += l; /* skip multi-byte character */
17066 else
17067#endif
17068 {
17069 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17070 ++p;
17071 }
17072 }
17073}
17074
17075/*
17076 * "toupper(string)" function
17077 */
17078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017079f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017080 typval_T *argvars;
17081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017083 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017084 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085}
17086
17087/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017088 * "tr(string, fromstr, tostr)" function
17089 */
17090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017091f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017092 typval_T *argvars;
17093 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017094{
17095 char_u *instr;
17096 char_u *fromstr;
17097 char_u *tostr;
17098 char_u *p;
17099#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017100 int inlen;
17101 int fromlen;
17102 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017103 int idx;
17104 char_u *cpstr;
17105 int cplen;
17106 int first = TRUE;
17107#endif
17108 char_u buf[NUMBUFLEN];
17109 char_u buf2[NUMBUFLEN];
17110 garray_T ga;
17111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017112 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017113 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17114 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017115
17116 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017117 rettv->v_type = VAR_STRING;
17118 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017119 if (fromstr == NULL || tostr == NULL)
17120 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017121 ga_init2(&ga, (int)sizeof(char), 80);
17122
17123#ifdef FEAT_MBYTE
17124 if (!has_mbyte)
17125#endif
17126 /* not multi-byte: fromstr and tostr must be the same length */
17127 if (STRLEN(fromstr) != STRLEN(tostr))
17128 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017129#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017130error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017131#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017132 EMSG2(_(e_invarg2), fromstr);
17133 ga_clear(&ga);
17134 return;
17135 }
17136
17137 /* fromstr and tostr have to contain the same number of chars */
17138 while (*instr != NUL)
17139 {
17140#ifdef FEAT_MBYTE
17141 if (has_mbyte)
17142 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017143 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017144 cpstr = instr;
17145 cplen = inlen;
17146 idx = 0;
17147 for (p = fromstr; *p != NUL; p += fromlen)
17148 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017149 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017150 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17151 {
17152 for (p = tostr; *p != NUL; p += tolen)
17153 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017154 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017155 if (idx-- == 0)
17156 {
17157 cplen = tolen;
17158 cpstr = p;
17159 break;
17160 }
17161 }
17162 if (*p == NUL) /* tostr is shorter than fromstr */
17163 goto error;
17164 break;
17165 }
17166 ++idx;
17167 }
17168
17169 if (first && cpstr == instr)
17170 {
17171 /* Check that fromstr and tostr have the same number of
17172 * (multi-byte) characters. Done only once when a character
17173 * of instr doesn't appear in fromstr. */
17174 first = FALSE;
17175 for (p = tostr; *p != NUL; p += tolen)
17176 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017177 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017178 --idx;
17179 }
17180 if (idx != 0)
17181 goto error;
17182 }
17183
17184 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017185 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017186 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017187
17188 instr += inlen;
17189 }
17190 else
17191#endif
17192 {
17193 /* When not using multi-byte chars we can do it faster. */
17194 p = vim_strchr(fromstr, *instr);
17195 if (p != NULL)
17196 ga_append(&ga, tostr[p - fromstr]);
17197 else
17198 ga_append(&ga, *instr);
17199 ++instr;
17200 }
17201 }
17202
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017203 /* add a terminating NUL */
17204 ga_grow(&ga, 1);
17205 ga_append(&ga, NUL);
17206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017207 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017208}
17209
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017210#ifdef FEAT_FLOAT
17211/*
17212 * "trunc({float})" function
17213 */
17214 static void
17215f_trunc(argvars, rettv)
17216 typval_T *argvars;
17217 typval_T *rettv;
17218{
17219 float_T f;
17220
17221 rettv->v_type = VAR_FLOAT;
17222 if (get_float_arg(argvars, &f) == OK)
17223 /* trunc() is not in C90, use floor() or ceil() instead. */
17224 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17225 else
17226 rettv->vval.v_float = 0.0;
17227}
17228#endif
17229
Bram Moolenaar8299df92004-07-10 09:47:34 +000017230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231 * "type(expr)" function
17232 */
17233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017234f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017235 typval_T *argvars;
17236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017237{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017238 int n;
17239
17240 switch (argvars[0].v_type)
17241 {
17242 case VAR_NUMBER: n = 0; break;
17243 case VAR_STRING: n = 1; break;
17244 case VAR_FUNC: n = 2; break;
17245 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017246 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017247#ifdef FEAT_FLOAT
17248 case VAR_FLOAT: n = 5; break;
17249#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017250 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17251 }
17252 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253}
17254
17255/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017256 * "values(dict)" function
17257 */
17258 static void
17259f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017260 typval_T *argvars;
17261 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017262{
17263 dict_list(argvars, rettv, 1);
17264}
17265
17266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267 * "virtcol(string)" function
17268 */
17269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017270f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017271 typval_T *argvars;
17272 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273{
17274 colnr_T vcol = 0;
17275 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017276 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017278 fp = var2fpos(&argvars[0], FALSE, &fnum);
17279 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17280 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281 {
17282 getvvcol(curwin, fp, NULL, NULL, &vcol);
17283 ++vcol;
17284 }
17285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017286 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287}
17288
17289/*
17290 * "visualmode()" function
17291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017293f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017294 typval_T *argvars UNUSED;
17295 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296{
17297#ifdef FEAT_VISUAL
17298 char_u str[2];
17299
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017300 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301 str[0] = curbuf->b_visual_mode_eval;
17302 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017303 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304
17305 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017306 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308#endif
17309}
17310
17311/*
17312 * "winbufnr(nr)" function
17313 */
17314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017315f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017316 typval_T *argvars;
17317 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318{
17319 win_T *wp;
17320
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017321 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017323 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017325 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326}
17327
17328/*
17329 * "wincol()" function
17330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017332f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017333 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335{
17336 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017337 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338}
17339
17340/*
17341 * "winheight(nr)" function
17342 */
17343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017344f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017345 typval_T *argvars;
17346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347{
17348 win_T *wp;
17349
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017350 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017352 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017354 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355}
17356
17357/*
17358 * "winline()" function
17359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017361f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017362 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364{
17365 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017366 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367}
17368
17369/*
17370 * "winnr()" function
17371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017373f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017374 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376{
17377 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017378
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017380 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017382 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383}
17384
17385/*
17386 * "winrestcmd()" function
17387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017389f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017390 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392{
17393#ifdef FEAT_WINDOWS
17394 win_T *wp;
17395 int winnr = 1;
17396 garray_T ga;
17397 char_u buf[50];
17398
17399 ga_init2(&ga, (int)sizeof(char), 70);
17400 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17401 {
17402 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17403 ga_concat(&ga, buf);
17404# ifdef FEAT_VERTSPLIT
17405 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17406 ga_concat(&ga, buf);
17407# endif
17408 ++winnr;
17409 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017410 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017412 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017414 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017416 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417}
17418
17419/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017420 * "winrestview()" function
17421 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017422 static void
17423f_winrestview(argvars, rettv)
17424 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017425 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017426{
17427 dict_T *dict;
17428
17429 if (argvars[0].v_type != VAR_DICT
17430 || (dict = argvars[0].vval.v_dict) == NULL)
17431 EMSG(_(e_invarg));
17432 else
17433 {
17434 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17435 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17436#ifdef FEAT_VIRTUALEDIT
17437 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17438#endif
17439 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017440 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017441
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017442 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017443#ifdef FEAT_DIFF
17444 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17445#endif
17446 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17447 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17448
17449 check_cursor();
17450 changed_cline_bef_curs();
17451 invalidate_botline();
17452 redraw_later(VALID);
17453
17454 if (curwin->w_topline == 0)
17455 curwin->w_topline = 1;
17456 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17457 curwin->w_topline = curbuf->b_ml.ml_line_count;
17458#ifdef FEAT_DIFF
17459 check_topfill(curwin, TRUE);
17460#endif
17461 }
17462}
17463
17464/*
17465 * "winsaveview()" function
17466 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017467 static void
17468f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017469 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017470 typval_T *rettv;
17471{
17472 dict_T *dict;
17473
17474 dict = dict_alloc();
17475 if (dict == NULL)
17476 return;
17477 rettv->v_type = VAR_DICT;
17478 rettv->vval.v_dict = dict;
17479 ++dict->dv_refcount;
17480
17481 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17482 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17483#ifdef FEAT_VIRTUALEDIT
17484 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17485#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017486 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017487 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17488
17489 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17490#ifdef FEAT_DIFF
17491 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17492#endif
17493 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17494 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17495}
17496
17497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498 * "winwidth(nr)" function
17499 */
17500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017501f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017502 typval_T *argvars;
17503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504{
17505 win_T *wp;
17506
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017507 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017509 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 else
17511#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017512 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017514 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515#endif
17516}
17517
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017519 * "writefile()" function
17520 */
17521 static void
17522f_writefile(argvars, rettv)
17523 typval_T *argvars;
17524 typval_T *rettv;
17525{
17526 int binary = FALSE;
17527 char_u *fname;
17528 FILE *fd;
17529 listitem_T *li;
17530 char_u *s;
17531 int ret = 0;
17532 int c;
17533
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017534 if (check_restricted() || check_secure())
17535 return;
17536
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017537 if (argvars[0].v_type != VAR_LIST)
17538 {
17539 EMSG2(_(e_listarg), "writefile()");
17540 return;
17541 }
17542 if (argvars[0].vval.v_list == NULL)
17543 return;
17544
17545 if (argvars[2].v_type != VAR_UNKNOWN
17546 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17547 binary = TRUE;
17548
17549 /* Always open the file in binary mode, library functions have a mind of
17550 * their own about CR-LF conversion. */
17551 fname = get_tv_string(&argvars[1]);
17552 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17553 {
17554 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17555 ret = -1;
17556 }
17557 else
17558 {
17559 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17560 li = li->li_next)
17561 {
17562 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17563 {
17564 if (*s == '\n')
17565 c = putc(NUL, fd);
17566 else
17567 c = putc(*s, fd);
17568 if (c == EOF)
17569 {
17570 ret = -1;
17571 break;
17572 }
17573 }
17574 if (!binary || li->li_next != NULL)
17575 if (putc('\n', fd) == EOF)
17576 {
17577 ret = -1;
17578 break;
17579 }
17580 if (ret < 0)
17581 {
17582 EMSG(_(e_write));
17583 break;
17584 }
17585 }
17586 fclose(fd);
17587 }
17588
17589 rettv->vval.v_number = ret;
17590}
17591
17592/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017594 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017595 */
17596 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017597var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017598 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017599 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017600 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017602 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017604 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605
Bram Moolenaara5525202006-03-02 22:52:09 +000017606 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017607 if (varp->v_type == VAR_LIST)
17608 {
17609 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017610 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000017611 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017612 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017613
17614 l = varp->vval.v_list;
17615 if (l == NULL)
17616 return NULL;
17617
17618 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017619 pos.lnum = list_find_nr(l, 0L, &error);
17620 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017621 return NULL; /* invalid line number */
17622
17623 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017624 pos.col = list_find_nr(l, 1L, &error);
17625 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017626 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017627 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000017628
17629 /* We accept "$" for the column number: last column. */
17630 li = list_find(l, 1L);
17631 if (li != NULL && li->li_tv.v_type == VAR_STRING
17632 && li->li_tv.vval.v_string != NULL
17633 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
17634 pos.col = len + 1;
17635
Bram Moolenaara5525202006-03-02 22:52:09 +000017636 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000017637 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017638 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017639 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017640
Bram Moolenaara5525202006-03-02 22:52:09 +000017641#ifdef FEAT_VIRTUALEDIT
17642 /* Get the virtual offset. Defaults to zero. */
17643 pos.coladd = list_find_nr(l, 2L, &error);
17644 if (error)
17645 pos.coladd = 0;
17646#endif
17647
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017648 return &pos;
17649 }
17650
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017651 name = get_tv_string_chk(varp);
17652 if (name == NULL)
17653 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017654 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017656#ifdef FEAT_VISUAL
17657 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
17658 {
17659 if (VIsual_active)
17660 return &VIsual;
17661 return &curwin->w_cursor;
17662 }
17663#endif
17664 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017666 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
17668 return NULL;
17669 return pp;
17670 }
Bram Moolenaara5525202006-03-02 22:52:09 +000017671
17672#ifdef FEAT_VIRTUALEDIT
17673 pos.coladd = 0;
17674#endif
17675
Bram Moolenaar477933c2007-07-17 14:32:23 +000017676 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017677 {
17678 pos.col = 0;
17679 if (name[1] == '0') /* "w0": first visible line */
17680 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017681 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017682 pos.lnum = curwin->w_topline;
17683 return &pos;
17684 }
17685 else if (name[1] == '$') /* "w$": last visible line */
17686 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017687 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017688 pos.lnum = curwin->w_botline - 1;
17689 return &pos;
17690 }
17691 }
17692 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000017694 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695 {
17696 pos.lnum = curbuf->b_ml.ml_line_count;
17697 pos.col = 0;
17698 }
17699 else
17700 {
17701 pos.lnum = curwin->w_cursor.lnum;
17702 pos.col = (colnr_T)STRLEN(ml_get_curline());
17703 }
17704 return &pos;
17705 }
17706 return NULL;
17707}
17708
17709/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017710 * Convert list in "arg" into a position and optional file number.
17711 * When "fnump" is NULL there is no file number, only 3 items.
17712 * Note that the column is passed on as-is, the caller may want to decrement
17713 * it to use 1 for the first column.
17714 * Return FAIL when conversion is not possible, doesn't check the position for
17715 * validity.
17716 */
17717 static int
17718list2fpos(arg, posp, fnump)
17719 typval_T *arg;
17720 pos_T *posp;
17721 int *fnump;
17722{
17723 list_T *l = arg->vval.v_list;
17724 long i = 0;
17725 long n;
17726
Bram Moolenaarbde35262006-07-23 20:12:24 +000017727 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17728 * when "fnump" isn't NULL and "coladd" is optional. */
17729 if (arg->v_type != VAR_LIST
17730 || l == NULL
17731 || l->lv_len < (fnump == NULL ? 2 : 3)
17732 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017733 return FAIL;
17734
17735 if (fnump != NULL)
17736 {
17737 n = list_find_nr(l, i++, NULL); /* fnum */
17738 if (n < 0)
17739 return FAIL;
17740 if (n == 0)
17741 n = curbuf->b_fnum; /* current buffer */
17742 *fnump = n;
17743 }
17744
17745 n = list_find_nr(l, i++, NULL); /* lnum */
17746 if (n < 0)
17747 return FAIL;
17748 posp->lnum = n;
17749
17750 n = list_find_nr(l, i++, NULL); /* col */
17751 if (n < 0)
17752 return FAIL;
17753 posp->col = n;
17754
17755#ifdef FEAT_VIRTUALEDIT
17756 n = list_find_nr(l, i, NULL);
17757 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000017758 posp->coladd = 0;
17759 else
17760 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017761#endif
17762
17763 return OK;
17764}
17765
17766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767 * Get the length of an environment variable name.
17768 * Advance "arg" to the first character after the name.
17769 * Return 0 for error.
17770 */
17771 static int
17772get_env_len(arg)
17773 char_u **arg;
17774{
17775 char_u *p;
17776 int len;
17777
17778 for (p = *arg; vim_isIDc(*p); ++p)
17779 ;
17780 if (p == *arg) /* no name found */
17781 return 0;
17782
17783 len = (int)(p - *arg);
17784 *arg = p;
17785 return len;
17786}
17787
17788/*
17789 * Get the length of the name of a function or internal variable.
17790 * "arg" is advanced to the first non-white character after the name.
17791 * Return 0 if something is wrong.
17792 */
17793 static int
17794get_id_len(arg)
17795 char_u **arg;
17796{
17797 char_u *p;
17798 int len;
17799
17800 /* Find the end of the name. */
17801 for (p = *arg; eval_isnamec(*p); ++p)
17802 ;
17803 if (p == *arg) /* no name found */
17804 return 0;
17805
17806 len = (int)(p - *arg);
17807 *arg = skipwhite(p);
17808
17809 return len;
17810}
17811
17812/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017813 * Get the length of the name of a variable or function.
17814 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017816 * Return -1 if curly braces expansion failed.
17817 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017818 * If the name contains 'magic' {}'s, expand them and return the
17819 * expanded name in an allocated string via 'alias' - caller must free.
17820 */
17821 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017822get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823 char_u **arg;
17824 char_u **alias;
17825 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017826 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827{
17828 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017829 char_u *p;
17830 char_u *expr_start;
17831 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832
17833 *alias = NULL; /* default to no alias */
17834
17835 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17836 && (*arg)[2] == (int)KE_SNR)
17837 {
17838 /* hard coded <SNR>, already translated */
17839 *arg += 3;
17840 return get_id_len(arg) + 3;
17841 }
17842 len = eval_fname_script(*arg);
17843 if (len > 0)
17844 {
17845 /* literal "<SID>", "s:" or "<SNR>" */
17846 *arg += len;
17847 }
17848
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017850 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017852 p = find_name_end(*arg, &expr_start, &expr_end,
17853 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854 if (expr_start != NULL)
17855 {
17856 char_u *temp_string;
17857
17858 if (!evaluate)
17859 {
17860 len += (int)(p - *arg);
17861 *arg = skipwhite(p);
17862 return len;
17863 }
17864
17865 /*
17866 * Include any <SID> etc in the expanded string:
17867 * Thus the -len here.
17868 */
17869 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17870 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017871 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 *alias = temp_string;
17873 *arg = skipwhite(p);
17874 return (int)STRLEN(temp_string);
17875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876
17877 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017878 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879 EMSG2(_(e_invexpr2), *arg);
17880
17881 return len;
17882}
17883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017884/*
17885 * Find the end of a variable or function name, taking care of magic braces.
17886 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17887 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017888 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017889 * Return a pointer to just after the name. Equal to "arg" if there is no
17890 * valid name.
17891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017893find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894 char_u *arg;
17895 char_u **expr_start;
17896 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017897 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017899 int mb_nest = 0;
17900 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901 char_u *p;
17902
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017903 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017905 *expr_start = NULL;
17906 *expr_end = NULL;
17907 }
17908
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017909 /* Quick check for valid starting character. */
17910 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17911 return arg;
17912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017913 for (p = arg; *p != NUL
17914 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017915 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017916 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017917 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017918 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017919 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017920 if (*p == '\'')
17921 {
17922 /* skip over 'string' to avoid counting [ and ] inside it. */
17923 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17924 ;
17925 if (*p == NUL)
17926 break;
17927 }
17928 else if (*p == '"')
17929 {
17930 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17931 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17932 if (*p == '\\' && p[1] != NUL)
17933 ++p;
17934 if (*p == NUL)
17935 break;
17936 }
17937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017938 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017940 if (*p == '[')
17941 ++br_nest;
17942 else if (*p == ']')
17943 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017944 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017945
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017946 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017948 if (*p == '{')
17949 {
17950 mb_nest++;
17951 if (expr_start != NULL && *expr_start == NULL)
17952 *expr_start = p;
17953 }
17954 else if (*p == '}')
17955 {
17956 mb_nest--;
17957 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17958 *expr_end = p;
17959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961 }
17962
17963 return p;
17964}
17965
17966/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017967 * Expands out the 'magic' {}'s in a variable/function name.
17968 * Note that this can call itself recursively, to deal with
17969 * constructs like foo{bar}{baz}{bam}
17970 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17971 * "in_start" ^
17972 * "expr_start" ^
17973 * "expr_end" ^
17974 * "in_end" ^
17975 *
17976 * Returns a new allocated string, which the caller must free.
17977 * Returns NULL for failure.
17978 */
17979 static char_u *
17980make_expanded_name(in_start, expr_start, expr_end, in_end)
17981 char_u *in_start;
17982 char_u *expr_start;
17983 char_u *expr_end;
17984 char_u *in_end;
17985{
17986 char_u c1;
17987 char_u *retval = NULL;
17988 char_u *temp_result;
17989 char_u *nextcmd = NULL;
17990
17991 if (expr_end == NULL || in_end == NULL)
17992 return NULL;
17993 *expr_start = NUL;
17994 *expr_end = NUL;
17995 c1 = *in_end;
17996 *in_end = NUL;
17997
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017998 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017999 if (temp_result != NULL && nextcmd == NULL)
18000 {
18001 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18002 + (in_end - expr_end) + 1));
18003 if (retval != NULL)
18004 {
18005 STRCPY(retval, in_start);
18006 STRCAT(retval, temp_result);
18007 STRCAT(retval, expr_end + 1);
18008 }
18009 }
18010 vim_free(temp_result);
18011
18012 *in_end = c1; /* put char back for error messages */
18013 *expr_start = '{';
18014 *expr_end = '}';
18015
18016 if (retval != NULL)
18017 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018018 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018019 if (expr_start != NULL)
18020 {
18021 /* Further expansion! */
18022 temp_result = make_expanded_name(retval, expr_start,
18023 expr_end, temp_result);
18024 vim_free(retval);
18025 retval = temp_result;
18026 }
18027 }
18028
18029 return retval;
18030}
18031
18032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018034 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035 */
18036 static int
18037eval_isnamec(c)
18038 int c;
18039{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018040 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18041}
18042
18043/*
18044 * Return TRUE if character "c" can be used as the first character in a
18045 * variable or function name (excluding '{' and '}').
18046 */
18047 static int
18048eval_isnamec1(c)
18049 int c;
18050{
18051 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052}
18053
18054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 * Set number v: variable to "val".
18056 */
18057 void
18058set_vim_var_nr(idx, val)
18059 int idx;
18060 long val;
18061{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018062 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063}
18064
18065/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018066 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067 */
18068 long
18069get_vim_var_nr(idx)
18070 int idx;
18071{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018072 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073}
18074
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018075/*
18076 * Get string v: variable value. Uses a static buffer, can only be used once.
18077 */
18078 char_u *
18079get_vim_var_str(idx)
18080 int idx;
18081{
18082 return get_tv_string(&vimvars[idx].vv_tv);
18083}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018084
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018086 * Get List v: variable value. Caller must take care of reference count when
18087 * needed.
18088 */
18089 list_T *
18090get_vim_var_list(idx)
18091 int idx;
18092{
18093 return vimvars[idx].vv_list;
18094}
18095
18096/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018097 * Set v:count to "count" and v:count1 to "count1".
18098 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 */
18100 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018101set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 long count;
18103 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018104 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018106 if (set_prevcount)
18107 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018108 vimvars[VV_COUNT].vv_nr = count;
18109 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110}
18111
18112/*
18113 * Set string v: variable to a copy of "val".
18114 */
18115 void
18116set_vim_var_string(idx, val, len)
18117 int idx;
18118 char_u *val;
18119 int len; /* length of "val" to use or -1 (whole string) */
18120{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018121 /* Need to do this (at least) once, since we can't initialize a union.
18122 * Will always be invoked when "v:progname" is set. */
18123 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18124
Bram Moolenaare9a41262005-01-15 22:18:47 +000018125 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018127 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018129 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018131 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132}
18133
18134/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018135 * Set List v: variable to "val".
18136 */
18137 void
18138set_vim_var_list(idx, val)
18139 int idx;
18140 list_T *val;
18141{
18142 list_unref(vimvars[idx].vv_list);
18143 vimvars[idx].vv_list = val;
18144 if (val != NULL)
18145 ++val->lv_refcount;
18146}
18147
18148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 * Set v:register if needed.
18150 */
18151 void
18152set_reg_var(c)
18153 int c;
18154{
18155 char_u regname;
18156
18157 if (c == 0 || c == ' ')
18158 regname = '"';
18159 else
18160 regname = c;
18161 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018162 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163 set_vim_var_string(VV_REG, &regname, 1);
18164}
18165
18166/*
18167 * Get or set v:exception. If "oldval" == NULL, return the current value.
18168 * Otherwise, restore the value to "oldval" and return NULL.
18169 * Must always be called in pairs to save and restore v:exception! Does not
18170 * take care of memory allocations.
18171 */
18172 char_u *
18173v_exception(oldval)
18174 char_u *oldval;
18175{
18176 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018177 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178
Bram Moolenaare9a41262005-01-15 22:18:47 +000018179 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180 return NULL;
18181}
18182
18183/*
18184 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18185 * Otherwise, restore the value to "oldval" and return NULL.
18186 * Must always be called in pairs to save and restore v:throwpoint! Does not
18187 * take care of memory allocations.
18188 */
18189 char_u *
18190v_throwpoint(oldval)
18191 char_u *oldval;
18192{
18193 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018194 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195
Bram Moolenaare9a41262005-01-15 22:18:47 +000018196 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197 return NULL;
18198}
18199
18200#if defined(FEAT_AUTOCMD) || defined(PROTO)
18201/*
18202 * Set v:cmdarg.
18203 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18204 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18205 * Must always be called in pairs!
18206 */
18207 char_u *
18208set_cmdarg(eap, oldarg)
18209 exarg_T *eap;
18210 char_u *oldarg;
18211{
18212 char_u *oldval;
18213 char_u *newval;
18214 unsigned len;
18215
Bram Moolenaare9a41262005-01-15 22:18:47 +000018216 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018217 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018219 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018220 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018221 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222 }
18223
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018224 if (eap->force_bin == FORCE_BIN)
18225 len = 6;
18226 else if (eap->force_bin == FORCE_NOBIN)
18227 len = 8;
18228 else
18229 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018230
18231 if (eap->read_edit)
18232 len += 7;
18233
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018234 if (eap->force_ff != 0)
18235 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18236# ifdef FEAT_MBYTE
18237 if (eap->force_enc != 0)
18238 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018239 if (eap->bad_char != 0)
18240 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018241# endif
18242
18243 newval = alloc(len + 1);
18244 if (newval == NULL)
18245 return NULL;
18246
18247 if (eap->force_bin == FORCE_BIN)
18248 sprintf((char *)newval, " ++bin");
18249 else if (eap->force_bin == FORCE_NOBIN)
18250 sprintf((char *)newval, " ++nobin");
18251 else
18252 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018253
18254 if (eap->read_edit)
18255 STRCAT(newval, " ++edit");
18256
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018257 if (eap->force_ff != 0)
18258 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18259 eap->cmd + eap->force_ff);
18260# ifdef FEAT_MBYTE
18261 if (eap->force_enc != 0)
18262 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18263 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018264 if (eap->bad_char != 0)
18265 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
18266 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018267# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018268 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018269 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270}
18271#endif
18272
18273/*
18274 * Get the value of internal variable "name".
18275 * Return OK or FAIL.
18276 */
18277 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018278get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018279 char_u *name;
18280 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018281 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018282 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283{
18284 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018285 typval_T *tv = NULL;
18286 typval_T atv;
18287 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289
18290 /* truncate the name, so that we can use strcmp() */
18291 cc = name[len];
18292 name[len] = NUL;
18293
18294 /*
18295 * Check for "b:changedtick".
18296 */
18297 if (STRCMP(name, "b:changedtick") == 0)
18298 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018299 atv.v_type = VAR_NUMBER;
18300 atv.vval.v_number = curbuf->b_changedtick;
18301 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018302 }
18303
18304 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305 * Check for user-defined variables.
18306 */
18307 else
18308 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018309 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018311 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312 }
18313
Bram Moolenaare9a41262005-01-15 22:18:47 +000018314 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018316 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018317 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318 ret = FAIL;
18319 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018320 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018321 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322
18323 name[len] = cc;
18324
18325 return ret;
18326}
18327
18328/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018329 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18330 * Also handle function call with Funcref variable: func(expr)
18331 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18332 */
18333 static int
18334handle_subscript(arg, rettv, evaluate, verbose)
18335 char_u **arg;
18336 typval_T *rettv;
18337 int evaluate; /* do more than finding the end */
18338 int verbose; /* give error messages */
18339{
18340 int ret = OK;
18341 dict_T *selfdict = NULL;
18342 char_u *s;
18343 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018344 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018345
18346 while (ret == OK
18347 && (**arg == '['
18348 || (**arg == '.' && rettv->v_type == VAR_DICT)
18349 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18350 && !vim_iswhite(*(*arg - 1)))
18351 {
18352 if (**arg == '(')
18353 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018354 /* need to copy the funcref so that we can clear rettv */
18355 functv = *rettv;
18356 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018357
18358 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018359 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018360 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018361 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18362 &len, evaluate, selfdict);
18363
18364 /* Clear the funcref afterwards, so that deleting it while
18365 * evaluating the arguments is possible (see test55). */
18366 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018367
18368 /* Stop the expression evaluation when immediately aborting on
18369 * error, or when an interrupt occurred or an exception was thrown
18370 * but not caught. */
18371 if (aborting())
18372 {
18373 if (ret == OK)
18374 clear_tv(rettv);
18375 ret = FAIL;
18376 }
18377 dict_unref(selfdict);
18378 selfdict = NULL;
18379 }
18380 else /* **arg == '[' || **arg == '.' */
18381 {
18382 dict_unref(selfdict);
18383 if (rettv->v_type == VAR_DICT)
18384 {
18385 selfdict = rettv->vval.v_dict;
18386 if (selfdict != NULL)
18387 ++selfdict->dv_refcount;
18388 }
18389 else
18390 selfdict = NULL;
18391 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18392 {
18393 clear_tv(rettv);
18394 ret = FAIL;
18395 }
18396 }
18397 }
18398 dict_unref(selfdict);
18399 return ret;
18400}
18401
18402/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018403 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018404 * value).
18405 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018406 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018407alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018408{
Bram Moolenaar33570922005-01-25 22:26:29 +000018409 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018410}
18411
18412/*
18413 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414 * The string "s" must have been allocated, it is consumed.
18415 * Return NULL for out of memory, the variable otherwise.
18416 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018417 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018418alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 char_u *s;
18420{
Bram Moolenaar33570922005-01-25 22:26:29 +000018421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018423 rettv = alloc_tv();
18424 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018426 rettv->v_type = VAR_STRING;
18427 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018428 }
18429 else
18430 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018431 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018432}
18433
18434/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018435 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018436 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018437 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018438free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018439 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440{
18441 if (varp != NULL)
18442 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018443 switch (varp->v_type)
18444 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018445 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018446 func_unref(varp->vval.v_string);
18447 /*FALLTHROUGH*/
18448 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018449 vim_free(varp->vval.v_string);
18450 break;
18451 case VAR_LIST:
18452 list_unref(varp->vval.v_list);
18453 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018454 case VAR_DICT:
18455 dict_unref(varp->vval.v_dict);
18456 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018457 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018458#ifdef FEAT_FLOAT
18459 case VAR_FLOAT:
18460#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018461 case VAR_UNKNOWN:
18462 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018463 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018464 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018465 break;
18466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018467 vim_free(varp);
18468 }
18469}
18470
18471/*
18472 * Free the memory for a variable value and set the value to NULL or 0.
18473 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018474 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018475clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018476 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477{
18478 if (varp != NULL)
18479 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018480 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018482 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018483 func_unref(varp->vval.v_string);
18484 /*FALLTHROUGH*/
18485 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018486 vim_free(varp->vval.v_string);
18487 varp->vval.v_string = NULL;
18488 break;
18489 case VAR_LIST:
18490 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018491 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018492 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018493 case VAR_DICT:
18494 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018495 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018496 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018497 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018498 varp->vval.v_number = 0;
18499 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018500#ifdef FEAT_FLOAT
18501 case VAR_FLOAT:
18502 varp->vval.v_float = 0.0;
18503 break;
18504#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018505 case VAR_UNKNOWN:
18506 break;
18507 default:
18508 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018509 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018510 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511 }
18512}
18513
18514/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018515 * Set the value of a variable to NULL without freeing items.
18516 */
18517 static void
18518init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018519 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018520{
18521 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018522 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018523}
18524
18525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526 * Get the number value of a variable.
18527 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018528 * For incompatible types, return 0.
18529 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18530 * caller of incompatible types: it sets *denote to TRUE if "denote"
18531 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532 */
18533 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018534get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018535 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018537 int error = FALSE;
18538
18539 return get_tv_number_chk(varp, &error); /* return 0L on error */
18540}
18541
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018542 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018543get_tv_number_chk(varp, denote)
18544 typval_T *varp;
18545 int *denote;
18546{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018547 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018549 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018551 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018552 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018553#ifdef FEAT_FLOAT
18554 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018555 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018556 break;
18557#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018558 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018559 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018560 break;
18561 case VAR_STRING:
18562 if (varp->vval.v_string != NULL)
18563 vim_str2nr(varp->vval.v_string, NULL, NULL,
18564 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018565 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018566 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018567 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018568 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018569 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018570 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018571 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018572 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018573 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018574 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018576 if (denote == NULL) /* useful for values that must be unsigned */
18577 n = -1;
18578 else
18579 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018580 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581}
18582
18583/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018584 * Get the lnum from the first argument.
18585 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018586 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 */
18588 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018589get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000018590 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591{
Bram Moolenaar33570922005-01-25 22:26:29 +000018592 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593 linenr_T lnum;
18594
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018595 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 if (lnum == 0) /* no valid number, try using line() */
18597 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018598 rettv.v_type = VAR_NUMBER;
18599 f_line(argvars, &rettv);
18600 lnum = rettv.vval.v_number;
18601 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602 }
18603 return lnum;
18604}
18605
18606/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018607 * Get the lnum from the first argument.
18608 * Also accepts "$", then "buf" is used.
18609 * Returns 0 on error.
18610 */
18611 static linenr_T
18612get_tv_lnum_buf(argvars, buf)
18613 typval_T *argvars;
18614 buf_T *buf;
18615{
18616 if (argvars[0].v_type == VAR_STRING
18617 && argvars[0].vval.v_string != NULL
18618 && argvars[0].vval.v_string[0] == '$'
18619 && buf != NULL)
18620 return buf->b_ml.ml_line_count;
18621 return get_tv_number_chk(&argvars[0], NULL);
18622}
18623
18624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 * Get the string value of a variable.
18626 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000018627 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18628 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629 * If the String variable has never been set, return an empty string.
18630 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018631 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18632 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633 */
18634 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018635get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018636 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637{
18638 static char_u mybuf[NUMBUFLEN];
18639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018640 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641}
18642
18643 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018644get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000018645 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018646 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018648 char_u *res = get_tv_string_buf_chk(varp, buf);
18649
18650 return res != NULL ? res : (char_u *)"";
18651}
18652
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018653 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018654get_tv_string_chk(varp)
18655 typval_T *varp;
18656{
18657 static char_u mybuf[NUMBUFLEN];
18658
18659 return get_tv_string_buf_chk(varp, mybuf);
18660}
18661
18662 static char_u *
18663get_tv_string_buf_chk(varp, buf)
18664 typval_T *varp;
18665 char_u *buf;
18666{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018667 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018669 case VAR_NUMBER:
18670 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
18671 return buf;
18672 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018673 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018674 break;
18675 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018676 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000018677 break;
18678 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018679 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018680 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018681#ifdef FEAT_FLOAT
18682 case VAR_FLOAT:
18683 EMSG(_("E806: using Float as a String"));
18684 break;
18685#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018686 case VAR_STRING:
18687 if (varp->vval.v_string != NULL)
18688 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018689 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018690 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018691 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018692 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018694 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695}
18696
18697/*
18698 * Find variable "name" in the list of variables.
18699 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018700 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018701 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000018702 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018704 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018705find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018707 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018710 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711
Bram Moolenaara7043832005-01-21 11:56:39 +000018712 ht = find_var_ht(name, &varname);
18713 if (htp != NULL)
18714 *htp = ht;
18715 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018717 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718}
18719
18720/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018721 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000018722 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018724 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018725find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000018726 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000018727 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018728 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000018729{
Bram Moolenaar33570922005-01-25 22:26:29 +000018730 hashitem_T *hi;
18731
18732 if (*varname == NUL)
18733 {
18734 /* Must be something like "s:", otherwise "ht" would be NULL. */
18735 switch (varname[-2])
18736 {
18737 case 's': return &SCRIPT_SV(current_SID).sv_var;
18738 case 'g': return &globvars_var;
18739 case 'v': return &vimvars_var;
18740 case 'b': return &curbuf->b_bufvar;
18741 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018742#ifdef FEAT_WINDOWS
18743 case 't': return &curtab->tp_winvar;
18744#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018745 case 'l': return current_funccal == NULL
18746 ? NULL : &current_funccal->l_vars_var;
18747 case 'a': return current_funccal == NULL
18748 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000018749 }
18750 return NULL;
18751 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018752
18753 hi = hash_find(ht, varname);
18754 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018755 {
18756 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018757 * worked find the variable again. Don't auto-load a script if it was
18758 * loaded already, otherwise it would be loaded every time when
18759 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018760 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018761 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018762 hi = hash_find(ht, varname);
18763 if (HASHITEM_EMPTY(hi))
18764 return NULL;
18765 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018766 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018767}
18768
18769/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018770 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018771 * Set "varname" to the start of name without ':'.
18772 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018773 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018774find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775 char_u *name;
18776 char_u **varname;
18777{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018778 hashitem_T *hi;
18779
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780 if (name[1] != ':')
18781 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018782 /* The name must not start with a colon or #. */
18783 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784 return NULL;
18785 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018786
18787 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018788 hi = hash_find(&compat_hashtab, name);
18789 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000018790 return &compat_hashtab;
18791
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018793 return &globvarht; /* global variable */
18794 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795 }
18796 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018797 if (*name == 'g') /* global variable */
18798 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018799 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18800 */
18801 if (vim_strchr(name + 2, ':') != NULL
18802 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018803 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018805 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018807 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018808#ifdef FEAT_WINDOWS
18809 if (*name == 't') /* tab page variable */
18810 return &curtab->tp_vars.dv_hashtab;
18811#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000018812 if (*name == 'v') /* v: variable */
18813 return &vimvarht;
18814 if (*name == 'a' && current_funccal != NULL) /* function argument */
18815 return &current_funccal->l_avars.dv_hashtab;
18816 if (*name == 'l' && current_funccal != NULL) /* local function variable */
18817 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818 if (*name == 's' /* script variable */
18819 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
18820 return &SCRIPT_VARS(current_SID);
18821 return NULL;
18822}
18823
18824/*
18825 * Get the string value of a (global/local) variable.
18826 * Returns NULL when it doesn't exist.
18827 */
18828 char_u *
18829get_var_value(name)
18830 char_u *name;
18831{
Bram Moolenaar33570922005-01-25 22:26:29 +000018832 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833
Bram Moolenaara7043832005-01-21 11:56:39 +000018834 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835 if (v == NULL)
18836 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018837 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838}
18839
18840/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018841 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842 * sourcing this script and when executing functions defined in the script.
18843 */
18844 void
18845new_script_vars(id)
18846 scid_T id;
18847{
Bram Moolenaara7043832005-01-21 11:56:39 +000018848 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018849 hashtab_T *ht;
18850 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018851
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18853 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018854 /* Re-allocating ga_data means that an ht_array pointing to
18855 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018856 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018857 for (i = 1; i <= ga_scripts.ga_len; ++i)
18858 {
18859 ht = &SCRIPT_VARS(i);
18860 if (ht->ht_mask == HT_INIT_SIZE - 1)
18861 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018862 sv = &SCRIPT_SV(i);
18863 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018864 }
18865
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866 while (ga_scripts.ga_len < id)
18867 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018868 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18869 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871 }
18872 }
18873}
18874
18875/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018876 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18877 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878 */
18879 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018880init_var_dict(dict, dict_var)
18881 dict_T *dict;
18882 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883{
Bram Moolenaar33570922005-01-25 22:26:29 +000018884 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000018885 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000018886 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018887 dict_var->di_tv.vval.v_dict = dict;
18888 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018889 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018890 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18891 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892}
18893
18894/*
18895 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018896 * Frees all allocated variables and the value they contain.
18897 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 */
18899 void
Bram Moolenaara7043832005-01-21 11:56:39 +000018900vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000018901 hashtab_T *ht;
18902{
18903 vars_clear_ext(ht, TRUE);
18904}
18905
18906/*
18907 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18908 */
18909 static void
18910vars_clear_ext(ht, free_val)
18911 hashtab_T *ht;
18912 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913{
Bram Moolenaara7043832005-01-21 11:56:39 +000018914 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018915 hashitem_T *hi;
18916 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917
Bram Moolenaar33570922005-01-25 22:26:29 +000018918 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018919 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000018920 for (hi = ht->ht_array; todo > 0; ++hi)
18921 {
18922 if (!HASHITEM_EMPTY(hi))
18923 {
18924 --todo;
18925
Bram Moolenaar33570922005-01-25 22:26:29 +000018926 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018927 * ht_array might change then. hash_clear() takes care of it
18928 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018929 v = HI2DI(hi);
18930 if (free_val)
18931 clear_tv(&v->di_tv);
18932 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18933 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018934 }
18935 }
18936 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018937 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018938}
18939
Bram Moolenaara7043832005-01-21 11:56:39 +000018940/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018941 * Delete a variable from hashtab "ht" at item "hi".
18942 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018945delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018946 hashtab_T *ht;
18947 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948{
Bram Moolenaar33570922005-01-25 22:26:29 +000018949 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018950
18951 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018952 clear_tv(&di->di_tv);
18953 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954}
18955
18956/*
18957 * List the value of one internal variable.
18958 */
18959 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018960list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018961 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018963 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018965 char_u *tofree;
18966 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018967 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018968
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018969 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018970 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018971 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018972 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973}
18974
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018976list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977 char_u *prefix;
18978 char_u *name;
18979 int type;
18980 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018981 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982{
Bram Moolenaar31859182007-08-14 20:41:13 +000018983 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18984 msg_start();
18985 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 if (name != NULL) /* "a:" vars don't have a name stored */
18987 msg_puts(name);
18988 msg_putchar(' ');
18989 msg_advance(22);
18990 if (type == VAR_NUMBER)
18991 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018992 else if (type == VAR_FUNC)
18993 msg_putchar('*');
18994 else if (type == VAR_LIST)
18995 {
18996 msg_putchar('[');
18997 if (*string == '[')
18998 ++string;
18999 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019000 else if (type == VAR_DICT)
19001 {
19002 msg_putchar('{');
19003 if (*string == '{')
19004 ++string;
19005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006 else
19007 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019008
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019010
19011 if (type == VAR_FUNC)
19012 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019013 if (*first)
19014 {
19015 msg_clr_eos();
19016 *first = FALSE;
19017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018}
19019
19020/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019021 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022 * If the variable already exists, the value is updated.
19023 * Otherwise the variable is created.
19024 */
19025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019026set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019027 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019028 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019029 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019030{
Bram Moolenaar33570922005-01-25 22:26:29 +000019031 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019033 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019034 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019036 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019037 {
19038 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19039 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19040 ? name[2] : name[0]))
19041 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019042 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019043 return;
19044 }
19045 if (function_exists(name))
19046 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019047 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019048 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019049 return;
19050 }
19051 }
19052
Bram Moolenaara7043832005-01-21 11:56:39 +000019053 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019054 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000019055 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000019056 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000019057 return;
19058 }
19059
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019060 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000019061 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019063 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019064 if (var_check_ro(v->di_flags, name)
19065 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019066 return;
19067 if (v->di_tv.v_type != tv->v_type
19068 && !((v->di_tv.v_type == VAR_STRING
19069 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019070 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019071 || tv->v_type == VAR_NUMBER))
19072#ifdef FEAT_FLOAT
19073 && !((v->di_tv.v_type == VAR_NUMBER
19074 || v->di_tv.v_type == VAR_FLOAT)
19075 && (tv->v_type == VAR_NUMBER
19076 || tv->v_type == VAR_FLOAT))
19077#endif
19078 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019079 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019080 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019081 return;
19082 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019083
19084 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019085 * Handle setting internal v: variables separately: we don't change
19086 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019087 */
19088 if (ht == &vimvarht)
19089 {
19090 if (v->di_tv.v_type == VAR_STRING)
19091 {
19092 vim_free(v->di_tv.vval.v_string);
19093 if (copy || tv->v_type != VAR_STRING)
19094 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19095 else
19096 {
19097 /* Take over the string to avoid an extra alloc/free. */
19098 v->di_tv.vval.v_string = tv->vval.v_string;
19099 tv->vval.v_string = NULL;
19100 }
19101 }
19102 else if (v->di_tv.v_type != VAR_NUMBER)
19103 EMSG2(_(e_intern2), "set_var()");
19104 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019105 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019106 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019107 if (STRCMP(varname, "searchforward") == 0)
19108 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19109 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019110 return;
19111 }
19112
19113 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019114 }
19115 else /* add a new variable */
19116 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019117 /* Can't add "v:" variable. */
19118 if (ht == &vimvarht)
19119 {
19120 EMSG2(_(e_illvar), name);
19121 return;
19122 }
19123
Bram Moolenaar92124a32005-06-17 22:03:40 +000019124 /* Make sure the variable name is valid. */
19125 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019126 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19127 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019128 {
19129 EMSG2(_(e_illvar), varname);
19130 return;
19131 }
19132
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019133 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19134 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019135 if (v == NULL)
19136 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019137 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019138 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019140 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141 return;
19142 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019143 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019145
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019146 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019147 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019148 else
19149 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019150 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019151 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019152 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154}
19155
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019156/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019157 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019158 * Also give an error message.
19159 */
19160 static int
19161var_check_ro(flags, name)
19162 int flags;
19163 char_u *name;
19164{
19165 if (flags & DI_FLAGS_RO)
19166 {
19167 EMSG2(_(e_readonlyvar), name);
19168 return TRUE;
19169 }
19170 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19171 {
19172 EMSG2(_(e_readonlysbx), name);
19173 return TRUE;
19174 }
19175 return FALSE;
19176}
19177
19178/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019179 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19180 * Also give an error message.
19181 */
19182 static int
19183var_check_fixed(flags, name)
19184 int flags;
19185 char_u *name;
19186{
19187 if (flags & DI_FLAGS_FIX)
19188 {
19189 EMSG2(_("E795: Cannot delete variable %s"), name);
19190 return TRUE;
19191 }
19192 return FALSE;
19193}
19194
19195/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019196 * Return TRUE if typeval "tv" is set to be locked (immutable).
19197 * Also give an error message, using "name".
19198 */
19199 static int
19200tv_check_lock(lock, name)
19201 int lock;
19202 char_u *name;
19203{
19204 if (lock & VAR_LOCKED)
19205 {
19206 EMSG2(_("E741: Value is locked: %s"),
19207 name == NULL ? (char_u *)_("Unknown") : name);
19208 return TRUE;
19209 }
19210 if (lock & VAR_FIXED)
19211 {
19212 EMSG2(_("E742: Cannot change value of %s"),
19213 name == NULL ? (char_u *)_("Unknown") : name);
19214 return TRUE;
19215 }
19216 return FALSE;
19217}
19218
19219/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019220 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019221 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019222 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019223 * It is OK for "from" and "to" to point to the same item. This is used to
19224 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019225 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019227copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019228 typval_T *from;
19229 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019231 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019232 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019233 switch (from->v_type)
19234 {
19235 case VAR_NUMBER:
19236 to->vval.v_number = from->vval.v_number;
19237 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019238#ifdef FEAT_FLOAT
19239 case VAR_FLOAT:
19240 to->vval.v_float = from->vval.v_float;
19241 break;
19242#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019243 case VAR_STRING:
19244 case VAR_FUNC:
19245 if (from->vval.v_string == NULL)
19246 to->vval.v_string = NULL;
19247 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019248 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019249 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019250 if (from->v_type == VAR_FUNC)
19251 func_ref(to->vval.v_string);
19252 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019253 break;
19254 case VAR_LIST:
19255 if (from->vval.v_list == NULL)
19256 to->vval.v_list = NULL;
19257 else
19258 {
19259 to->vval.v_list = from->vval.v_list;
19260 ++to->vval.v_list->lv_refcount;
19261 }
19262 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019263 case VAR_DICT:
19264 if (from->vval.v_dict == NULL)
19265 to->vval.v_dict = NULL;
19266 else
19267 {
19268 to->vval.v_dict = from->vval.v_dict;
19269 ++to->vval.v_dict->dv_refcount;
19270 }
19271 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019272 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019273 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019274 break;
19275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019276}
19277
19278/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019279 * Make a copy of an item.
19280 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019281 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19282 * reference to an already copied list/dict can be used.
19283 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019284 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019285 static int
19286item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019287 typval_T *from;
19288 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019289 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019290 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019291{
19292 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019293 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019294
Bram Moolenaar33570922005-01-25 22:26:29 +000019295 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019296 {
19297 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019298 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019299 }
19300 ++recurse;
19301
19302 switch (from->v_type)
19303 {
19304 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019305#ifdef FEAT_FLOAT
19306 case VAR_FLOAT:
19307#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019308 case VAR_STRING:
19309 case VAR_FUNC:
19310 copy_tv(from, to);
19311 break;
19312 case VAR_LIST:
19313 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019314 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019315 if (from->vval.v_list == NULL)
19316 to->vval.v_list = NULL;
19317 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19318 {
19319 /* use the copy made earlier */
19320 to->vval.v_list = from->vval.v_list->lv_copylist;
19321 ++to->vval.v_list->lv_refcount;
19322 }
19323 else
19324 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19325 if (to->vval.v_list == NULL)
19326 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019327 break;
19328 case VAR_DICT:
19329 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019330 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019331 if (from->vval.v_dict == NULL)
19332 to->vval.v_dict = NULL;
19333 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19334 {
19335 /* use the copy made earlier */
19336 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19337 ++to->vval.v_dict->dv_refcount;
19338 }
19339 else
19340 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19341 if (to->vval.v_dict == NULL)
19342 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019343 break;
19344 default:
19345 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019346 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019347 }
19348 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019349 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019350}
19351
19352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353 * ":echo expr1 ..." print each argument separated with a space, add a
19354 * newline at the end.
19355 * ":echon expr1 ..." print each argument plain.
19356 */
19357 void
19358ex_echo(eap)
19359 exarg_T *eap;
19360{
19361 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019362 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019363 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364 char_u *p;
19365 int needclr = TRUE;
19366 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019367 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368
19369 if (eap->skip)
19370 ++emsg_skip;
19371 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19372 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019373 /* If eval1() causes an error message the text from the command may
19374 * still need to be cleared. E.g., "echo 22,44". */
19375 need_clr_eos = needclr;
19376
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019378 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 {
19380 /*
19381 * Report the invalid expression unless the expression evaluation
19382 * has been cancelled due to an aborting error, an interrupt, or an
19383 * exception.
19384 */
19385 if (!aborting())
19386 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019387 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388 break;
19389 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019390 need_clr_eos = FALSE;
19391
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392 if (!eap->skip)
19393 {
19394 if (atstart)
19395 {
19396 atstart = FALSE;
19397 /* Call msg_start() after eval1(), evaluating the expression
19398 * may cause a message to appear. */
19399 if (eap->cmdidx == CMD_echo)
19400 msg_start();
19401 }
19402 else if (eap->cmdidx == CMD_echo)
19403 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019404 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019405 if (p != NULL)
19406 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019408 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019410 if (*p != TAB && needclr)
19411 {
19412 /* remove any text still there from the command */
19413 msg_clr_eos();
19414 needclr = FALSE;
19415 }
19416 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417 }
19418 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019419 {
19420#ifdef FEAT_MBYTE
19421 if (has_mbyte)
19422 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019423 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019424
19425 (void)msg_outtrans_len_attr(p, i, echo_attr);
19426 p += i - 1;
19427 }
19428 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019430 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019433 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019434 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019435 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436 arg = skipwhite(arg);
19437 }
19438 eap->nextcmd = check_nextcmd(arg);
19439
19440 if (eap->skip)
19441 --emsg_skip;
19442 else
19443 {
19444 /* remove text that may still be there from the command */
19445 if (needclr)
19446 msg_clr_eos();
19447 if (eap->cmdidx == CMD_echo)
19448 msg_end();
19449 }
19450}
19451
19452/*
19453 * ":echohl {name}".
19454 */
19455 void
19456ex_echohl(eap)
19457 exarg_T *eap;
19458{
19459 int id;
19460
19461 id = syn_name2id(eap->arg);
19462 if (id == 0)
19463 echo_attr = 0;
19464 else
19465 echo_attr = syn_id2attr(id);
19466}
19467
19468/*
19469 * ":execute expr1 ..." execute the result of an expression.
19470 * ":echomsg expr1 ..." Print a message
19471 * ":echoerr expr1 ..." Print an error
19472 * Each gets spaces around each argument and a newline at the end for
19473 * echo commands
19474 */
19475 void
19476ex_execute(eap)
19477 exarg_T *eap;
19478{
19479 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019480 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 int ret = OK;
19482 char_u *p;
19483 garray_T ga;
19484 int len;
19485 int save_did_emsg;
19486
19487 ga_init2(&ga, 1, 80);
19488
19489 if (eap->skip)
19490 ++emsg_skip;
19491 while (*arg != NUL && *arg != '|' && *arg != '\n')
19492 {
19493 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019494 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019495 {
19496 /*
19497 * Report the invalid expression unless the expression evaluation
19498 * has been cancelled due to an aborting error, an interrupt, or an
19499 * exception.
19500 */
19501 if (!aborting())
19502 EMSG2(_(e_invexpr2), p);
19503 ret = FAIL;
19504 break;
19505 }
19506
19507 if (!eap->skip)
19508 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019509 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510 len = (int)STRLEN(p);
19511 if (ga_grow(&ga, len + 2) == FAIL)
19512 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019513 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514 ret = FAIL;
19515 break;
19516 }
19517 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019518 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520 ga.ga_len += len;
19521 }
19522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019523 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 arg = skipwhite(arg);
19525 }
19526
19527 if (ret != FAIL && ga.ga_data != NULL)
19528 {
19529 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019530 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019532 out_flush();
19533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534 else if (eap->cmdidx == CMD_echoerr)
19535 {
19536 /* We don't want to abort following commands, restore did_emsg. */
19537 save_did_emsg = did_emsg;
19538 EMSG((char_u *)ga.ga_data);
19539 if (!force_abort)
19540 did_emsg = save_did_emsg;
19541 }
19542 else if (eap->cmdidx == CMD_execute)
19543 do_cmdline((char_u *)ga.ga_data,
19544 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19545 }
19546
19547 ga_clear(&ga);
19548
19549 if (eap->skip)
19550 --emsg_skip;
19551
19552 eap->nextcmd = check_nextcmd(arg);
19553}
19554
19555/*
19556 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19557 * "arg" points to the "&" or '+' when called, to "option" when returning.
19558 * Returns NULL when no option name found. Otherwise pointer to the char
19559 * after the option name.
19560 */
19561 static char_u *
19562find_option_end(arg, opt_flags)
19563 char_u **arg;
19564 int *opt_flags;
19565{
19566 char_u *p = *arg;
19567
19568 ++p;
19569 if (*p == 'g' && p[1] == ':')
19570 {
19571 *opt_flags = OPT_GLOBAL;
19572 p += 2;
19573 }
19574 else if (*p == 'l' && p[1] == ':')
19575 {
19576 *opt_flags = OPT_LOCAL;
19577 p += 2;
19578 }
19579 else
19580 *opt_flags = 0;
19581
19582 if (!ASCII_ISALPHA(*p))
19583 return NULL;
19584 *arg = p;
19585
19586 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
19587 p += 4; /* termcap option */
19588 else
19589 while (ASCII_ISALPHA(*p))
19590 ++p;
19591 return p;
19592}
19593
19594/*
19595 * ":function"
19596 */
19597 void
19598ex_function(eap)
19599 exarg_T *eap;
19600{
19601 char_u *theline;
19602 int j;
19603 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605 char_u *name = NULL;
19606 char_u *p;
19607 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019608 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609 garray_T newargs;
19610 garray_T newlines;
19611 int varargs = FALSE;
19612 int mustend = FALSE;
19613 int flags = 0;
19614 ufunc_T *fp;
19615 int indent;
19616 int nesting;
19617 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019618 dictitem_T *v;
19619 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019620 static int func_nr = 0; /* number for nameless function */
19621 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019622 hashtab_T *ht;
19623 int todo;
19624 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019625 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626
19627 /*
19628 * ":function" without argument: list functions.
19629 */
19630 if (ends_excmd(*eap->arg))
19631 {
19632 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019633 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019634 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000019635 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019636 {
19637 if (!HASHITEM_EMPTY(hi))
19638 {
19639 --todo;
19640 fp = HI2UF(hi);
19641 if (!isdigit(*fp->uf_name))
19642 list_func_head(fp, FALSE);
19643 }
19644 }
19645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646 eap->nextcmd = check_nextcmd(eap->arg);
19647 return;
19648 }
19649
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019650 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019651 * ":function /pat": list functions matching pattern.
19652 */
19653 if (*eap->arg == '/')
19654 {
19655 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
19656 if (!eap->skip)
19657 {
19658 regmatch_T regmatch;
19659
19660 c = *p;
19661 *p = NUL;
19662 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
19663 *p = c;
19664 if (regmatch.regprog != NULL)
19665 {
19666 regmatch.rm_ic = p_ic;
19667
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019668 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019669 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
19670 {
19671 if (!HASHITEM_EMPTY(hi))
19672 {
19673 --todo;
19674 fp = HI2UF(hi);
19675 if (!isdigit(*fp->uf_name)
19676 && vim_regexec(&regmatch, fp->uf_name, 0))
19677 list_func_head(fp, FALSE);
19678 }
19679 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000019680 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019681 }
19682 }
19683 if (*p == '/')
19684 ++p;
19685 eap->nextcmd = check_nextcmd(p);
19686 return;
19687 }
19688
19689 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019690 * Get the function name. There are these situations:
19691 * func normal function name
19692 * "name" == func, "fudi.fd_dict" == NULL
19693 * dict.func new dictionary entry
19694 * "name" == NULL, "fudi.fd_dict" set,
19695 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19696 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019697 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019698 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19699 * dict.func existing dict entry that's not a Funcref
19700 * "name" == NULL, "fudi.fd_dict" set,
19701 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019704 name = trans_function_name(&p, eap->skip, 0, &fudi);
19705 paren = (vim_strchr(p, '(') != NULL);
19706 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019707 {
19708 /*
19709 * Return on an invalid expression in braces, unless the expression
19710 * evaluation has been cancelled due to an aborting error, an
19711 * interrupt, or an exception.
19712 */
19713 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019714 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019715 if (!eap->skip && fudi.fd_newkey != NULL)
19716 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019717 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 else
19721 eap->skip = TRUE;
19722 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000019723
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724 /* An error in a function call during evaluation of an expression in magic
19725 * braces should not cause the function not to be defined. */
19726 saved_did_emsg = did_emsg;
19727 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728
19729 /*
19730 * ":function func" with only function name: list function.
19731 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019732 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733 {
19734 if (!ends_excmd(*skipwhite(p)))
19735 {
19736 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019737 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019738 }
19739 eap->nextcmd = check_nextcmd(p);
19740 if (eap->nextcmd != NULL)
19741 *p = NUL;
19742 if (!eap->skip && !got_int)
19743 {
19744 fp = find_func(name);
19745 if (fp != NULL)
19746 {
19747 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019748 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019750 if (FUNCLINE(fp, j) == NULL)
19751 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752 msg_putchar('\n');
19753 msg_outnum((long)(j + 1));
19754 if (j < 9)
19755 msg_putchar(' ');
19756 if (j < 99)
19757 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019758 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759 out_flush(); /* show a line at a time */
19760 ui_breakcheck();
19761 }
19762 if (!got_int)
19763 {
19764 msg_putchar('\n');
19765 msg_puts((char_u *)" endfunction");
19766 }
19767 }
19768 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000019769 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019771 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 }
19773
19774 /*
19775 * ":function name(arg1, arg2)" Define function.
19776 */
19777 p = skipwhite(p);
19778 if (*p != '(')
19779 {
19780 if (!eap->skip)
19781 {
19782 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019783 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019784 }
19785 /* attempt to continue by skipping some text */
19786 if (vim_strchr(p, '(') != NULL)
19787 p = vim_strchr(p, '(');
19788 }
19789 p = skipwhite(p + 1);
19790
19791 ga_init2(&newargs, (int)sizeof(char_u *), 3);
19792 ga_init2(&newlines, (int)sizeof(char_u *), 3);
19793
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019794 if (!eap->skip)
19795 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019796 /* Check the name of the function. Unless it's a dictionary function
19797 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019798 if (name != NULL)
19799 arg = name;
19800 else
19801 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019802 if (arg != NULL && (fudi.fd_di == NULL
19803 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019804 {
19805 if (*arg == K_SPECIAL)
19806 j = 3;
19807 else
19808 j = 0;
19809 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
19810 : eval_isnamec(arg[j])))
19811 ++j;
19812 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000019813 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019814 }
19815 }
19816
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 /*
19818 * Isolate the arguments: "arg1, arg2, ...)"
19819 */
19820 while (*p != ')')
19821 {
19822 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
19823 {
19824 varargs = TRUE;
19825 p += 3;
19826 mustend = TRUE;
19827 }
19828 else
19829 {
19830 arg = p;
19831 while (ASCII_ISALNUM(*p) || *p == '_')
19832 ++p;
19833 if (arg == p || isdigit(*arg)
19834 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
19835 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
19836 {
19837 if (!eap->skip)
19838 EMSG2(_("E125: Illegal argument: %s"), arg);
19839 break;
19840 }
19841 if (ga_grow(&newargs, 1) == FAIL)
19842 goto erret;
19843 c = *p;
19844 *p = NUL;
19845 arg = vim_strsave(arg);
19846 if (arg == NULL)
19847 goto erret;
19848 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
19849 *p = c;
19850 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019851 if (*p == ',')
19852 ++p;
19853 else
19854 mustend = TRUE;
19855 }
19856 p = skipwhite(p);
19857 if (mustend && *p != ')')
19858 {
19859 if (!eap->skip)
19860 EMSG2(_(e_invarg2), eap->arg);
19861 break;
19862 }
19863 }
19864 ++p; /* skip the ')' */
19865
Bram Moolenaare9a41262005-01-15 22:18:47 +000019866 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867 for (;;)
19868 {
19869 p = skipwhite(p);
19870 if (STRNCMP(p, "range", 5) == 0)
19871 {
19872 flags |= FC_RANGE;
19873 p += 5;
19874 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019875 else if (STRNCMP(p, "dict", 4) == 0)
19876 {
19877 flags |= FC_DICT;
19878 p += 4;
19879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 else if (STRNCMP(p, "abort", 5) == 0)
19881 {
19882 flags |= FC_ABORT;
19883 p += 5;
19884 }
19885 else
19886 break;
19887 }
19888
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019889 /* When there is a line break use what follows for the function body.
19890 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19891 if (*p == '\n')
19892 line_arg = p + 1;
19893 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894 EMSG(_(e_trailing));
19895
19896 /*
19897 * Read the body of the function, until ":endfunction" is found.
19898 */
19899 if (KeyTyped)
19900 {
19901 /* Check if the function already exists, don't let the user type the
19902 * whole function before telling him it doesn't work! For a script we
19903 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019904 if (!eap->skip && !eap->forceit)
19905 {
19906 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
19907 EMSG(_(e_funcdict));
19908 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019909 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019911
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019912 if (!eap->skip && did_emsg)
19913 goto erret;
19914
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915 msg_putchar('\n'); /* don't overwrite the function name */
19916 cmdline_row = msg_row;
19917 }
19918
19919 indent = 2;
19920 nesting = 0;
19921 for (;;)
19922 {
19923 msg_scroll = TRUE;
19924 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019925 sourcing_lnum_off = sourcing_lnum;
19926
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019927 if (line_arg != NULL)
19928 {
19929 /* Use eap->arg, split up in parts by line breaks. */
19930 theline = line_arg;
19931 p = vim_strchr(theline, '\n');
19932 if (p == NULL)
19933 line_arg += STRLEN(line_arg);
19934 else
19935 {
19936 *p = NUL;
19937 line_arg = p + 1;
19938 }
19939 }
19940 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941 theline = getcmdline(':', 0L, indent);
19942 else
19943 theline = eap->getline(':', eap->cookie, indent);
19944 if (KeyTyped)
19945 lines_left = Rows - 1;
19946 if (theline == NULL)
19947 {
19948 EMSG(_("E126: Missing :endfunction"));
19949 goto erret;
19950 }
19951
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019952 /* Detect line continuation: sourcing_lnum increased more than one. */
19953 if (sourcing_lnum > sourcing_lnum_off + 1)
19954 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19955 else
19956 sourcing_lnum_off = 0;
19957
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958 if (skip_until != NULL)
19959 {
19960 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19961 * don't check for ":endfunc". */
19962 if (STRCMP(theline, skip_until) == 0)
19963 {
19964 vim_free(skip_until);
19965 skip_until = NULL;
19966 }
19967 }
19968 else
19969 {
19970 /* skip ':' and blanks*/
19971 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19972 ;
19973
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019974 /* Check for "endfunction". */
19975 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019977 if (line_arg == NULL)
19978 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979 break;
19980 }
19981
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019982 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983 * at "end". */
19984 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19985 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019986 else if (STRNCMP(p, "if", 2) == 0
19987 || STRNCMP(p, "wh", 2) == 0
19988 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989 || STRNCMP(p, "try", 3) == 0)
19990 indent += 2;
19991
19992 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019993 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019995 if (*p == '!')
19996 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 p += eval_fname_script(p);
19998 if (ASCII_ISALPHA(*p))
19999 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020000 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001 if (*skipwhite(p) == '(')
20002 {
20003 ++nesting;
20004 indent += 2;
20005 }
20006 }
20007 }
20008
20009 /* Check for ":append" or ":insert". */
20010 p = skip_range(p, NULL);
20011 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20012 || (p[0] == 'i'
20013 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20014 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20015 skip_until = vim_strsave((char_u *)".");
20016
20017 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20018 arg = skipwhite(skiptowhite(p));
20019 if (arg[0] == '<' && arg[1] =='<'
20020 && ((p[0] == 'p' && p[1] == 'y'
20021 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20022 || (p[0] == 'p' && p[1] == 'e'
20023 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20024 || (p[0] == 't' && p[1] == 'c'
20025 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20026 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20027 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020028 || (p[0] == 'm' && p[1] == 'z'
20029 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030 ))
20031 {
20032 /* ":python <<" continues until a dot, like ":append" */
20033 p = skipwhite(arg + 2);
20034 if (*p == NUL)
20035 skip_until = vim_strsave((char_u *)".");
20036 else
20037 skip_until = vim_strsave(p);
20038 }
20039 }
20040
20041 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020042 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020043 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020044 if (line_arg == NULL)
20045 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020047 }
20048
20049 /* Copy the line to newly allocated memory. get_one_sourceline()
20050 * allocates 250 bytes per line, this saves 80% on average. The cost
20051 * is an extra alloc/free. */
20052 p = vim_strsave(theline);
20053 if (p != NULL)
20054 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020055 if (line_arg == NULL)
20056 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020057 theline = p;
20058 }
20059
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020060 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20061
20062 /* Add NULL lines for continuation lines, so that the line count is
20063 * equal to the index in the growarray. */
20064 while (sourcing_lnum_off-- > 0)
20065 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020066
20067 /* Check for end of eap->arg. */
20068 if (line_arg != NULL && *line_arg == NUL)
20069 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070 }
20071
20072 /* Don't define the function when skipping commands or when an error was
20073 * detected. */
20074 if (eap->skip || did_emsg)
20075 goto erret;
20076
20077 /*
20078 * If there are no errors, add the function
20079 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020080 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020081 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020082 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020083 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020084 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020085 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020086 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020087 goto erret;
20088 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020089
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020090 fp = find_func(name);
20091 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020092 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020093 if (!eap->forceit)
20094 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020095 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020096 goto erret;
20097 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020098 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020099 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020100 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020101 name);
20102 goto erret;
20103 }
20104 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020105 ga_clear_strings(&(fp->uf_args));
20106 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020107 vim_free(name);
20108 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110 }
20111 else
20112 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020113 char numbuf[20];
20114
20115 fp = NULL;
20116 if (fudi.fd_newkey == NULL && !eap->forceit)
20117 {
20118 EMSG(_(e_funcdict));
20119 goto erret;
20120 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020121 if (fudi.fd_di == NULL)
20122 {
20123 /* Can't add a function to a locked dictionary */
20124 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20125 goto erret;
20126 }
20127 /* Can't change an existing function if it is locked */
20128 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20129 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020130
20131 /* Give the function a sequential number. Can only be used with a
20132 * Funcref! */
20133 vim_free(name);
20134 sprintf(numbuf, "%d", ++func_nr);
20135 name = vim_strsave((char_u *)numbuf);
20136 if (name == NULL)
20137 goto erret;
20138 }
20139
20140 if (fp == NULL)
20141 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020142 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020143 {
20144 int slen, plen;
20145 char_u *scriptname;
20146
20147 /* Check that the autoload name matches the script name. */
20148 j = FAIL;
20149 if (sourcing_name != NULL)
20150 {
20151 scriptname = autoload_name(name);
20152 if (scriptname != NULL)
20153 {
20154 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020155 plen = (int)STRLEN(p);
20156 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020157 if (slen > plen && fnamecmp(p,
20158 sourcing_name + slen - plen) == 0)
20159 j = OK;
20160 vim_free(scriptname);
20161 }
20162 }
20163 if (j == FAIL)
20164 {
20165 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20166 goto erret;
20167 }
20168 }
20169
20170 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 if (fp == NULL)
20172 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020173
20174 if (fudi.fd_dict != NULL)
20175 {
20176 if (fudi.fd_di == NULL)
20177 {
20178 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020179 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020180 if (fudi.fd_di == NULL)
20181 {
20182 vim_free(fp);
20183 goto erret;
20184 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020185 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20186 {
20187 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020188 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020189 goto erret;
20190 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020191 }
20192 else
20193 /* overwrite existing dict entry */
20194 clear_tv(&fudi.fd_di->di_tv);
20195 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020196 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020197 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020198 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020199
20200 /* behave like "dict" was used */
20201 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020202 }
20203
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020205 STRCPY(fp->uf_name, name);
20206 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020208 fp->uf_args = newargs;
20209 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020210#ifdef FEAT_PROFILE
20211 fp->uf_tml_count = NULL;
20212 fp->uf_tml_total = NULL;
20213 fp->uf_tml_self = NULL;
20214 fp->uf_profiling = FALSE;
20215 if (prof_def_func())
20216 func_do_profile(fp);
20217#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020218 fp->uf_varargs = varargs;
20219 fp->uf_flags = flags;
20220 fp->uf_calls = 0;
20221 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020222 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223
20224erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 ga_clear_strings(&newargs);
20226 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020227ret_free:
20228 vim_free(skip_until);
20229 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020232}
20233
20234/*
20235 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020236 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020238 * flags:
20239 * TFN_INT: internal function name OK
20240 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 * Advances "pp" to just after the function name (if no error).
20242 */
20243 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020244trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245 char_u **pp;
20246 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020247 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020248 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020250 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 char_u *start;
20252 char_u *end;
20253 int lead;
20254 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020256 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020257
20258 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020259 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020261
20262 /* Check for hard coded <SNR>: already translated function ID (from a user
20263 * command). */
20264 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20265 && (*pp)[2] == (int)KE_SNR)
20266 {
20267 *pp += 3;
20268 len = get_id_len(pp) + 3;
20269 return vim_strnsave(start, len);
20270 }
20271
20272 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20273 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020275 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020277
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020278 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20279 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020280 if (end == start)
20281 {
20282 if (!skip)
20283 EMSG(_("E129: Function name required"));
20284 goto theend;
20285 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020286 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020287 {
20288 /*
20289 * Report an invalid expression in braces, unless the expression
20290 * evaluation has been cancelled due to an aborting error, an
20291 * interrupt, or an exception.
20292 */
20293 if (!aborting())
20294 {
20295 if (end != NULL)
20296 EMSG2(_(e_invarg2), start);
20297 }
20298 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020299 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020300 goto theend;
20301 }
20302
20303 if (lv.ll_tv != NULL)
20304 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020305 if (fdp != NULL)
20306 {
20307 fdp->fd_dict = lv.ll_dict;
20308 fdp->fd_newkey = lv.ll_newkey;
20309 lv.ll_newkey = NULL;
20310 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020311 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020312 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20313 {
20314 name = vim_strsave(lv.ll_tv->vval.v_string);
20315 *pp = end;
20316 }
20317 else
20318 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020319 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20320 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020321 EMSG(_(e_funcref));
20322 else
20323 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020324 name = NULL;
20325 }
20326 goto theend;
20327 }
20328
20329 if (lv.ll_name == NULL)
20330 {
20331 /* Error found, but continue after the function name. */
20332 *pp = end;
20333 goto theend;
20334 }
20335
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020336 /* Check if the name is a Funcref. If so, use the value. */
20337 if (lv.ll_exp_name != NULL)
20338 {
20339 len = (int)STRLEN(lv.ll_exp_name);
20340 name = deref_func_name(lv.ll_exp_name, &len);
20341 if (name == lv.ll_exp_name)
20342 name = NULL;
20343 }
20344 else
20345 {
20346 len = (int)(end - *pp);
20347 name = deref_func_name(*pp, &len);
20348 if (name == *pp)
20349 name = NULL;
20350 }
20351 if (name != NULL)
20352 {
20353 name = vim_strsave(name);
20354 *pp = end;
20355 goto theend;
20356 }
20357
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020358 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020359 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020360 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020361 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20362 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20363 {
20364 /* When there was "s:" already or the name expanded to get a
20365 * leading "s:" then remove it. */
20366 lv.ll_name += 2;
20367 len -= 2;
20368 lead = 2;
20369 }
20370 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020371 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020372 {
20373 if (lead == 2) /* skip over "s:" */
20374 lv.ll_name += 2;
20375 len = (int)(end - lv.ll_name);
20376 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020377
20378 /*
20379 * Copy the function name to allocated memory.
20380 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20381 * Accept <SNR>123_name() outside a script.
20382 */
20383 if (skip)
20384 lead = 0; /* do nothing */
20385 else if (lead > 0)
20386 {
20387 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020388 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20389 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020390 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020391 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020392 if (current_SID <= 0)
20393 {
20394 EMSG(_(e_usingsid));
20395 goto theend;
20396 }
20397 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20398 lead += (int)STRLEN(sid_buf);
20399 }
20400 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020401 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020402 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020403 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020404 goto theend;
20405 }
20406 name = alloc((unsigned)(len + lead + 1));
20407 if (name != NULL)
20408 {
20409 if (lead > 0)
20410 {
20411 name[0] = K_SPECIAL;
20412 name[1] = KS_EXTRA;
20413 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020414 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020415 STRCPY(name + 3, sid_buf);
20416 }
20417 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20418 name[len + lead] = NUL;
20419 }
20420 *pp = end;
20421
20422theend:
20423 clear_lval(&lv);
20424 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425}
20426
20427/*
20428 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20429 * Return 2 if "p" starts with "s:".
20430 * Return 0 otherwise.
20431 */
20432 static int
20433eval_fname_script(p)
20434 char_u *p;
20435{
20436 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20437 || STRNICMP(p + 1, "SNR>", 4) == 0))
20438 return 5;
20439 if (p[0] == 's' && p[1] == ':')
20440 return 2;
20441 return 0;
20442}
20443
20444/*
20445 * Return TRUE if "p" starts with "<SID>" or "s:".
20446 * Only works if eval_fname_script() returned non-zero for "p"!
20447 */
20448 static int
20449eval_fname_sid(p)
20450 char_u *p;
20451{
20452 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20453}
20454
20455/*
20456 * List the head of the function: "name(arg1, arg2)".
20457 */
20458 static void
20459list_func_head(fp, indent)
20460 ufunc_T *fp;
20461 int indent;
20462{
20463 int j;
20464
20465 msg_start();
20466 if (indent)
20467 MSG_PUTS(" ");
20468 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020469 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470 {
20471 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020472 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 }
20474 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020475 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020477 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478 {
20479 if (j)
20480 MSG_PUTS(", ");
20481 msg_puts(FUNCARG(fp, j));
20482 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020483 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020484 {
20485 if (j)
20486 MSG_PUTS(", ");
20487 MSG_PUTS("...");
20488 }
20489 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020490 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020491 if (p_verbose > 0)
20492 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493}
20494
20495/*
20496 * Find a function by name, return pointer to it in ufuncs.
20497 * Return NULL for unknown function.
20498 */
20499 static ufunc_T *
20500find_func(name)
20501 char_u *name;
20502{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020503 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020504
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020505 hi = hash_find(&func_hashtab, name);
20506 if (!HASHITEM_EMPTY(hi))
20507 return HI2UF(hi);
20508 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509}
20510
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020511#if defined(EXITFREE) || defined(PROTO)
20512 void
20513free_all_functions()
20514{
20515 hashitem_T *hi;
20516
20517 /* Need to start all over every time, because func_free() may change the
20518 * hash table. */
20519 while (func_hashtab.ht_used > 0)
20520 for (hi = func_hashtab.ht_array; ; ++hi)
20521 if (!HASHITEM_EMPTY(hi))
20522 {
20523 func_free(HI2UF(hi));
20524 break;
20525 }
20526}
20527#endif
20528
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020529/*
20530 * Return TRUE if a function "name" exists.
20531 */
20532 static int
20533function_exists(name)
20534 char_u *name;
20535{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020536 char_u *nm = name;
20537 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020538 int n = FALSE;
20539
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020540 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020541 nm = skipwhite(nm);
20542
20543 /* Only accept "funcname", "funcname ", "funcname (..." and
20544 * "funcname(...", not "funcname!...". */
20545 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020546 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020547 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020548 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020549 else
20550 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020551 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020552 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020553 return n;
20554}
20555
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020556/*
20557 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020558 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020559 */
20560 static int
20561builtin_function(name)
20562 char_u *name;
20563{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020564 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20565 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020566}
20567
Bram Moolenaar05159a02005-02-26 23:04:13 +000020568#if defined(FEAT_PROFILE) || defined(PROTO)
20569/*
20570 * Start profiling function "fp".
20571 */
20572 static void
20573func_do_profile(fp)
20574 ufunc_T *fp;
20575{
20576 fp->uf_tm_count = 0;
20577 profile_zero(&fp->uf_tm_self);
20578 profile_zero(&fp->uf_tm_total);
20579 if (fp->uf_tml_count == NULL)
20580 fp->uf_tml_count = (int *)alloc_clear((unsigned)
20581 (sizeof(int) * fp->uf_lines.ga_len));
20582 if (fp->uf_tml_total == NULL)
20583 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
20584 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20585 if (fp->uf_tml_self == NULL)
20586 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
20587 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20588 fp->uf_tml_idx = -1;
20589 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
20590 || fp->uf_tml_self == NULL)
20591 return; /* out of memory */
20592
20593 fp->uf_profiling = TRUE;
20594}
20595
20596/*
20597 * Dump the profiling results for all functions in file "fd".
20598 */
20599 void
20600func_dump_profile(fd)
20601 FILE *fd;
20602{
20603 hashitem_T *hi;
20604 int todo;
20605 ufunc_T *fp;
20606 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000020607 ufunc_T **sorttab;
20608 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020609
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020610 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020611 if (todo == 0)
20612 return; /* nothing to dump */
20613
Bram Moolenaar73830342005-02-28 22:48:19 +000020614 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
20615
Bram Moolenaar05159a02005-02-26 23:04:13 +000020616 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
20617 {
20618 if (!HASHITEM_EMPTY(hi))
20619 {
20620 --todo;
20621 fp = HI2UF(hi);
20622 if (fp->uf_profiling)
20623 {
Bram Moolenaar73830342005-02-28 22:48:19 +000020624 if (sorttab != NULL)
20625 sorttab[st_len++] = fp;
20626
Bram Moolenaar05159a02005-02-26 23:04:13 +000020627 if (fp->uf_name[0] == K_SPECIAL)
20628 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
20629 else
20630 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
20631 if (fp->uf_tm_count == 1)
20632 fprintf(fd, "Called 1 time\n");
20633 else
20634 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
20635 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
20636 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
20637 fprintf(fd, "\n");
20638 fprintf(fd, "count total (s) self (s)\n");
20639
20640 for (i = 0; i < fp->uf_lines.ga_len; ++i)
20641 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020642 if (FUNCLINE(fp, i) == NULL)
20643 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000020644 prof_func_line(fd, fp->uf_tml_count[i],
20645 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020646 fprintf(fd, "%s\n", FUNCLINE(fp, i));
20647 }
20648 fprintf(fd, "\n");
20649 }
20650 }
20651 }
Bram Moolenaar73830342005-02-28 22:48:19 +000020652
20653 if (sorttab != NULL && st_len > 0)
20654 {
20655 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20656 prof_total_cmp);
20657 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
20658 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20659 prof_self_cmp);
20660 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
20661 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020662
20663 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020664}
Bram Moolenaar73830342005-02-28 22:48:19 +000020665
20666 static void
20667prof_sort_list(fd, sorttab, st_len, title, prefer_self)
20668 FILE *fd;
20669 ufunc_T **sorttab;
20670 int st_len;
20671 char *title;
20672 int prefer_self; /* when equal print only self time */
20673{
20674 int i;
20675 ufunc_T *fp;
20676
20677 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
20678 fprintf(fd, "count total (s) self (s) function\n");
20679 for (i = 0; i < 20 && i < st_len; ++i)
20680 {
20681 fp = sorttab[i];
20682 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
20683 prefer_self);
20684 if (fp->uf_name[0] == K_SPECIAL)
20685 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
20686 else
20687 fprintf(fd, " %s()\n", fp->uf_name);
20688 }
20689 fprintf(fd, "\n");
20690}
20691
20692/*
20693 * Print the count and times for one function or function line.
20694 */
20695 static void
20696prof_func_line(fd, count, total, self, prefer_self)
20697 FILE *fd;
20698 int count;
20699 proftime_T *total;
20700 proftime_T *self;
20701 int prefer_self; /* when equal print only self time */
20702{
20703 if (count > 0)
20704 {
20705 fprintf(fd, "%5d ", count);
20706 if (prefer_self && profile_equal(total, self))
20707 fprintf(fd, " ");
20708 else
20709 fprintf(fd, "%s ", profile_msg(total));
20710 if (!prefer_self && profile_equal(total, self))
20711 fprintf(fd, " ");
20712 else
20713 fprintf(fd, "%s ", profile_msg(self));
20714 }
20715 else
20716 fprintf(fd, " ");
20717}
20718
20719/*
20720 * Compare function for total time sorting.
20721 */
20722 static int
20723#ifdef __BORLANDC__
20724_RTLENTRYF
20725#endif
20726prof_total_cmp(s1, s2)
20727 const void *s1;
20728 const void *s2;
20729{
20730 ufunc_T *p1, *p2;
20731
20732 p1 = *(ufunc_T **)s1;
20733 p2 = *(ufunc_T **)s2;
20734 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
20735}
20736
20737/*
20738 * Compare function for self time sorting.
20739 */
20740 static int
20741#ifdef __BORLANDC__
20742_RTLENTRYF
20743#endif
20744prof_self_cmp(s1, s2)
20745 const void *s1;
20746 const void *s2;
20747{
20748 ufunc_T *p1, *p2;
20749
20750 p1 = *(ufunc_T **)s1;
20751 p2 = *(ufunc_T **)s2;
20752 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
20753}
20754
Bram Moolenaar05159a02005-02-26 23:04:13 +000020755#endif
20756
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020757/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020758 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020759 * Return TRUE if a package was loaded.
20760 */
20761 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020762script_autoload(name, reload)
20763 char_u *name;
20764 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020765{
20766 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020767 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020768 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020769 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020770
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020771 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020772 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020773 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020774 return FALSE;
20775
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020776 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020777
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020778 /* Find the name in the list of previously loaded package names. Skip
20779 * "autoload/", it's always the same. */
20780 for (i = 0; i < ga_loaded.ga_len; ++i)
20781 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
20782 break;
20783 if (!reload && i < ga_loaded.ga_len)
20784 ret = FALSE; /* was loaded already */
20785 else
20786 {
20787 /* Remember the name if it wasn't loaded already. */
20788 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
20789 {
20790 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
20791 tofree = NULL;
20792 }
20793
20794 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000020795 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020796 ret = TRUE;
20797 }
20798
20799 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020800 return ret;
20801}
20802
20803/*
20804 * Return the autoload script name for a function or variable name.
20805 * Returns NULL when out of memory.
20806 */
20807 static char_u *
20808autoload_name(name)
20809 char_u *name;
20810{
20811 char_u *p;
20812 char_u *scriptname;
20813
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020814 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020815 scriptname = alloc((unsigned)(STRLEN(name) + 14));
20816 if (scriptname == NULL)
20817 return FALSE;
20818 STRCPY(scriptname, "autoload/");
20819 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020820 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020821 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020822 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020823 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020824 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020825}
20826
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20828
20829/*
20830 * Function given to ExpandGeneric() to obtain the list of user defined
20831 * function names.
20832 */
20833 char_u *
20834get_user_func_name(xp, idx)
20835 expand_T *xp;
20836 int idx;
20837{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020838 static long_u done;
20839 static hashitem_T *hi;
20840 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841
20842 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020843 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020844 done = 0;
20845 hi = func_hashtab.ht_array;
20846 }
20847 if (done < func_hashtab.ht_used)
20848 {
20849 if (done++ > 0)
20850 ++hi;
20851 while (HASHITEM_EMPTY(hi))
20852 ++hi;
20853 fp = HI2UF(hi);
20854
20855 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
20856 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857
20858 cat_func_name(IObuff, fp);
20859 if (xp->xp_context != EXPAND_USER_FUNC)
20860 {
20861 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020862 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863 STRCAT(IObuff, ")");
20864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020865 return IObuff;
20866 }
20867 return NULL;
20868}
20869
20870#endif /* FEAT_CMDL_COMPL */
20871
20872/*
20873 * Copy the function name of "fp" to buffer "buf".
20874 * "buf" must be able to hold the function name plus three bytes.
20875 * Takes care of script-local function names.
20876 */
20877 static void
20878cat_func_name(buf, fp)
20879 char_u *buf;
20880 ufunc_T *fp;
20881{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020882 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883 {
20884 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020885 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886 }
20887 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020888 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889}
20890
20891/*
20892 * ":delfunction {name}"
20893 */
20894 void
20895ex_delfunction(eap)
20896 exarg_T *eap;
20897{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020898 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899 char_u *p;
20900 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020901 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902
20903 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020904 name = trans_function_name(&p, eap->skip, 0, &fudi);
20905 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020907 {
20908 if (fudi.fd_dict != NULL && !eap->skip)
20909 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912 if (!ends_excmd(*skipwhite(p)))
20913 {
20914 vim_free(name);
20915 EMSG(_(e_trailing));
20916 return;
20917 }
20918 eap->nextcmd = check_nextcmd(p);
20919 if (eap->nextcmd != NULL)
20920 *p = NUL;
20921
20922 if (!eap->skip)
20923 fp = find_func(name);
20924 vim_free(name);
20925
20926 if (!eap->skip)
20927 {
20928 if (fp == NULL)
20929 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020930 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931 return;
20932 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020933 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934 {
20935 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
20936 return;
20937 }
20938
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020939 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020940 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020941 /* Delete the dict item that refers to the function, it will
20942 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020943 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020945 else
20946 func_free(fp);
20947 }
20948}
20949
20950/*
20951 * Free a function and remove it from the list of functions.
20952 */
20953 static void
20954func_free(fp)
20955 ufunc_T *fp;
20956{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020957 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020958
20959 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020960 ga_clear_strings(&(fp->uf_args));
20961 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020962#ifdef FEAT_PROFILE
20963 vim_free(fp->uf_tml_count);
20964 vim_free(fp->uf_tml_total);
20965 vim_free(fp->uf_tml_self);
20966#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020967
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020968 /* remove the function from the function hashtable */
20969 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20970 if (HASHITEM_EMPTY(hi))
20971 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020972 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020973 hash_remove(&func_hashtab, hi);
20974
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020975 vim_free(fp);
20976}
20977
20978/*
20979 * Unreference a Function: decrement the reference count and free it when it
20980 * becomes zero. Only for numbered functions.
20981 */
20982 static void
20983func_unref(name)
20984 char_u *name;
20985{
20986 ufunc_T *fp;
20987
20988 if (name != NULL && isdigit(*name))
20989 {
20990 fp = find_func(name);
20991 if (fp == NULL)
20992 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020993 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020994 {
20995 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020996 * when "uf_calls" becomes zero. */
20997 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020998 func_free(fp);
20999 }
21000 }
21001}
21002
21003/*
21004 * Count a reference to a Function.
21005 */
21006 static void
21007func_ref(name)
21008 char_u *name;
21009{
21010 ufunc_T *fp;
21011
21012 if (name != NULL && isdigit(*name))
21013 {
21014 fp = find_func(name);
21015 if (fp == NULL)
21016 EMSG2(_(e_intern2), "func_ref()");
21017 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021018 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 }
21020}
21021
21022/*
21023 * Call a user function.
21024 */
21025 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021026call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021027 ufunc_T *fp; /* pointer to function */
21028 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021029 typval_T *argvars; /* arguments */
21030 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031 linenr_T firstline; /* first line of range */
21032 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021033 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034{
Bram Moolenaar33570922005-01-25 22:26:29 +000021035 char_u *save_sourcing_name;
21036 linenr_T save_sourcing_lnum;
21037 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021038 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021039 int save_did_emsg;
21040 static int depth = 0;
21041 dictitem_T *v;
21042 int fixvar_idx = 0; /* index in fixvar[] */
21043 int i;
21044 int ai;
21045 char_u numbuf[NUMBUFLEN];
21046 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021047#ifdef FEAT_PROFILE
21048 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021049 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021051
21052 /* If depth of calling is getting too high, don't execute the function */
21053 if (depth >= p_mfd)
21054 {
21055 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021056 rettv->v_type = VAR_NUMBER;
21057 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021058 return;
21059 }
21060 ++depth;
21061
21062 line_breakcheck(); /* check for CTRL-C hit */
21063
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021064 fc = (funccall_T *)alloc(sizeof(funccall_T));
21065 fc->caller = current_funccal;
21066 current_funccal = fc;
21067 fc->func = fp;
21068 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021069 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021070 fc->linenr = 0;
21071 fc->returned = FALSE;
21072 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021073 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021074 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21075 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076
Bram Moolenaar33570922005-01-25 22:26:29 +000021077 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021078 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021079 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21080 * each argument variable and saves a lot of time.
21081 */
21082 /*
21083 * Init l: variables.
21084 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021085 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021086 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021087 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021088 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21089 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021090 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021091 name = v->di_key;
21092 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021093 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021094 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021095 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021096 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021097 v->di_tv.vval.v_dict = selfdict;
21098 ++selfdict->dv_refcount;
21099 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021100
Bram Moolenaar33570922005-01-25 22:26:29 +000021101 /*
21102 * Init a: variables.
21103 * Set a:0 to "argcount".
21104 * Set a:000 to a list with room for the "..." arguments.
21105 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021106 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21107 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021108 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021109 /* Use "name" to avoid a warning from some compiler that checks the
21110 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021111 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021112 name = v->di_key;
21113 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021114 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021115 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021116 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021117 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021118 v->di_tv.vval.v_list = &fc->l_varlist;
21119 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21120 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21121 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021122
21123 /*
21124 * Set a:firstline to "firstline" and a:lastline to "lastline".
21125 * Set a:name to named arguments.
21126 * Set a:N to the "..." arguments.
21127 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021128 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021129 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021130 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021131 (varnumber_T)lastline);
21132 for (i = 0; i < argcount; ++i)
21133 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021134 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021135 if (ai < 0)
21136 /* named argument a:name */
21137 name = FUNCARG(fp, i);
21138 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021139 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021140 /* "..." argument a:1, a:2, etc. */
21141 sprintf((char *)numbuf, "%d", ai + 1);
21142 name = numbuf;
21143 }
21144 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21145 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021146 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021147 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21148 }
21149 else
21150 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021151 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21152 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021153 if (v == NULL)
21154 break;
21155 v->di_flags = DI_FLAGS_RO;
21156 }
21157 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021158 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021159
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021160 /* Note: the values are copied directly to avoid alloc/free.
21161 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021162 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021163 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021164
21165 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21166 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021167 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21168 fc->l_listitems[ai].li_tv = argvars[i];
21169 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021170 }
21171 }
21172
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 /* Don't redraw while executing the function. */
21174 ++RedrawingDisabled;
21175 save_sourcing_name = sourcing_name;
21176 save_sourcing_lnum = sourcing_lnum;
21177 sourcing_lnum = 1;
21178 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021179 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180 if (sourcing_name != NULL)
21181 {
21182 if (save_sourcing_name != NULL
21183 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21184 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21185 else
21186 STRCPY(sourcing_name, "function ");
21187 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21188
21189 if (p_verbose >= 12)
21190 {
21191 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021192 verbose_enter_scroll();
21193
Bram Moolenaar555b2802005-05-19 21:08:39 +000021194 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 if (p_verbose >= 14)
21196 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021198 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021199 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021200 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201
21202 msg_puts((char_u *)"(");
21203 for (i = 0; i < argcount; ++i)
21204 {
21205 if (i > 0)
21206 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021207 if (argvars[i].v_type == VAR_NUMBER)
21208 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 else
21210 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021211 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21212 if (s != NULL)
21213 {
21214 trunc_string(s, buf, MSG_BUF_CLEN);
21215 msg_puts(buf);
21216 vim_free(tofree);
21217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218 }
21219 }
21220 msg_puts((char_u *)")");
21221 }
21222 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021223
21224 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225 --no_wait_return;
21226 }
21227 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021228#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021229 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021230 {
21231 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21232 func_do_profile(fp);
21233 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021234 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021235 {
21236 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021237 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021238 profile_zero(&fp->uf_tm_children);
21239 }
21240 script_prof_save(&wait_start);
21241 }
21242#endif
21243
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021245 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021246 save_did_emsg = did_emsg;
21247 did_emsg = FALSE;
21248
21249 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021250 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21252
21253 --RedrawingDisabled;
21254
21255 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021256 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021258 clear_tv(rettv);
21259 rettv->v_type = VAR_NUMBER;
21260 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261 }
21262
Bram Moolenaar05159a02005-02-26 23:04:13 +000021263#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021264 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021265 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021266 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021267 profile_end(&call_start);
21268 profile_sub_wait(&wait_start, &call_start);
21269 profile_add(&fp->uf_tm_total, &call_start);
21270 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021271 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021272 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021273 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21274 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021275 }
21276 }
21277#endif
21278
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279 /* when being verbose, mention the return value */
21280 if (p_verbose >= 12)
21281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021283 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284
Bram Moolenaar071d4272004-06-13 20:20:40 +000021285 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021286 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021287 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021288 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021289 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021290 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021292 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021293 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021294 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021295 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021296
Bram Moolenaar555b2802005-05-19 21:08:39 +000021297 /* The value may be very long. Skip the middle part, so that we
21298 * have some idea how it starts and ends. smsg() would always
21299 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021300 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021301 if (s != NULL)
21302 {
21303 trunc_string(s, buf, MSG_BUF_CLEN);
21304 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21305 vim_free(tofree);
21306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021307 }
21308 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021309
21310 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311 --no_wait_return;
21312 }
21313
21314 vim_free(sourcing_name);
21315 sourcing_name = save_sourcing_name;
21316 sourcing_lnum = save_sourcing_lnum;
21317 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021318#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021319 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021320 script_prof_restore(&wait_start);
21321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322
21323 if (p_verbose >= 12 && sourcing_name != NULL)
21324 {
21325 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021326 verbose_enter_scroll();
21327
Bram Moolenaar555b2802005-05-19 21:08:39 +000021328 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021330
21331 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332 --no_wait_return;
21333 }
21334
21335 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021336 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021338
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021339 /* If the a:000 list and the l: and a: dicts are not referenced we can
21340 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021341 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21342 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21343 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21344 {
21345 free_funccal(fc, FALSE);
21346 }
21347 else
21348 {
21349 hashitem_T *hi;
21350 listitem_T *li;
21351 int todo;
21352
21353 /* "fc" is still in use. This can happen when returning "a:000" or
21354 * assigning "l:" to a global variable.
21355 * Link "fc" in the list for garbage collection later. */
21356 fc->caller = previous_funccal;
21357 previous_funccal = fc;
21358
21359 /* Make a copy of the a: variables, since we didn't do that above. */
21360 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21361 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21362 {
21363 if (!HASHITEM_EMPTY(hi))
21364 {
21365 --todo;
21366 v = HI2DI(hi);
21367 copy_tv(&v->di_tv, &v->di_tv);
21368 }
21369 }
21370
21371 /* Make a copy of the a:000 items, since we didn't do that above. */
21372 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21373 copy_tv(&li->li_tv, &li->li_tv);
21374 }
21375}
21376
21377/*
21378 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021379 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021380 */
21381 static int
21382can_free_funccal(fc, copyID)
21383 funccall_T *fc;
21384 int copyID;
21385{
21386 return (fc->l_varlist.lv_copyID != copyID
21387 && fc->l_vars.dv_copyID != copyID
21388 && fc->l_avars.dv_copyID != copyID);
21389}
21390
21391/*
21392 * Free "fc" and what it contains.
21393 */
21394 static void
21395free_funccal(fc, free_val)
21396 funccall_T *fc;
21397 int free_val; /* a: vars were allocated */
21398{
21399 listitem_T *li;
21400
21401 /* The a: variables typevals may not have been allocated, only free the
21402 * allocated variables. */
21403 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21404
21405 /* free all l: variables */
21406 vars_clear(&fc->l_vars.dv_hashtab);
21407
21408 /* Free the a:000 variables if they were allocated. */
21409 if (free_val)
21410 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21411 clear_tv(&li->li_tv);
21412
21413 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414}
21415
21416/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021417 * Add a number variable "name" to dict "dp" with value "nr".
21418 */
21419 static void
21420add_nr_var(dp, v, name, nr)
21421 dict_T *dp;
21422 dictitem_T *v;
21423 char *name;
21424 varnumber_T nr;
21425{
21426 STRCPY(v->di_key, name);
21427 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21428 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21429 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021430 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021431 v->di_tv.vval.v_number = nr;
21432}
21433
21434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 * ":return [expr]"
21436 */
21437 void
21438ex_return(eap)
21439 exarg_T *eap;
21440{
21441 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021442 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443 int returning = FALSE;
21444
21445 if (current_funccal == NULL)
21446 {
21447 EMSG(_("E133: :return not inside a function"));
21448 return;
21449 }
21450
21451 if (eap->skip)
21452 ++emsg_skip;
21453
21454 eap->nextcmd = NULL;
21455 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021456 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457 {
21458 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021459 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021461 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021462 }
21463 /* It's safer to return also on error. */
21464 else if (!eap->skip)
21465 {
21466 /*
21467 * Return unless the expression evaluation has been cancelled due to an
21468 * aborting error, an interrupt, or an exception.
21469 */
21470 if (!aborting())
21471 returning = do_return(eap, FALSE, TRUE, NULL);
21472 }
21473
21474 /* When skipping or the return gets pending, advance to the next command
21475 * in this line (!returning). Otherwise, ignore the rest of the line.
21476 * Following lines will be ignored by get_func_line(). */
21477 if (returning)
21478 eap->nextcmd = NULL;
21479 else if (eap->nextcmd == NULL) /* no argument */
21480 eap->nextcmd = check_nextcmd(arg);
21481
21482 if (eap->skip)
21483 --emsg_skip;
21484}
21485
21486/*
21487 * Return from a function. Possibly makes the return pending. Also called
21488 * for a pending return at the ":endtry" or after returning from an extra
21489 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021490 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021491 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492 * FALSE when the return gets pending.
21493 */
21494 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021495do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496 exarg_T *eap;
21497 int reanimate;
21498 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021499 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500{
21501 int idx;
21502 struct condstack *cstack = eap->cstack;
21503
21504 if (reanimate)
21505 /* Undo the return. */
21506 current_funccal->returned = FALSE;
21507
21508 /*
21509 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21510 * not in its finally clause (which then is to be executed next) is found.
21511 * In this case, make the ":return" pending for execution at the ":endtry".
21512 * Otherwise, return normally.
21513 */
21514 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21515 if (idx >= 0)
21516 {
21517 cstack->cs_pending[idx] = CSTP_RETURN;
21518
21519 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021520 /* A pending return again gets pending. "rettv" points to an
21521 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021523 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 else
21525 {
21526 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021527 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021528 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021529 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021531 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 {
21533 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021534 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021535 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536 else
21537 EMSG(_(e_outofmem));
21538 }
21539 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021540 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541
21542 if (reanimate)
21543 {
21544 /* The pending return value could be overwritten by a ":return"
21545 * without argument in a finally clause; reset the default
21546 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021547 current_funccal->rettv->v_type = VAR_NUMBER;
21548 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021549 }
21550 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021551 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021552 }
21553 else
21554 {
21555 current_funccal->returned = TRUE;
21556
21557 /* If the return is carried out now, store the return value. For
21558 * a return immediately after reanimation, the value is already
21559 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021560 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021562 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021563 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021565 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566 }
21567 }
21568
21569 return idx < 0;
21570}
21571
21572/*
21573 * Free the variable with a pending return value.
21574 */
21575 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021576discard_pending_return(rettv)
21577 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578{
Bram Moolenaar33570922005-01-25 22:26:29 +000021579 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580}
21581
21582/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021583 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000021584 * is an allocated string. Used by report_pending() for verbose messages.
21585 */
21586 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021587get_return_cmd(rettv)
21588 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021590 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021591 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021592 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021594 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021595 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021596 if (s == NULL)
21597 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021598
21599 STRCPY(IObuff, ":return ");
21600 STRNCPY(IObuff + 8, s, IOSIZE - 8);
21601 if (STRLEN(s) + 8 >= IOSIZE)
21602 STRCPY(IObuff + IOSIZE - 4, "...");
21603 vim_free(tofree);
21604 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605}
21606
21607/*
21608 * Get next function line.
21609 * Called by do_cmdline() to get the next line.
21610 * Returns allocated string, or NULL for end of function.
21611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612 char_u *
21613get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000021614 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000021616 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617{
Bram Moolenaar33570922005-01-25 22:26:29 +000021618 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021619 ufunc_T *fp = fcp->func;
21620 char_u *retval;
21621 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622
21623 /* If breakpoints have been added/deleted need to check for it. */
21624 if (fcp->dbg_tick != debug_tick)
21625 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021626 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627 sourcing_lnum);
21628 fcp->dbg_tick = debug_tick;
21629 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021630#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021631 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021632 func_line_end(cookie);
21633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634
Bram Moolenaar05159a02005-02-26 23:04:13 +000021635 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021636 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
21637 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 retval = NULL;
21639 else
21640 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021641 /* Skip NULL lines (continuation lines). */
21642 while (fcp->linenr < gap->ga_len
21643 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
21644 ++fcp->linenr;
21645 if (fcp->linenr >= gap->ga_len)
21646 retval = NULL;
21647 else
21648 {
21649 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
21650 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021651#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021652 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021653 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021654#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 }
21657
21658 /* Did we encounter a breakpoint? */
21659 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
21660 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021661 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021663 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021664 sourcing_lnum);
21665 fcp->dbg_tick = debug_tick;
21666 }
21667
21668 return retval;
21669}
21670
Bram Moolenaar05159a02005-02-26 23:04:13 +000021671#if defined(FEAT_PROFILE) || defined(PROTO)
21672/*
21673 * Called when starting to read a function line.
21674 * "sourcing_lnum" must be correct!
21675 * When skipping lines it may not actually be executed, but we won't find out
21676 * until later and we need to store the time now.
21677 */
21678 void
21679func_line_start(cookie)
21680 void *cookie;
21681{
21682 funccall_T *fcp = (funccall_T *)cookie;
21683 ufunc_T *fp = fcp->func;
21684
21685 if (fp->uf_profiling && sourcing_lnum >= 1
21686 && sourcing_lnum <= fp->uf_lines.ga_len)
21687 {
21688 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021689 /* Skip continuation lines. */
21690 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
21691 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021692 fp->uf_tml_execed = FALSE;
21693 profile_start(&fp->uf_tml_start);
21694 profile_zero(&fp->uf_tml_children);
21695 profile_get_wait(&fp->uf_tml_wait);
21696 }
21697}
21698
21699/*
21700 * Called when actually executing a function line.
21701 */
21702 void
21703func_line_exec(cookie)
21704 void *cookie;
21705{
21706 funccall_T *fcp = (funccall_T *)cookie;
21707 ufunc_T *fp = fcp->func;
21708
21709 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21710 fp->uf_tml_execed = TRUE;
21711}
21712
21713/*
21714 * Called when done with a function line.
21715 */
21716 void
21717func_line_end(cookie)
21718 void *cookie;
21719{
21720 funccall_T *fcp = (funccall_T *)cookie;
21721 ufunc_T *fp = fcp->func;
21722
21723 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21724 {
21725 if (fp->uf_tml_execed)
21726 {
21727 ++fp->uf_tml_count[fp->uf_tml_idx];
21728 profile_end(&fp->uf_tml_start);
21729 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021730 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000021731 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
21732 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021733 }
21734 fp->uf_tml_idx = -1;
21735 }
21736}
21737#endif
21738
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739/*
21740 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021741 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742 */
21743 int
21744func_has_ended(cookie)
21745 void *cookie;
21746{
Bram Moolenaar33570922005-01-25 22:26:29 +000021747 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748
21749 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21750 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021751 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752 || fcp->returned);
21753}
21754
21755/*
21756 * return TRUE if cookie indicates a function which "abort"s on errors.
21757 */
21758 int
21759func_has_abort(cookie)
21760 void *cookie;
21761{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021762 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763}
21764
21765#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21766typedef enum
21767{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021768 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
21769 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
21770 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771} var_flavour_T;
21772
21773static var_flavour_T var_flavour __ARGS((char_u *varname));
21774
21775 static var_flavour_T
21776var_flavour(varname)
21777 char_u *varname;
21778{
21779 char_u *p = varname;
21780
21781 if (ASCII_ISUPPER(*p))
21782 {
21783 while (*(++p))
21784 if (ASCII_ISLOWER(*p))
21785 return VAR_FLAVOUR_SESSION;
21786 return VAR_FLAVOUR_VIMINFO;
21787 }
21788 else
21789 return VAR_FLAVOUR_DEFAULT;
21790}
21791#endif
21792
21793#if defined(FEAT_VIMINFO) || defined(PROTO)
21794/*
21795 * Restore global vars that start with a capital from the viminfo file
21796 */
21797 int
21798read_viminfo_varlist(virp, writing)
21799 vir_T *virp;
21800 int writing;
21801{
21802 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021803 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000021804 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021805
21806 if (!writing && (find_viminfo_parameter('!') != NULL))
21807 {
21808 tab = vim_strchr(virp->vir_line + 1, '\t');
21809 if (tab != NULL)
21810 {
21811 *tab++ = '\0'; /* isolate the variable name */
21812 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021813 type = VAR_STRING;
21814#ifdef FEAT_FLOAT
21815 else if (*tab == 'F')
21816 type = VAR_FLOAT;
21817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818
21819 tab = vim_strchr(tab, '\t');
21820 if (tab != NULL)
21821 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021822 tv.v_type = type;
21823 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021824 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021825 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021826#ifdef FEAT_FLOAT
21827 else if (type == VAR_FLOAT)
21828 (void)string2float(tab + 1, &tv.vval.v_float);
21829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021831 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021832 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021833 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021834 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835 }
21836 }
21837 }
21838
21839 return viminfo_readline(virp);
21840}
21841
21842/*
21843 * Write global vars that start with a capital to the viminfo file
21844 */
21845 void
21846write_viminfo_varlist(fp)
21847 FILE *fp;
21848{
Bram Moolenaar33570922005-01-25 22:26:29 +000021849 hashitem_T *hi;
21850 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021851 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021852 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021853 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021854 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021855 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856
21857 if (find_viminfo_parameter('!') == NULL)
21858 return;
21859
21860 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000021861
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021862 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021863 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021865 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021867 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021868 this_var = HI2DI(hi);
21869 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021870 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021871 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000021872 {
21873 case VAR_STRING: s = "STR"; break;
21874 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021875#ifdef FEAT_FLOAT
21876 case VAR_FLOAT: s = "FLO"; break;
21877#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000021878 default: continue;
21879 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021880 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021881 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021882 if (p != NULL)
21883 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000021884 vim_free(tofree);
21885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 }
21887 }
21888}
21889#endif
21890
21891#if defined(FEAT_SESSION) || defined(PROTO)
21892 int
21893store_session_globals(fd)
21894 FILE *fd;
21895{
Bram Moolenaar33570922005-01-25 22:26:29 +000021896 hashitem_T *hi;
21897 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021898 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899 char_u *p, *t;
21900
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021901 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021902 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021903 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021904 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021906 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021907 this_var = HI2DI(hi);
21908 if ((this_var->di_tv.v_type == VAR_NUMBER
21909 || this_var->di_tv.v_type == VAR_STRING)
21910 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021911 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021912 /* Escape special characters with a backslash. Turn a LF and
21913 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021914 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000021915 (char_u *)"\\\"\n\r");
21916 if (p == NULL) /* out of memory */
21917 break;
21918 for (t = p; *t != NUL; ++t)
21919 if (*t == '\n')
21920 *t = 'n';
21921 else if (*t == '\r')
21922 *t = 'r';
21923 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000021924 this_var->di_key,
21925 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21926 : ' ',
21927 p,
21928 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21929 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000021930 || put_eol(fd) == FAIL)
21931 {
21932 vim_free(p);
21933 return FAIL;
21934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021935 vim_free(p);
21936 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021937#ifdef FEAT_FLOAT
21938 else if (this_var->di_tv.v_type == VAR_FLOAT
21939 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
21940 {
21941 float_T f = this_var->di_tv.vval.v_float;
21942 int sign = ' ';
21943
21944 if (f < 0)
21945 {
21946 f = -f;
21947 sign = '-';
21948 }
21949 if ((fprintf(fd, "let %s = %c&%f",
21950 this_var->di_key, sign, f) < 0)
21951 || put_eol(fd) == FAIL)
21952 return FAIL;
21953 }
21954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955 }
21956 }
21957 return OK;
21958}
21959#endif
21960
Bram Moolenaar661b1822005-07-28 22:36:45 +000021961/*
21962 * Display script name where an item was last set.
21963 * Should only be invoked when 'verbose' is non-zero.
21964 */
21965 void
21966last_set_msg(scriptID)
21967 scid_T scriptID;
21968{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021969 char_u *p;
21970
Bram Moolenaar661b1822005-07-28 22:36:45 +000021971 if (scriptID != 0)
21972 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021973 p = home_replace_save(NULL, get_scriptname(scriptID));
21974 if (p != NULL)
21975 {
21976 verbose_enter();
21977 MSG_PUTS(_("\n\tLast set from "));
21978 MSG_PUTS(p);
21979 vim_free(p);
21980 verbose_leave();
21981 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000021982 }
21983}
21984
Bram Moolenaard812df62008-11-09 12:46:09 +000021985/*
21986 * List v:oldfiles in a nice way.
21987 */
Bram Moolenaard812df62008-11-09 12:46:09 +000021988 void
21989ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000021990 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000021991{
21992 list_T *l = vimvars[VV_OLDFILES].vv_list;
21993 listitem_T *li;
21994 int nr = 0;
21995
21996 if (l == NULL)
21997 msg((char_u *)_("No old files"));
21998 else
21999 {
22000 msg_start();
22001 msg_scroll = TRUE;
22002 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22003 {
22004 msg_outnum((long)++nr);
22005 MSG_PUTS(": ");
22006 msg_outtrans(get_tv_string(&li->li_tv));
22007 msg_putchar('\n');
22008 out_flush(); /* output one line at a time */
22009 ui_breakcheck();
22010 }
22011 /* Assume "got_int" was set to truncate the listing. */
22012 got_int = FALSE;
22013
22014#ifdef FEAT_BROWSE_CMD
22015 if (cmdmod.browse)
22016 {
22017 quit_more = FALSE;
22018 nr = prompt_for_number(FALSE);
22019 msg_starthere();
22020 if (nr > 0)
22021 {
22022 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22023 (long)nr);
22024
22025 if (p != NULL)
22026 {
22027 p = expand_env_save(p);
22028 eap->arg = p;
22029 eap->cmdidx = CMD_edit;
22030 cmdmod.browse = FALSE;
22031 do_exedit(eap, NULL);
22032 vim_free(p);
22033 }
22034 }
22035 }
22036#endif
22037 }
22038}
22039
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040#endif /* FEAT_EVAL */
22041
Bram Moolenaar071d4272004-06-13 20:20:40 +000022042
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022043#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044
22045#ifdef WIN3264
22046/*
22047 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22048 */
22049static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22050static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22051static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22052
22053/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022054 * Get the short path (8.3) for the filename in "fnamep".
22055 * Only works for a valid file name.
22056 * When the path gets longer "fnamep" is changed and the allocated buffer
22057 * is put in "bufp".
22058 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22059 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060 */
22061 static int
22062get_short_pathname(fnamep, bufp, fnamelen)
22063 char_u **fnamep;
22064 char_u **bufp;
22065 int *fnamelen;
22066{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022067 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068 char_u *newbuf;
22069
22070 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 l = GetShortPathName(*fnamep, *fnamep, len);
22072 if (l > len - 1)
22073 {
22074 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022075 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076 newbuf = vim_strnsave(*fnamep, l);
22077 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022078 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022079
22080 vim_free(*bufp);
22081 *fnamep = *bufp = newbuf;
22082
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022083 /* Really should always succeed, as the buffer is big enough. */
22084 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085 }
22086
22087 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022088 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089}
22090
22091/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022092 * Get the short path (8.3) for the filename in "fname". The converted
22093 * path is returned in "bufp".
22094 *
22095 * Some of the directories specified in "fname" may not exist. This function
22096 * will shorten the existing directories at the beginning of the path and then
22097 * append the remaining non-existing path.
22098 *
22099 * fname - Pointer to the filename to shorten. On return, contains the
22100 * pointer to the shortened pathname
22101 * bufp - Pointer to an allocated buffer for the filename.
22102 * fnamelen - Length of the filename pointed to by fname
22103 *
22104 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105 */
22106 static int
22107shortpath_for_invalid_fname(fname, bufp, fnamelen)
22108 char_u **fname;
22109 char_u **bufp;
22110 int *fnamelen;
22111{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022112 char_u *short_fname, *save_fname, *pbuf_unused;
22113 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022115 int old_len, len;
22116 int new_len, sfx_len;
22117 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118
22119 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022120 old_len = *fnamelen;
22121 save_fname = vim_strnsave(*fname, old_len);
22122 pbuf_unused = NULL;
22123 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022125 endp = save_fname + old_len - 1; /* Find the end of the copy */
22126 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022128 /*
22129 * Try shortening the supplied path till it succeeds by removing one
22130 * directory at a time from the tail of the path.
22131 */
22132 len = 0;
22133 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022134 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022135 /* go back one path-separator */
22136 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22137 --endp;
22138 if (endp <= save_fname)
22139 break; /* processed the complete path */
22140
22141 /*
22142 * Replace the path separator with a NUL and try to shorten the
22143 * resulting path.
22144 */
22145 ch = *endp;
22146 *endp = 0;
22147 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022148 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022149 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22150 {
22151 retval = FAIL;
22152 goto theend;
22153 }
22154 *endp = ch; /* preserve the string */
22155
22156 if (len > 0)
22157 break; /* successfully shortened the path */
22158
22159 /* failed to shorten the path. Skip the path separator */
22160 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 }
22162
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022163 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022165 /*
22166 * Succeeded in shortening the path. Now concatenate the shortened
22167 * path with the remaining path at the tail.
22168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022170 /* Compute the length of the new path. */
22171 sfx_len = (int)(save_endp - endp) + 1;
22172 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022174 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022176 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022178 /* There is not enough space in the currently allocated string,
22179 * copy it to a buffer big enough. */
22180 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022181 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022182 {
22183 retval = FAIL;
22184 goto theend;
22185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022186 }
22187 else
22188 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022189 /* Transfer short_fname to the main buffer (it's big enough),
22190 * unless get_short_pathname() did its work in-place. */
22191 *fname = *bufp = save_fname;
22192 if (short_fname != save_fname)
22193 vim_strncpy(save_fname, short_fname, len);
22194 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022195 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022196
22197 /* concat the not-shortened part of the path */
22198 vim_strncpy(*fname + len, endp, sfx_len);
22199 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022201
22202theend:
22203 vim_free(pbuf_unused);
22204 vim_free(save_fname);
22205
22206 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207}
22208
22209/*
22210 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022211 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212 */
22213 static int
22214shortpath_for_partial(fnamep, bufp, fnamelen)
22215 char_u **fnamep;
22216 char_u **bufp;
22217 int *fnamelen;
22218{
22219 int sepcount, len, tflen;
22220 char_u *p;
22221 char_u *pbuf, *tfname;
22222 int hasTilde;
22223
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022224 /* Count up the path separators from the RHS.. so we know which part
22225 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022227 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228 if (vim_ispathsep(*p))
22229 ++sepcount;
22230
22231 /* Need full path first (use expand_env() to remove a "~/") */
22232 hasTilde = (**fnamep == '~');
22233 if (hasTilde)
22234 pbuf = tfname = expand_env_save(*fnamep);
22235 else
22236 pbuf = tfname = FullName_save(*fnamep, FALSE);
22237
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022238 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022240 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22241 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242
22243 if (len == 0)
22244 {
22245 /* Don't have a valid filename, so shorten the rest of the
22246 * path if we can. This CAN give us invalid 8.3 filenames, but
22247 * there's not a lot of point in guessing what it might be.
22248 */
22249 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022250 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22251 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022252 }
22253
22254 /* Count the paths backward to find the beginning of the desired string. */
22255 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022256 {
22257#ifdef FEAT_MBYTE
22258 if (has_mbyte)
22259 p -= mb_head_off(tfname, p);
22260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022261 if (vim_ispathsep(*p))
22262 {
22263 if (sepcount == 0 || (hasTilde && sepcount == 1))
22264 break;
22265 else
22266 sepcount --;
22267 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269 if (hasTilde)
22270 {
22271 --p;
22272 if (p >= tfname)
22273 *p = '~';
22274 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022275 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 }
22277 else
22278 ++p;
22279
22280 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22281 vim_free(*bufp);
22282 *fnamelen = (int)STRLEN(p);
22283 *bufp = pbuf;
22284 *fnamep = p;
22285
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022286 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287}
22288#endif /* WIN3264 */
22289
22290/*
22291 * Adjust a filename, according to a string of modifiers.
22292 * *fnamep must be NUL terminated when called. When returning, the length is
22293 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022294 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295 * When there is an error, *fnamep is set to NULL.
22296 */
22297 int
22298modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22299 char_u *src; /* string with modifiers */
22300 int *usedlen; /* characters after src that are used */
22301 char_u **fnamep; /* file name so far */
22302 char_u **bufp; /* buffer for allocated file name or NULL */
22303 int *fnamelen; /* length of fnamep */
22304{
22305 int valid = 0;
22306 char_u *tail;
22307 char_u *s, *p, *pbuf;
22308 char_u dirname[MAXPATHL];
22309 int c;
22310 int has_fullname = 0;
22311#ifdef WIN3264
22312 int has_shortname = 0;
22313#endif
22314
22315repeat:
22316 /* ":p" - full path/file_name */
22317 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22318 {
22319 has_fullname = 1;
22320
22321 valid |= VALID_PATH;
22322 *usedlen += 2;
22323
22324 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22325 if ((*fnamep)[0] == '~'
22326#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22327 && ((*fnamep)[1] == '/'
22328# ifdef BACKSLASH_IN_FILENAME
22329 || (*fnamep)[1] == '\\'
22330# endif
22331 || (*fnamep)[1] == NUL)
22332
22333#endif
22334 )
22335 {
22336 *fnamep = expand_env_save(*fnamep);
22337 vim_free(*bufp); /* free any allocated file name */
22338 *bufp = *fnamep;
22339 if (*fnamep == NULL)
22340 return -1;
22341 }
22342
22343 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022344 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345 {
22346 if (vim_ispathsep(*p)
22347 && p[1] == '.'
22348 && (p[2] == NUL
22349 || vim_ispathsep(p[2])
22350 || (p[2] == '.'
22351 && (p[3] == NUL || vim_ispathsep(p[3])))))
22352 break;
22353 }
22354
22355 /* FullName_save() is slow, don't use it when not needed. */
22356 if (*p != NUL || !vim_isAbsName(*fnamep))
22357 {
22358 *fnamep = FullName_save(*fnamep, *p != NUL);
22359 vim_free(*bufp); /* free any allocated file name */
22360 *bufp = *fnamep;
22361 if (*fnamep == NULL)
22362 return -1;
22363 }
22364
22365 /* Append a path separator to a directory. */
22366 if (mch_isdir(*fnamep))
22367 {
22368 /* Make room for one or two extra characters. */
22369 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22370 vim_free(*bufp); /* free any allocated file name */
22371 *bufp = *fnamep;
22372 if (*fnamep == NULL)
22373 return -1;
22374 add_pathsep(*fnamep);
22375 }
22376 }
22377
22378 /* ":." - path relative to the current directory */
22379 /* ":~" - path relative to the home directory */
22380 /* ":8" - shortname path - postponed till after */
22381 while (src[*usedlen] == ':'
22382 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22383 {
22384 *usedlen += 2;
22385 if (c == '8')
22386 {
22387#ifdef WIN3264
22388 has_shortname = 1; /* Postpone this. */
22389#endif
22390 continue;
22391 }
22392 pbuf = NULL;
22393 /* Need full path first (use expand_env() to remove a "~/") */
22394 if (!has_fullname)
22395 {
22396 if (c == '.' && **fnamep == '~')
22397 p = pbuf = expand_env_save(*fnamep);
22398 else
22399 p = pbuf = FullName_save(*fnamep, FALSE);
22400 }
22401 else
22402 p = *fnamep;
22403
22404 has_fullname = 0;
22405
22406 if (p != NULL)
22407 {
22408 if (c == '.')
22409 {
22410 mch_dirname(dirname, MAXPATHL);
22411 s = shorten_fname(p, dirname);
22412 if (s != NULL)
22413 {
22414 *fnamep = s;
22415 if (pbuf != NULL)
22416 {
22417 vim_free(*bufp); /* free any allocated file name */
22418 *bufp = pbuf;
22419 pbuf = NULL;
22420 }
22421 }
22422 }
22423 else
22424 {
22425 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22426 /* Only replace it when it starts with '~' */
22427 if (*dirname == '~')
22428 {
22429 s = vim_strsave(dirname);
22430 if (s != NULL)
22431 {
22432 *fnamep = s;
22433 vim_free(*bufp);
22434 *bufp = s;
22435 }
22436 }
22437 }
22438 vim_free(pbuf);
22439 }
22440 }
22441
22442 tail = gettail(*fnamep);
22443 *fnamelen = (int)STRLEN(*fnamep);
22444
22445 /* ":h" - head, remove "/file_name", can be repeated */
22446 /* Don't remove the first "/" or "c:\" */
22447 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22448 {
22449 valid |= VALID_HEAD;
22450 *usedlen += 2;
22451 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022452 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022453 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454 *fnamelen = (int)(tail - *fnamep);
22455#ifdef VMS
22456 if (*fnamelen > 0)
22457 *fnamelen += 1; /* the path separator is part of the path */
22458#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022459 if (*fnamelen == 0)
22460 {
22461 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22462 p = vim_strsave((char_u *)".");
22463 if (p == NULL)
22464 return -1;
22465 vim_free(*bufp);
22466 *bufp = *fnamep = tail = p;
22467 *fnamelen = 1;
22468 }
22469 else
22470 {
22471 while (tail > s && !after_pathsep(s, tail))
22472 mb_ptr_back(*fnamep, tail);
22473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474 }
22475
22476 /* ":8" - shortname */
22477 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22478 {
22479 *usedlen += 2;
22480#ifdef WIN3264
22481 has_shortname = 1;
22482#endif
22483 }
22484
22485#ifdef WIN3264
22486 /* Check shortname after we have done 'heads' and before we do 'tails'
22487 */
22488 if (has_shortname)
22489 {
22490 pbuf = NULL;
22491 /* Copy the string if it is shortened by :h */
22492 if (*fnamelen < (int)STRLEN(*fnamep))
22493 {
22494 p = vim_strnsave(*fnamep, *fnamelen);
22495 if (p == 0)
22496 return -1;
22497 vim_free(*bufp);
22498 *bufp = *fnamep = p;
22499 }
22500
22501 /* Split into two implementations - makes it easier. First is where
22502 * there isn't a full name already, second is where there is.
22503 */
22504 if (!has_fullname && !vim_isAbsName(*fnamep))
22505 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022506 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507 return -1;
22508 }
22509 else
22510 {
22511 int l;
22512
22513 /* Simple case, already have the full-name
22514 * Nearly always shorter, so try first time. */
22515 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022516 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 return -1;
22518
22519 if (l == 0)
22520 {
22521 /* Couldn't find the filename.. search the paths.
22522 */
22523 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022524 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525 return -1;
22526 }
22527 *fnamelen = l;
22528 }
22529 }
22530#endif /* WIN3264 */
22531
22532 /* ":t" - tail, just the basename */
22533 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22534 {
22535 *usedlen += 2;
22536 *fnamelen -= (int)(tail - *fnamep);
22537 *fnamep = tail;
22538 }
22539
22540 /* ":e" - extension, can be repeated */
22541 /* ":r" - root, without extension, can be repeated */
22542 while (src[*usedlen] == ':'
22543 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22544 {
22545 /* find a '.' in the tail:
22546 * - for second :e: before the current fname
22547 * - otherwise: The last '.'
22548 */
22549 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22550 s = *fnamep - 2;
22551 else
22552 s = *fnamep + *fnamelen - 1;
22553 for ( ; s > tail; --s)
22554 if (s[0] == '.')
22555 break;
22556 if (src[*usedlen + 1] == 'e') /* :e */
22557 {
22558 if (s > tail)
22559 {
22560 *fnamelen += (int)(*fnamep - (s + 1));
22561 *fnamep = s + 1;
22562#ifdef VMS
22563 /* cut version from the extension */
22564 s = *fnamep + *fnamelen - 1;
22565 for ( ; s > *fnamep; --s)
22566 if (s[0] == ';')
22567 break;
22568 if (s > *fnamep)
22569 *fnamelen = s - *fnamep;
22570#endif
22571 }
22572 else if (*fnamep <= tail)
22573 *fnamelen = 0;
22574 }
22575 else /* :r */
22576 {
22577 if (s > tail) /* remove one extension */
22578 *fnamelen = (int)(s - *fnamep);
22579 }
22580 *usedlen += 2;
22581 }
22582
22583 /* ":s?pat?foo?" - substitute */
22584 /* ":gs?pat?foo?" - global substitute */
22585 if (src[*usedlen] == ':'
22586 && (src[*usedlen + 1] == 's'
22587 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
22588 {
22589 char_u *str;
22590 char_u *pat;
22591 char_u *sub;
22592 int sep;
22593 char_u *flags;
22594 int didit = FALSE;
22595
22596 flags = (char_u *)"";
22597 s = src + *usedlen + 2;
22598 if (src[*usedlen + 1] == 'g')
22599 {
22600 flags = (char_u *)"g";
22601 ++s;
22602 }
22603
22604 sep = *s++;
22605 if (sep)
22606 {
22607 /* find end of pattern */
22608 p = vim_strchr(s, sep);
22609 if (p != NULL)
22610 {
22611 pat = vim_strnsave(s, (int)(p - s));
22612 if (pat != NULL)
22613 {
22614 s = p + 1;
22615 /* find end of substitution */
22616 p = vim_strchr(s, sep);
22617 if (p != NULL)
22618 {
22619 sub = vim_strnsave(s, (int)(p - s));
22620 str = vim_strnsave(*fnamep, *fnamelen);
22621 if (sub != NULL && str != NULL)
22622 {
22623 *usedlen = (int)(p + 1 - src);
22624 s = do_string_sub(str, pat, sub, flags);
22625 if (s != NULL)
22626 {
22627 *fnamep = s;
22628 *fnamelen = (int)STRLEN(s);
22629 vim_free(*bufp);
22630 *bufp = s;
22631 didit = TRUE;
22632 }
22633 }
22634 vim_free(sub);
22635 vim_free(str);
22636 }
22637 vim_free(pat);
22638 }
22639 }
22640 /* after using ":s", repeat all the modifiers */
22641 if (didit)
22642 goto repeat;
22643 }
22644 }
22645
22646 return valid;
22647}
22648
22649/*
22650 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22651 * "flags" can be "g" to do a global substitute.
22652 * Returns an allocated string, NULL for error.
22653 */
22654 char_u *
22655do_string_sub(str, pat, sub, flags)
22656 char_u *str;
22657 char_u *pat;
22658 char_u *sub;
22659 char_u *flags;
22660{
22661 int sublen;
22662 regmatch_T regmatch;
22663 int i;
22664 int do_all;
22665 char_u *tail;
22666 garray_T ga;
22667 char_u *ret;
22668 char_u *save_cpo;
22669
22670 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22671 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022672 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022673
22674 ga_init2(&ga, 1, 200);
22675
22676 do_all = (flags[0] == 'g');
22677
22678 regmatch.rm_ic = p_ic;
22679 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
22680 if (regmatch.regprog != NULL)
22681 {
22682 tail = str;
22683 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
22684 {
22685 /*
22686 * Get some space for a temporary buffer to do the substitution
22687 * into. It will contain:
22688 * - The text up to where the match is.
22689 * - The substituted text.
22690 * - The text after the match.
22691 */
22692 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
22693 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
22694 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
22695 {
22696 ga_clear(&ga);
22697 break;
22698 }
22699
22700 /* copy the text up to where the match is */
22701 i = (int)(regmatch.startp[0] - tail);
22702 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
22703 /* add the substituted text */
22704 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
22705 + ga.ga_len + i, TRUE, TRUE, FALSE);
22706 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022707 /* avoid getting stuck on a match with an empty string */
22708 if (tail == regmatch.endp[0])
22709 {
22710 if (*tail == NUL)
22711 break;
22712 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
22713 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714 }
22715 else
22716 {
22717 tail = regmatch.endp[0];
22718 if (*tail == NUL)
22719 break;
22720 }
22721 if (!do_all)
22722 break;
22723 }
22724
22725 if (ga.ga_data != NULL)
22726 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
22727
22728 vim_free(regmatch.regprog);
22729 }
22730
22731 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
22732 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022733 if (p_cpo == empty_option)
22734 p_cpo = save_cpo;
22735 else
22736 /* Darn, evaluating {sub} expression changed the value. */
22737 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738
22739 return ret;
22740}
22741
22742#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */