blob: a48e1529372c4b309bd5f02e718d52e9d7807846 [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 Moolenaarcdb92af2009-06-03 12:26:06 +0000289#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {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 Moolenaar9e70cf12009-05-26 20:59:55 +00005869#if defined(FEAT_PYTHON) || defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005870/*
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
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006529 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006530 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 Moolenaar2c2398c2009-06-03 11:22:45 +00006567 /*
6568 * 2. Free lists and dictionaries that are not referenced.
6569 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006570 did_free = free_unref_items(copyID);
6571
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006572 /*
6573 * 3. Check if any funccal can be freed now.
6574 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006575 for (pfc = &previous_funccal; *pfc != NULL; )
6576 {
6577 if (can_free_funccal(*pfc, copyID))
6578 {
6579 fc = *pfc;
6580 *pfc = fc->caller;
6581 free_funccal(fc, TRUE);
6582 did_free = TRUE;
6583 did_free_funccal = TRUE;
6584 }
6585 else
6586 pfc = &(*pfc)->caller;
6587 }
6588 if (did_free_funccal)
6589 /* When a funccal was freed some more items might be garbage
6590 * collected, so run again. */
6591 (void)garbage_collect();
6592
6593 return did_free;
6594}
6595
6596/*
6597 * Free lists and dictionaries that are no longer referenced.
6598 */
6599 static int
6600free_unref_items(copyID)
6601 int copyID;
6602{
6603 dict_T *dd;
6604 list_T *ll;
6605 int did_free = FALSE;
6606
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006607 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006608 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006609 */
6610 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006611 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006612 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006613 /* Free the Dictionary and ordinary items it contains, but don't
6614 * recurse into Lists and Dictionaries, they will be in the list
6615 * of dicts or list of lists. */
6616 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006617 did_free = TRUE;
6618
6619 /* restart, next dict may also have been freed */
6620 dd = first_dict;
6621 }
6622 else
6623 dd = dd->dv_used_next;
6624
6625 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006626 * Go through the list of lists and free items without the copyID.
6627 * But don't free a list that has a watcher (used in a for loop), these
6628 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006629 */
6630 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006631 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6632 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006633 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006634 /* Free the List and ordinary items it contains, but don't recurse
6635 * into Lists and Dictionaries, they will be in the list of dicts
6636 * or list of lists. */
6637 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006638 did_free = TRUE;
6639
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006640 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006641 ll = first_list;
6642 }
6643 else
6644 ll = ll->lv_used_next;
6645
6646 return did_free;
6647}
6648
6649/*
6650 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6651 */
6652 static void
6653set_ref_in_ht(ht, copyID)
6654 hashtab_T *ht;
6655 int copyID;
6656{
6657 int todo;
6658 hashitem_T *hi;
6659
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006660 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006661 for (hi = ht->ht_array; todo > 0; ++hi)
6662 if (!HASHITEM_EMPTY(hi))
6663 {
6664 --todo;
6665 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6666 }
6667}
6668
6669/*
6670 * Mark all lists and dicts referenced through list "l" with "copyID".
6671 */
6672 static void
6673set_ref_in_list(l, copyID)
6674 list_T *l;
6675 int copyID;
6676{
6677 listitem_T *li;
6678
6679 for (li = l->lv_first; li != NULL; li = li->li_next)
6680 set_ref_in_item(&li->li_tv, copyID);
6681}
6682
6683/*
6684 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6685 */
6686 static void
6687set_ref_in_item(tv, copyID)
6688 typval_T *tv;
6689 int copyID;
6690{
6691 dict_T *dd;
6692 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006693
6694 switch (tv->v_type)
6695 {
6696 case VAR_DICT:
6697 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006698 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006699 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006700 /* Didn't see this dict yet. */
6701 dd->dv_copyID = copyID;
6702 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006703 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006704 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006705
6706 case VAR_LIST:
6707 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006708 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006709 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006710 /* Didn't see this list yet. */
6711 ll->lv_copyID = copyID;
6712 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006713 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006714 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006715 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006716 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006717}
6718
6719/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006720 * Allocate an empty header for a dictionary.
6721 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006722 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006723dict_alloc()
6724{
Bram Moolenaar33570922005-01-25 22:26:29 +00006725 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006726
Bram Moolenaar33570922005-01-25 22:26:29 +00006727 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006728 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006729 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006730 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006731 if (first_dict != NULL)
6732 first_dict->dv_used_prev = d;
6733 d->dv_used_next = first_dict;
6734 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006735 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006736
Bram Moolenaar33570922005-01-25 22:26:29 +00006737 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006738 d->dv_lock = 0;
6739 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006740 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006741 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006742 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006743}
6744
6745/*
6746 * Unreference a Dictionary: decrement the reference count and free it when it
6747 * becomes zero.
6748 */
6749 static void
6750dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006751 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006752{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006753 if (d != NULL && --d->dv_refcount <= 0)
6754 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006755}
6756
6757/*
6758 * Free a Dictionary, including all items it contains.
6759 * Ignores the reference count.
6760 */
6761 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006762dict_free(d, recurse)
6763 dict_T *d;
6764 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006765{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006766 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006767 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006768 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006769
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 /* Remove the dict from the list of dicts for garbage collection. */
6771 if (d->dv_used_prev == NULL)
6772 first_dict = d->dv_used_next;
6773 else
6774 d->dv_used_prev->dv_used_next = d->dv_used_next;
6775 if (d->dv_used_next != NULL)
6776 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6777
6778 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006779 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006780 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006781 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006782 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006783 if (!HASHITEM_EMPTY(hi))
6784 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006785 /* Remove the item before deleting it, just in case there is
6786 * something recursive causing trouble. */
6787 di = HI2DI(hi);
6788 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006789 if (recurse || (di->di_tv.v_type != VAR_LIST
6790 && di->di_tv.v_type != VAR_DICT))
6791 clear_tv(&di->di_tv);
6792 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006793 --todo;
6794 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006795 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006796 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006797 vim_free(d);
6798}
6799
6800/*
6801 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006802 * The "key" is copied to the new item.
6803 * Note that the value of the item "di_tv" still needs to be initialized!
6804 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006805 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006806 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006807dictitem_alloc(key)
6808 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006809{
Bram Moolenaar33570922005-01-25 22:26:29 +00006810 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006811
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006812 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006813 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006814 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006815 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006816 di->di_flags = 0;
6817 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006818 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006819}
6820
6821/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006822 * Make a copy of a Dictionary item.
6823 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006824 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006825dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006826 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006827{
Bram Moolenaar33570922005-01-25 22:26:29 +00006828 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006829
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006830 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6831 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006832 if (di != NULL)
6833 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006834 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006835 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006836 copy_tv(&org->di_tv, &di->di_tv);
6837 }
6838 return di;
6839}
6840
6841/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006842 * Remove item "item" from Dictionary "dict" and free it.
6843 */
6844 static void
6845dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006846 dict_T *dict;
6847 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006848{
Bram Moolenaar33570922005-01-25 22:26:29 +00006849 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006850
Bram Moolenaar33570922005-01-25 22:26:29 +00006851 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006852 if (HASHITEM_EMPTY(hi))
6853 EMSG2(_(e_intern2), "dictitem_remove()");
6854 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006855 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006856 dictitem_free(item);
6857}
6858
6859/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006860 * Free a dict item. Also clears the value.
6861 */
6862 static void
6863dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006864 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006865{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006866 clear_tv(&item->di_tv);
6867 vim_free(item);
6868}
6869
6870/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006871 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6872 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006873 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006874 * Returns NULL when out of memory.
6875 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006876 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006877dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006878 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006879 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006880 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006881{
Bram Moolenaar33570922005-01-25 22:26:29 +00006882 dict_T *copy;
6883 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006884 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006885 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006886
6887 if (orig == NULL)
6888 return NULL;
6889
6890 copy = dict_alloc();
6891 if (copy != NULL)
6892 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006893 if (copyID != 0)
6894 {
6895 orig->dv_copyID = copyID;
6896 orig->dv_copydict = copy;
6897 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006898 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006899 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006900 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006901 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006902 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006903 --todo;
6904
6905 di = dictitem_alloc(hi->hi_key);
6906 if (di == NULL)
6907 break;
6908 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006909 {
6910 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6911 copyID) == FAIL)
6912 {
6913 vim_free(di);
6914 break;
6915 }
6916 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006917 else
6918 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6919 if (dict_add(copy, di) == FAIL)
6920 {
6921 dictitem_free(di);
6922 break;
6923 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006924 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006925 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006926
Bram Moolenaare9a41262005-01-15 22:18:47 +00006927 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006928 if (todo > 0)
6929 {
6930 dict_unref(copy);
6931 copy = NULL;
6932 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006933 }
6934
6935 return copy;
6936}
6937
6938/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006939 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006940 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006941 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006942 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006943dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006944 dict_T *d;
6945 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006946{
Bram Moolenaar33570922005-01-25 22:26:29 +00006947 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006948}
6949
Bram Moolenaar8c711452005-01-14 21:53:12 +00006950/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006951 * Add a number or string entry to dictionary "d".
6952 * When "str" is NULL use number "nr", otherwise use "str".
6953 * Returns FAIL when out of memory and when key already exists.
6954 */
6955 int
6956dict_add_nr_str(d, key, nr, str)
6957 dict_T *d;
6958 char *key;
6959 long nr;
6960 char_u *str;
6961{
6962 dictitem_T *item;
6963
6964 item = dictitem_alloc((char_u *)key);
6965 if (item == NULL)
6966 return FAIL;
6967 item->di_tv.v_lock = 0;
6968 if (str == NULL)
6969 {
6970 item->di_tv.v_type = VAR_NUMBER;
6971 item->di_tv.vval.v_number = nr;
6972 }
6973 else
6974 {
6975 item->di_tv.v_type = VAR_STRING;
6976 item->di_tv.vval.v_string = vim_strsave(str);
6977 }
6978 if (dict_add(d, item) == FAIL)
6979 {
6980 dictitem_free(item);
6981 return FAIL;
6982 }
6983 return OK;
6984}
6985
6986/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006987 * Get the number of items in a Dictionary.
6988 */
6989 static long
6990dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006991 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006992{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006993 if (d == NULL)
6994 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006995 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006996}
6997
6998/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006999 * Find item "key[len]" in Dictionary "d".
7000 * If "len" is negative use strlen(key).
7001 * Returns NULL when not found.
7002 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007003 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007004dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007005 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007006 char_u *key;
7007 int len;
7008{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007009#define AKEYLEN 200
7010 char_u buf[AKEYLEN];
7011 char_u *akey;
7012 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007013 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007014
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007015 if (len < 0)
7016 akey = key;
7017 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007018 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007019 tofree = akey = vim_strnsave(key, len);
7020 if (akey == NULL)
7021 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007022 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007023 else
7024 {
7025 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007026 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007027 akey = buf;
7028 }
7029
Bram Moolenaar33570922005-01-25 22:26:29 +00007030 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007031 vim_free(tofree);
7032 if (HASHITEM_EMPTY(hi))
7033 return NULL;
7034 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007035}
7036
7037/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007038 * Get a string item from a dictionary.
7039 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007040 * Returns NULL if the entry doesn't exist or out of memory.
7041 */
7042 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007043get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007044 dict_T *d;
7045 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007046 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007047{
7048 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007049 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007050
7051 di = dict_find(d, key, -1);
7052 if (di == NULL)
7053 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007054 s = get_tv_string(&di->di_tv);
7055 if (save && s != NULL)
7056 s = vim_strsave(s);
7057 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007058}
7059
7060/*
7061 * Get a number item from a dictionary.
7062 * Returns 0 if the entry doesn't exist or out of memory.
7063 */
7064 long
7065get_dict_number(d, key)
7066 dict_T *d;
7067 char_u *key;
7068{
7069 dictitem_T *di;
7070
7071 di = dict_find(d, key, -1);
7072 if (di == NULL)
7073 return 0;
7074 return get_tv_number(&di->di_tv);
7075}
7076
7077/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007078 * Return an allocated string with the string representation of a Dictionary.
7079 * May return NULL.
7080 */
7081 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007082dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007083 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007084 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007085{
7086 garray_T ga;
7087 int first = TRUE;
7088 char_u *tofree;
7089 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007090 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007091 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007092 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007093 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007094
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007095 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007096 return NULL;
7097 ga_init2(&ga, (int)sizeof(char), 80);
7098 ga_append(&ga, '{');
7099
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007100 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007101 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007102 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007103 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007105 --todo;
7106
7107 if (first)
7108 first = FALSE;
7109 else
7110 ga_concat(&ga, (char_u *)", ");
7111
7112 tofree = string_quote(hi->hi_key, FALSE);
7113 if (tofree != NULL)
7114 {
7115 ga_concat(&ga, tofree);
7116 vim_free(tofree);
7117 }
7118 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007119 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120 if (s != NULL)
7121 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007122 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007123 if (s == NULL)
7124 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007125 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007126 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007127 if (todo > 0)
7128 {
7129 vim_free(ga.ga_data);
7130 return NULL;
7131 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007132
7133 ga_append(&ga, '}');
7134 ga_append(&ga, NUL);
7135 return (char_u *)ga.ga_data;
7136}
7137
7138/*
7139 * Allocate a variable for a Dictionary and fill it from "*arg".
7140 * Return OK or FAIL. Returns NOTDONE for {expr}.
7141 */
7142 static int
7143get_dict_tv(arg, rettv, evaluate)
7144 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007146 int evaluate;
7147{
Bram Moolenaar33570922005-01-25 22:26:29 +00007148 dict_T *d = NULL;
7149 typval_T tvkey;
7150 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007151 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007152 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007153 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007154 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007155
7156 /*
7157 * First check if it's not a curly-braces thing: {expr}.
7158 * Must do this without evaluating, otherwise a function may be called
7159 * twice. Unfortunately this means we need to call eval1() twice for the
7160 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007161 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007162 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007163 if (*start != '}')
7164 {
7165 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7166 return FAIL;
7167 if (*start == '}')
7168 return NOTDONE;
7169 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007170
7171 if (evaluate)
7172 {
7173 d = dict_alloc();
7174 if (d == NULL)
7175 return FAIL;
7176 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007177 tvkey.v_type = VAR_UNKNOWN;
7178 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007179
7180 *arg = skipwhite(*arg + 1);
7181 while (**arg != '}' && **arg != NUL)
7182 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007183 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007184 goto failret;
7185 if (**arg != ':')
7186 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007187 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007188 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007189 goto failret;
7190 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007191 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007192 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007193 key = get_tv_string_buf_chk(&tvkey, buf);
7194 if (key == NULL || *key == NUL)
7195 {
7196 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7197 if (key != NULL)
7198 EMSG(_(e_emptykey));
7199 clear_tv(&tvkey);
7200 goto failret;
7201 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007202 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007203
7204 *arg = skipwhite(*arg + 1);
7205 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7206 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007207 if (evaluate)
7208 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007209 goto failret;
7210 }
7211 if (evaluate)
7212 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007213 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214 if (item != NULL)
7215 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007216 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007217 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218 clear_tv(&tv);
7219 goto failret;
7220 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007221 item = dictitem_alloc(key);
7222 clear_tv(&tvkey);
7223 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007226 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007227 if (dict_add(d, item) == FAIL)
7228 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229 }
7230 }
7231
7232 if (**arg == '}')
7233 break;
7234 if (**arg != ',')
7235 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007236 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007237 goto failret;
7238 }
7239 *arg = skipwhite(*arg + 1);
7240 }
7241
7242 if (**arg != '}')
7243 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007244 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245failret:
7246 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007247 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248 return FAIL;
7249 }
7250
7251 *arg = skipwhite(*arg + 1);
7252 if (evaluate)
7253 {
7254 rettv->v_type = VAR_DICT;
7255 rettv->vval.v_dict = d;
7256 ++d->dv_refcount;
7257 }
7258
7259 return OK;
7260}
7261
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007263 * Return a string with the string representation of a variable.
7264 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007265 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007266 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007267 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007268 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007269 */
7270 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007271echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007272 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007273 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007274 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007275 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007276{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007277 static int recurse = 0;
7278 char_u *r = NULL;
7279
Bram Moolenaar33570922005-01-25 22:26:29 +00007280 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007281 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007282 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007283 *tofree = NULL;
7284 return NULL;
7285 }
7286 ++recurse;
7287
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007288 switch (tv->v_type)
7289 {
7290 case VAR_FUNC:
7291 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007292 r = tv->vval.v_string;
7293 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007294
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007295 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007296 if (tv->vval.v_list == NULL)
7297 {
7298 *tofree = NULL;
7299 r = NULL;
7300 }
7301 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7302 {
7303 *tofree = NULL;
7304 r = (char_u *)"[...]";
7305 }
7306 else
7307 {
7308 tv->vval.v_list->lv_copyID = copyID;
7309 *tofree = list2string(tv, copyID);
7310 r = *tofree;
7311 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007312 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007313
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007315 if (tv->vval.v_dict == NULL)
7316 {
7317 *tofree = NULL;
7318 r = NULL;
7319 }
7320 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7321 {
7322 *tofree = NULL;
7323 r = (char_u *)"{...}";
7324 }
7325 else
7326 {
7327 tv->vval.v_dict->dv_copyID = copyID;
7328 *tofree = dict2string(tv, copyID);
7329 r = *tofree;
7330 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007331 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007332
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007333 case VAR_STRING:
7334 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007335 *tofree = NULL;
7336 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007337 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007338
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007339#ifdef FEAT_FLOAT
7340 case VAR_FLOAT:
7341 *tofree = NULL;
7342 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7343 r = numbuf;
7344 break;
7345#endif
7346
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007347 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007348 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007349 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007350 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007351
7352 --recurse;
7353 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007354}
7355
7356/*
7357 * Return a string with the string representation of a variable.
7358 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7359 * "numbuf" is used for a number.
7360 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007361 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007362 */
7363 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007364tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007366 char_u **tofree;
7367 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007368 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007369{
7370 switch (tv->v_type)
7371 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007372 case VAR_FUNC:
7373 *tofree = string_quote(tv->vval.v_string, TRUE);
7374 return *tofree;
7375 case VAR_STRING:
7376 *tofree = string_quote(tv->vval.v_string, FALSE);
7377 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007378#ifdef FEAT_FLOAT
7379 case VAR_FLOAT:
7380 *tofree = NULL;
7381 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7382 return numbuf;
7383#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007384 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007385 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007386 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007387 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007388 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007389 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007390 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007391 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007392}
7393
7394/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 * Return string "str" in ' quotes, doubling ' characters.
7396 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007397 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007398 */
7399 static char_u *
7400string_quote(str, function)
7401 char_u *str;
7402 int function;
7403{
Bram Moolenaar33570922005-01-25 22:26:29 +00007404 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007405 char_u *p, *r, *s;
7406
Bram Moolenaar33570922005-01-25 22:26:29 +00007407 len = (function ? 13 : 3);
7408 if (str != NULL)
7409 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007410 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007411 for (p = str; *p != NUL; mb_ptr_adv(p))
7412 if (*p == '\'')
7413 ++len;
7414 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007415 s = r = alloc(len);
7416 if (r != NULL)
7417 {
7418 if (function)
7419 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007421 r += 10;
7422 }
7423 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007425 if (str != NULL)
7426 for (p = str; *p != NUL; )
7427 {
7428 if (*p == '\'')
7429 *r++ = '\'';
7430 MB_COPY_CHAR(p, r);
7431 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007433 if (function)
7434 *r++ = ')';
7435 *r++ = NUL;
7436 }
7437 return s;
7438}
7439
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007440#ifdef FEAT_FLOAT
7441/*
7442 * Convert the string "text" to a floating point number.
7443 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7444 * this always uses a decimal point.
7445 * Returns the length of the text that was consumed.
7446 */
7447 static int
7448string2float(text, value)
7449 char_u *text;
7450 float_T *value; /* result stored here */
7451{
7452 char *s = (char *)text;
7453 float_T f;
7454
7455 f = strtod(s, &s);
7456 *value = f;
7457 return (int)((char_u *)s - text);
7458}
7459#endif
7460
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007461/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 * Get the value of an environment variable.
7463 * "arg" is pointing to the '$'. It is advanced to after the name.
7464 * If the environment variable was not set, silently assume it is empty.
7465 * Always return OK.
7466 */
7467 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007468get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 int evaluate;
7472{
7473 char_u *string = NULL;
7474 int len;
7475 int cc;
7476 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007477 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478
7479 ++*arg;
7480 name = *arg;
7481 len = get_env_len(arg);
7482 if (evaluate)
7483 {
7484 if (len != 0)
7485 {
7486 cc = name[len];
7487 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007488 /* first try vim_getenv(), fast for normal environment vars */
7489 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007491 {
7492 if (!mustfree)
7493 string = vim_strsave(string);
7494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 else
7496 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007497 if (mustfree)
7498 vim_free(string);
7499
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 /* next try expanding things like $VIM and ${HOME} */
7501 string = expand_env_save(name - 1);
7502 if (string != NULL && *string == '$')
7503 {
7504 vim_free(string);
7505 string = NULL;
7506 }
7507 }
7508 name[len] = cc;
7509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007510 rettv->v_type = VAR_STRING;
7511 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 }
7513
7514 return OK;
7515}
7516
7517/*
7518 * Array with names and number of arguments of all internal functions
7519 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7520 */
7521static struct fst
7522{
7523 char *f_name; /* function name */
7524 char f_min_argc; /* minimal number of arguments */
7525 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007526 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007527 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528} functions[] =
7529{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007530#ifdef FEAT_FLOAT
7531 {"abs", 1, 1, f_abs},
7532#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007533 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 {"append", 2, 2, f_append},
7535 {"argc", 0, 0, f_argc},
7536 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007537 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007538#ifdef FEAT_FLOAT
7539 {"atan", 1, 1, f_atan},
7540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007542 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 {"bufexists", 1, 1, f_bufexists},
7544 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7545 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7546 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7547 {"buflisted", 1, 1, f_buflisted},
7548 {"bufloaded", 1, 1, f_bufloaded},
7549 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007550 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 {"bufwinnr", 1, 1, f_bufwinnr},
7552 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007553 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007554 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007555#ifdef FEAT_FLOAT
7556 {"ceil", 1, 1, f_ceil},
7557#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007558 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 {"char2nr", 1, 1, f_char2nr},
7560 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007561 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007563#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007564 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007565 {"complete_add", 1, 1, f_complete_add},
7566 {"complete_check", 0, 0, f_complete_check},
7567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007569 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007570#ifdef FEAT_FLOAT
7571 {"cos", 1, 1, f_cos},
7572#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007573 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007575 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007576 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 {"delete", 1, 1, f_delete},
7578 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007579 {"diff_filler", 1, 1, f_diff_filler},
7580 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007581 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007583 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 {"eventhandler", 0, 0, f_eventhandler},
7585 {"executable", 1, 1, f_executable},
7586 {"exists", 1, 1, f_exists},
7587 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007588 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007589 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7591 {"filereadable", 1, 1, f_filereadable},
7592 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007593 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007594 {"finddir", 1, 3, f_finddir},
7595 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007596#ifdef FEAT_FLOAT
7597 {"float2nr", 1, 1, f_float2nr},
7598 {"floor", 1, 1, f_floor},
7599#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007600 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 {"fnamemodify", 2, 2, f_fnamemodify},
7602 {"foldclosed", 1, 1, f_foldclosed},
7603 {"foldclosedend", 1, 1, f_foldclosedend},
7604 {"foldlevel", 1, 1, f_foldlevel},
7605 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007606 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007608 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007609 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007610 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007611 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 {"getbufvar", 2, 2, f_getbufvar},
7613 {"getchar", 0, 1, f_getchar},
7614 {"getcharmod", 0, 0, f_getcharmod},
7615 {"getcmdline", 0, 0, f_getcmdline},
7616 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007617 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007619 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007620 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 {"getfsize", 1, 1, f_getfsize},
7622 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007623 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007624 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007625 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007626 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007627 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007628 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007629 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007630 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007632 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 {"getwinposx", 0, 0, f_getwinposx},
7634 {"getwinposy", 0, 0, f_getwinposy},
7635 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007636 {"glob", 1, 2, f_glob},
7637 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007640 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007641 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7643 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7644 {"histadd", 2, 2, f_histadd},
7645 {"histdel", 1, 2, f_histdel},
7646 {"histget", 1, 2, f_histget},
7647 {"histnr", 1, 1, f_histnr},
7648 {"hlID", 1, 1, f_hlID},
7649 {"hlexists", 1, 1, f_hlexists},
7650 {"hostname", 0, 0, f_hostname},
7651 {"iconv", 3, 3, f_iconv},
7652 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007653 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007654 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007656 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 {"inputrestore", 0, 0, f_inputrestore},
7658 {"inputsave", 0, 0, f_inputsave},
7659 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007660 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007662 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007664 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007667 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 {"libcall", 3, 3, f_libcall},
7669 {"libcallnr", 3, 3, f_libcallnr},
7670 {"line", 1, 1, f_line},
7671 {"line2byte", 1, 1, f_line2byte},
7672 {"lispindent", 1, 1, f_lispindent},
7673 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007674#ifdef FEAT_FLOAT
7675 {"log10", 1, 1, f_log10},
7676#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007677 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007678 {"maparg", 1, 3, f_maparg},
7679 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007680 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007681 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007682 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007683 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007684 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007685 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007686 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007687 {"max", 1, 1, f_max},
7688 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007689#ifdef vim_mkdir
7690 {"mkdir", 1, 3, f_mkdir},
7691#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007692 {"mode", 0, 1, f_mode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 {"nextnonblank", 1, 1, f_nextnonblank},
7694 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007695 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007696#ifdef FEAT_FLOAT
7697 {"pow", 2, 2, f_pow},
7698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007700 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007701 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007703 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007704 {"reltime", 0, 2, f_reltime},
7705 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 {"remote_expr", 2, 3, f_remote_expr},
7707 {"remote_foreground", 1, 1, f_remote_foreground},
7708 {"remote_peek", 1, 2, f_remote_peek},
7709 {"remote_read", 1, 1, f_remote_read},
7710 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007711 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007713 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007715 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007716#ifdef FEAT_FLOAT
7717 {"round", 1, 1, f_round},
7718#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007719 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007720 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007721 {"searchpair", 3, 7, f_searchpair},
7722 {"searchpairpos", 3, 7, f_searchpairpos},
7723 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 {"server2client", 2, 2, f_server2client},
7725 {"serverlist", 0, 0, f_serverlist},
7726 {"setbufvar", 3, 3, f_setbufvar},
7727 {"setcmdpos", 1, 1, f_setcmdpos},
7728 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007729 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007730 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007731 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007732 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007734 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007736 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007738#ifdef FEAT_FLOAT
7739 {"sin", 1, 1, f_sin},
7740#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007741 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007742 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007743 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007744 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007745 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007746#ifdef FEAT_FLOAT
7747 {"sqrt", 1, 1, f_sqrt},
7748 {"str2float", 1, 1, f_str2float},
7749#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007750 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751#ifdef HAVE_STRFTIME
7752 {"strftime", 1, 2, f_strftime},
7753#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007754 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007755 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 {"strlen", 1, 1, f_strlen},
7757 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007758 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 {"strtrans", 1, 1, f_strtrans},
7760 {"submatch", 1, 1, f_submatch},
7761 {"substitute", 4, 4, f_substitute},
7762 {"synID", 3, 3, f_synID},
7763 {"synIDattr", 2, 3, f_synIDattr},
7764 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007765 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007766 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007767 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007768 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007769 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007770 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007771 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007773 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 {"tolower", 1, 1, f_tolower},
7775 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007776 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007777#ifdef FEAT_FLOAT
7778 {"trunc", 1, 1, f_trunc},
7779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007781 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 {"virtcol", 1, 1, f_virtcol},
7783 {"visualmode", 0, 1, f_visualmode},
7784 {"winbufnr", 1, 1, f_winbufnr},
7785 {"wincol", 0, 0, f_wincol},
7786 {"winheight", 1, 1, f_winheight},
7787 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007788 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007790 {"winrestview", 1, 1, f_winrestview},
7791 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007793 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794};
7795
7796#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7797
7798/*
7799 * Function given to ExpandGeneric() to obtain the list of internal
7800 * or user defined function names.
7801 */
7802 char_u *
7803get_function_name(xp, idx)
7804 expand_T *xp;
7805 int idx;
7806{
7807 static int intidx = -1;
7808 char_u *name;
7809
7810 if (idx == 0)
7811 intidx = -1;
7812 if (intidx < 0)
7813 {
7814 name = get_user_func_name(xp, idx);
7815 if (name != NULL)
7816 return name;
7817 }
7818 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7819 {
7820 STRCPY(IObuff, functions[intidx].f_name);
7821 STRCAT(IObuff, "(");
7822 if (functions[intidx].f_max_argc == 0)
7823 STRCAT(IObuff, ")");
7824 return IObuff;
7825 }
7826
7827 return NULL;
7828}
7829
7830/*
7831 * Function given to ExpandGeneric() to obtain the list of internal or
7832 * user defined variable or function names.
7833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 char_u *
7835get_expr_name(xp, idx)
7836 expand_T *xp;
7837 int idx;
7838{
7839 static int intidx = -1;
7840 char_u *name;
7841
7842 if (idx == 0)
7843 intidx = -1;
7844 if (intidx < 0)
7845 {
7846 name = get_function_name(xp, idx);
7847 if (name != NULL)
7848 return name;
7849 }
7850 return get_user_var_name(xp, ++intidx);
7851}
7852
7853#endif /* FEAT_CMDL_COMPL */
7854
7855/*
7856 * Find internal function in table above.
7857 * Return index, or -1 if not found
7858 */
7859 static int
7860find_internal_func(name)
7861 char_u *name; /* name of the function */
7862{
7863 int first = 0;
7864 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7865 int cmp;
7866 int x;
7867
7868 /*
7869 * Find the function name in the table. Binary search.
7870 */
7871 while (first <= last)
7872 {
7873 x = first + ((unsigned)(last - first) >> 1);
7874 cmp = STRCMP(name, functions[x].f_name);
7875 if (cmp < 0)
7876 last = x - 1;
7877 else if (cmp > 0)
7878 first = x + 1;
7879 else
7880 return x;
7881 }
7882 return -1;
7883}
7884
7885/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007886 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7887 * name it contains, otherwise return "name".
7888 */
7889 static char_u *
7890deref_func_name(name, lenp)
7891 char_u *name;
7892 int *lenp;
7893{
Bram Moolenaar33570922005-01-25 22:26:29 +00007894 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007895 int cc;
7896
7897 cc = name[*lenp];
7898 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007899 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007900 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007901 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007902 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007903 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007904 {
7905 *lenp = 0;
7906 return (char_u *)""; /* just in case */
7907 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007908 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007909 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007910 }
7911
7912 return name;
7913}
7914
7915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 * Allocate a variable for the result of a function.
7917 * Return OK or FAIL.
7918 */
7919 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007920get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7921 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 char_u *name; /* name of the function */
7923 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 char_u **arg; /* argument, pointing to the '(' */
7926 linenr_T firstline; /* first line of range */
7927 linenr_T lastline; /* last line of range */
7928 int *doesrange; /* return: function handled range */
7929 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007930 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931{
7932 char_u *argp;
7933 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007934 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 int argcount = 0; /* number of arguments found */
7936
7937 /*
7938 * Get the arguments.
7939 */
7940 argp = *arg;
7941 while (argcount < MAX_FUNC_ARGS)
7942 {
7943 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7944 if (*argp == ')' || *argp == ',' || *argp == NUL)
7945 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7947 {
7948 ret = FAIL;
7949 break;
7950 }
7951 ++argcount;
7952 if (*argp != ',')
7953 break;
7954 }
7955 if (*argp == ')')
7956 ++argp;
7957 else
7958 ret = FAIL;
7959
7960 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007961 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007962 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007964 {
7965 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00007966 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007967 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00007968 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970
7971 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007972 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973
7974 *arg = skipwhite(argp);
7975 return ret;
7976}
7977
7978
7979/*
7980 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007981 * Return OK when the function can't be called, FAIL otherwise.
7982 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 */
7984 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007985call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007986 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 char_u *name; /* name of the function */
7988 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007989 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007991 typval_T *argvars; /* vars for arguments, must have "argcount"
7992 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 linenr_T firstline; /* first line of range */
7994 linenr_T lastline; /* last line of range */
7995 int *doesrange; /* return: function handled range */
7996 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007997 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998{
7999 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000#define ERROR_UNKNOWN 0
8001#define ERROR_TOOMANY 1
8002#define ERROR_TOOFEW 2
8003#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008004#define ERROR_DICT 4
8005#define ERROR_NONE 5
8006#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 int error = ERROR_NONE;
8008 int i;
8009 int llen;
8010 ufunc_T *fp;
8011 int cc;
8012#define FLEN_FIXED 40
8013 char_u fname_buf[FLEN_FIXED + 1];
8014 char_u *fname;
8015
8016 /*
8017 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8018 * Change <SNR>123_name() to K_SNR 123_name().
8019 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8020 */
8021 cc = name[len];
8022 name[len] = NUL;
8023 llen = eval_fname_script(name);
8024 if (llen > 0)
8025 {
8026 fname_buf[0] = K_SPECIAL;
8027 fname_buf[1] = KS_EXTRA;
8028 fname_buf[2] = (int)KE_SNR;
8029 i = 3;
8030 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8031 {
8032 if (current_SID <= 0)
8033 error = ERROR_SCRIPT;
8034 else
8035 {
8036 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8037 i = (int)STRLEN(fname_buf);
8038 }
8039 }
8040 if (i + STRLEN(name + llen) < FLEN_FIXED)
8041 {
8042 STRCPY(fname_buf + i, name + llen);
8043 fname = fname_buf;
8044 }
8045 else
8046 {
8047 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8048 if (fname == NULL)
8049 error = ERROR_OTHER;
8050 else
8051 {
8052 mch_memmove(fname, fname_buf, (size_t)i);
8053 STRCPY(fname + i, name + llen);
8054 }
8055 }
8056 }
8057 else
8058 fname = name;
8059
8060 *doesrange = FALSE;
8061
8062
8063 /* execute the function if no errors detected and executing */
8064 if (evaluate && error == ERROR_NONE)
8065 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008066 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8067 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 error = ERROR_UNKNOWN;
8069
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008070 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {
8072 /*
8073 * User defined function.
8074 */
8075 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008076
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008078 /* Trigger FuncUndefined event, may load the function. */
8079 if (fp == NULL
8080 && apply_autocmds(EVENT_FUNCUNDEFINED,
8081 fname, fname, TRUE, NULL)
8082 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008084 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 fp = find_func(fname);
8086 }
8087#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008088 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008089 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008090 {
8091 /* loaded a package, search for the function again */
8092 fp = find_func(fname);
8093 }
8094
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 if (fp != NULL)
8096 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008097 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008099 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008101 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008103 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008104 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 else
8106 {
8107 /*
8108 * Call the user function.
8109 * Save and restore search patterns, script variables and
8110 * redo buffer.
8111 */
8112 save_search_patterns();
8113 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008114 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008115 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008116 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008117 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8118 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8119 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008120 /* Function was unreferenced while being used, free it
8121 * now. */
8122 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 restoreRedobuff();
8124 restore_search_patterns();
8125 error = ERROR_NONE;
8126 }
8127 }
8128 }
8129 else
8130 {
8131 /*
8132 * Find the function name in the table, call its implementation.
8133 */
8134 i = find_internal_func(fname);
8135 if (i >= 0)
8136 {
8137 if (argcount < functions[i].f_min_argc)
8138 error = ERROR_TOOFEW;
8139 else if (argcount > functions[i].f_max_argc)
8140 error = ERROR_TOOMANY;
8141 else
8142 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008143 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008144 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 error = ERROR_NONE;
8146 }
8147 }
8148 }
8149 /*
8150 * The function call (or "FuncUndefined" autocommand sequence) might
8151 * have been aborted by an error, an interrupt, or an explicitly thrown
8152 * exception that has not been caught so far. This situation can be
8153 * tested for by calling aborting(). For an error in an internal
8154 * function or for the "E132" error in call_user_func(), however, the
8155 * throw point at which the "force_abort" flag (temporarily reset by
8156 * emsg()) is normally updated has not been reached yet. We need to
8157 * update that flag first to make aborting() reliable.
8158 */
8159 update_force_abort();
8160 }
8161 if (error == ERROR_NONE)
8162 ret = OK;
8163
8164 /*
8165 * Report an error unless the argument evaluation or function call has been
8166 * cancelled due to an aborting error, an interrupt, or an exception.
8167 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008168 if (!aborting())
8169 {
8170 switch (error)
8171 {
8172 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008173 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008174 break;
8175 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008176 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008177 break;
8178 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008179 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008180 name);
8181 break;
8182 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008183 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008184 name);
8185 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008186 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008187 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008188 name);
8189 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008190 }
8191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192
8193 name[len] = cc;
8194 if (fname != name && fname != fname_buf)
8195 vim_free(fname);
8196
8197 return ret;
8198}
8199
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008200/*
8201 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008202 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008203 */
8204 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008205emsg_funcname(ermsg, name)
8206 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008207 char_u *name;
8208{
8209 char_u *p;
8210
8211 if (*name == K_SPECIAL)
8212 p = concat_str((char_u *)"<SNR>", name + 3);
8213 else
8214 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008215 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008216 if (p != name)
8217 vim_free(p);
8218}
8219
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008220/*
8221 * Return TRUE for a non-zero Number and a non-empty String.
8222 */
8223 static int
8224non_zero_arg(argvars)
8225 typval_T *argvars;
8226{
8227 return ((argvars[0].v_type == VAR_NUMBER
8228 && argvars[0].vval.v_number != 0)
8229 || (argvars[0].v_type == VAR_STRING
8230 && argvars[0].vval.v_string != NULL
8231 && *argvars[0].vval.v_string != NUL));
8232}
8233
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234/*********************************************
8235 * Implementation of the built-in functions
8236 */
8237
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008238#ifdef FEAT_FLOAT
8239/*
8240 * "abs(expr)" function
8241 */
8242 static void
8243f_abs(argvars, rettv)
8244 typval_T *argvars;
8245 typval_T *rettv;
8246{
8247 if (argvars[0].v_type == VAR_FLOAT)
8248 {
8249 rettv->v_type = VAR_FLOAT;
8250 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8251 }
8252 else
8253 {
8254 varnumber_T n;
8255 int error = FALSE;
8256
8257 n = get_tv_number_chk(&argvars[0], &error);
8258 if (error)
8259 rettv->vval.v_number = -1;
8260 else if (n > 0)
8261 rettv->vval.v_number = n;
8262 else
8263 rettv->vval.v_number = -n;
8264 }
8265}
8266#endif
8267
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008269 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 */
8271 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008272f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008273 typval_T *argvars;
8274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275{
Bram Moolenaar33570922005-01-25 22:26:29 +00008276 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008278 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008279 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008281 if ((l = argvars[0].vval.v_list) != NULL
8282 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8283 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008284 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008285 }
8286 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008287 EMSG(_(e_listreq));
8288}
8289
8290/*
8291 * "append(lnum, string/list)" function
8292 */
8293 static void
8294f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008295 typval_T *argvars;
8296 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008297{
8298 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008299 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008300 list_T *l = NULL;
8301 listitem_T *li = NULL;
8302 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008303 long added = 0;
8304
Bram Moolenaar0d660222005-01-07 21:51:51 +00008305 lnum = get_tv_lnum(argvars);
8306 if (lnum >= 0
8307 && lnum <= curbuf->b_ml.ml_line_count
8308 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008309 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008310 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008311 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008312 l = argvars[1].vval.v_list;
8313 if (l == NULL)
8314 return;
8315 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008316 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008317 for (;;)
8318 {
8319 if (l == NULL)
8320 tv = &argvars[1]; /* append a string */
8321 else if (li == NULL)
8322 break; /* end of list */
8323 else
8324 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008325 line = get_tv_string_chk(tv);
8326 if (line == NULL) /* type error */
8327 {
8328 rettv->vval.v_number = 1; /* Failed */
8329 break;
8330 }
8331 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008332 ++added;
8333 if (l == NULL)
8334 break;
8335 li = li->li_next;
8336 }
8337
8338 appended_lines_mark(lnum, added);
8339 if (curwin->w_cursor.lnum > lnum)
8340 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008342 else
8343 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344}
8345
8346/*
8347 * "argc()" function
8348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008350f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008351 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008354 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355}
8356
8357/*
8358 * "argidx()" function
8359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008361f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008362 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008365 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366}
8367
8368/*
8369 * "argv(nr)" function
8370 */
8371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008372f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008373 typval_T *argvars;
8374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375{
8376 int idx;
8377
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008378 if (argvars[0].v_type != VAR_UNKNOWN)
8379 {
8380 idx = get_tv_number_chk(&argvars[0], NULL);
8381 if (idx >= 0 && idx < ARGCOUNT)
8382 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8383 else
8384 rettv->vval.v_string = NULL;
8385 rettv->v_type = VAR_STRING;
8386 }
8387 else if (rettv_list_alloc(rettv) == OK)
8388 for (idx = 0; idx < ARGCOUNT; ++idx)
8389 list_append_string(rettv->vval.v_list,
8390 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391}
8392
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008393#ifdef FEAT_FLOAT
8394static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8395
8396/*
8397 * Get the float value of "argvars[0]" into "f".
8398 * Returns FAIL when the argument is not a Number or Float.
8399 */
8400 static int
8401get_float_arg(argvars, f)
8402 typval_T *argvars;
8403 float_T *f;
8404{
8405 if (argvars[0].v_type == VAR_FLOAT)
8406 {
8407 *f = argvars[0].vval.v_float;
8408 return OK;
8409 }
8410 if (argvars[0].v_type == VAR_NUMBER)
8411 {
8412 *f = (float_T)argvars[0].vval.v_number;
8413 return OK;
8414 }
8415 EMSG(_("E808: Number or Float required"));
8416 return FAIL;
8417}
8418
8419/*
8420 * "atan()" function
8421 */
8422 static void
8423f_atan(argvars, rettv)
8424 typval_T *argvars;
8425 typval_T *rettv;
8426{
8427 float_T f;
8428
8429 rettv->v_type = VAR_FLOAT;
8430 if (get_float_arg(argvars, &f) == OK)
8431 rettv->vval.v_float = atan(f);
8432 else
8433 rettv->vval.v_float = 0.0;
8434}
8435#endif
8436
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437/*
8438 * "browse(save, title, initdir, default)" function
8439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008441f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008442 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444{
8445#ifdef FEAT_BROWSE
8446 int save;
8447 char_u *title;
8448 char_u *initdir;
8449 char_u *defname;
8450 char_u buf[NUMBUFLEN];
8451 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008452 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008454 save = get_tv_number_chk(&argvars[0], &error);
8455 title = get_tv_string_chk(&argvars[1]);
8456 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8457 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008459 if (error || title == NULL || initdir == NULL || defname == NULL)
8460 rettv->vval.v_string = NULL;
8461 else
8462 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008463 do_browse(save ? BROWSE_SAVE : 0,
8464 title, defname, NULL, initdir, NULL, curbuf);
8465#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008466 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008467#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008468 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008469}
8470
8471/*
8472 * "browsedir(title, initdir)" function
8473 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008475f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008476 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008477 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008478{
8479#ifdef FEAT_BROWSE
8480 char_u *title;
8481 char_u *initdir;
8482 char_u buf[NUMBUFLEN];
8483
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008484 title = get_tv_string_chk(&argvars[0]);
8485 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008486
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008487 if (title == NULL || initdir == NULL)
8488 rettv->vval.v_string = NULL;
8489 else
8490 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008491 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008493 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008495 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496}
8497
Bram Moolenaar33570922005-01-25 22:26:29 +00008498static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008499
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500/*
8501 * Find a buffer by number or exact name.
8502 */
8503 static buf_T *
8504find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008505 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506{
8507 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008509 if (avar->v_type == VAR_NUMBER)
8510 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008511 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008513 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008514 if (buf == NULL)
8515 {
8516 /* No full path name match, try a match with a URL or a "nofile"
8517 * buffer, these don't use the full path. */
8518 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8519 if (buf->b_fname != NULL
8520 && (path_with_url(buf->b_fname)
8521#ifdef FEAT_QUICKFIX
8522 || bt_nofile(buf)
8523#endif
8524 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008525 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008526 break;
8527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 }
8529 return buf;
8530}
8531
8532/*
8533 * "bufexists(expr)" function
8534 */
8535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008536f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008537 typval_T *argvars;
8538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008540 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541}
8542
8543/*
8544 * "buflisted(expr)" function
8545 */
8546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008547f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008548 typval_T *argvars;
8549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551 buf_T *buf;
8552
8553 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008554 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555}
8556
8557/*
8558 * "bufloaded(expr)" function
8559 */
8560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008561f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008562 typval_T *argvars;
8563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564{
8565 buf_T *buf;
8566
8567 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008568 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569}
8570
Bram Moolenaar33570922005-01-25 22:26:29 +00008571static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008572
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573/*
8574 * Get buffer by number or pattern.
8575 */
8576 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008577get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008578 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 int save_magic;
8582 char_u *save_cpo;
8583 buf_T *buf;
8584
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008585 if (tv->v_type == VAR_NUMBER)
8586 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008587 if (tv->v_type != VAR_STRING)
8588 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 if (name == NULL || *name == NUL)
8590 return curbuf;
8591 if (name[0] == '$' && name[1] == NUL)
8592 return lastbuf;
8593
8594 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8595 save_magic = p_magic;
8596 p_magic = TRUE;
8597 save_cpo = p_cpo;
8598 p_cpo = (char_u *)"";
8599
8600 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8601 TRUE, FALSE));
8602
8603 p_magic = save_magic;
8604 p_cpo = save_cpo;
8605
8606 /* If not found, try expanding the name, like done for bufexists(). */
8607 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008608 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609
8610 return buf;
8611}
8612
8613/*
8614 * "bufname(expr)" function
8615 */
8616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008617f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008618 typval_T *argvars;
8619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620{
8621 buf_T *buf;
8622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008623 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008625 buf = get_buf_tv(&argvars[0]);
8626 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008628 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008630 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 --emsg_off;
8632}
8633
8634/*
8635 * "bufnr(expr)" function
8636 */
8637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008638f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008639 typval_T *argvars;
8640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641{
8642 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008643 int error = FALSE;
8644 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008646 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008648 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008649 --emsg_off;
8650
8651 /* If the buffer isn't found and the second argument is not zero create a
8652 * new buffer. */
8653 if (buf == NULL
8654 && argvars[1].v_type != VAR_UNKNOWN
8655 && get_tv_number_chk(&argvars[1], &error) != 0
8656 && !error
8657 && (name = get_tv_string_chk(&argvars[0])) != NULL
8658 && !error)
8659 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8660
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008662 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008664 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665}
8666
8667/*
8668 * "bufwinnr(nr)" function
8669 */
8670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008671f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008672 typval_T *argvars;
8673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674{
8675#ifdef FEAT_WINDOWS
8676 win_T *wp;
8677 int winnr = 0;
8678#endif
8679 buf_T *buf;
8680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008681 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008683 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684#ifdef FEAT_WINDOWS
8685 for (wp = firstwin; wp; wp = wp->w_next)
8686 {
8687 ++winnr;
8688 if (wp->w_buffer == buf)
8689 break;
8690 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008691 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008693 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694#endif
8695 --emsg_off;
8696}
8697
8698/*
8699 * "byte2line(byte)" function
8700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008702f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008703 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705{
8706#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008707 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708#else
8709 long boff = 0;
8710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008711 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008713 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008715 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 (linenr_T)0, &boff);
8717#endif
8718}
8719
8720/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008721 * "byteidx()" function
8722 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008724f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008725 typval_T *argvars;
8726 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008727{
8728#ifdef FEAT_MBYTE
8729 char_u *t;
8730#endif
8731 char_u *str;
8732 long idx;
8733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008734 str = get_tv_string_chk(&argvars[0]);
8735 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008736 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008737 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008738 return;
8739
8740#ifdef FEAT_MBYTE
8741 t = str;
8742 for ( ; idx > 0; idx--)
8743 {
8744 if (*t == NUL) /* EOL reached */
8745 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008746 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008747 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008748 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008749#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008750 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008752#endif
8753}
8754
8755/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008756 * "call(func, arglist)" function
8757 */
8758 static void
8759f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008760 typval_T *argvars;
8761 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008762{
8763 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008764 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008765 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008766 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008767 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008768 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008769
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008770 if (argvars[1].v_type != VAR_LIST)
8771 {
8772 EMSG(_(e_listreq));
8773 return;
8774 }
8775 if (argvars[1].vval.v_list == NULL)
8776 return;
8777
8778 if (argvars[0].v_type == VAR_FUNC)
8779 func = argvars[0].vval.v_string;
8780 else
8781 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008782 if (*func == NUL)
8783 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008784
Bram Moolenaare9a41262005-01-15 22:18:47 +00008785 if (argvars[2].v_type != VAR_UNKNOWN)
8786 {
8787 if (argvars[2].v_type != VAR_DICT)
8788 {
8789 EMSG(_(e_dictreq));
8790 return;
8791 }
8792 selfdict = argvars[2].vval.v_dict;
8793 }
8794
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008795 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8796 item = item->li_next)
8797 {
8798 if (argc == MAX_FUNC_ARGS)
8799 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008800 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008801 break;
8802 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008803 /* Make a copy of each argument. This is needed to be able to set
8804 * v_lock to VAR_FIXED in the copy without changing the original list.
8805 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008806 copy_tv(&item->li_tv, &argv[argc++]);
8807 }
8808
8809 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008810 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008811 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8812 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008813
8814 /* Free the arguments. */
8815 while (argc > 0)
8816 clear_tv(&argv[--argc]);
8817}
8818
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008819#ifdef FEAT_FLOAT
8820/*
8821 * "ceil({float})" function
8822 */
8823 static void
8824f_ceil(argvars, rettv)
8825 typval_T *argvars;
8826 typval_T *rettv;
8827{
8828 float_T f;
8829
8830 rettv->v_type = VAR_FLOAT;
8831 if (get_float_arg(argvars, &f) == OK)
8832 rettv->vval.v_float = ceil(f);
8833 else
8834 rettv->vval.v_float = 0.0;
8835}
8836#endif
8837
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008838/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008839 * "changenr()" function
8840 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008841 static void
8842f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008843 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008844 typval_T *rettv;
8845{
8846 rettv->vval.v_number = curbuf->b_u_seq_cur;
8847}
8848
8849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 * "char2nr(string)" function
8851 */
8852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008853f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008854 typval_T *argvars;
8855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856{
8857#ifdef FEAT_MBYTE
8858 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008859 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 else
8861#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863}
8864
8865/*
8866 * "cindent(lnum)" function
8867 */
8868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008869f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008870 typval_T *argvars;
8871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872{
8873#ifdef FEAT_CINDENT
8874 pos_T pos;
8875 linenr_T lnum;
8876
8877 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008878 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8880 {
8881 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008882 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 curwin->w_cursor = pos;
8884 }
8885 else
8886#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008887 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888}
8889
8890/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008891 * "clearmatches()" function
8892 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008893 static void
8894f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008895 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008896 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008897{
8898#ifdef FEAT_SEARCH_EXTRA
8899 clear_matches(curwin);
8900#endif
8901}
8902
8903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 * "col(string)" function
8905 */
8906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008907f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008908 typval_T *argvars;
8909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910{
8911 colnr_T col = 0;
8912 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008913 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008915 fp = var2fpos(&argvars[0], FALSE, &fnum);
8916 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 {
8918 if (fp->col == MAXCOL)
8919 {
8920 /* '> can be MAXCOL, get the length of the line then */
8921 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008922 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 else
8924 col = MAXCOL;
8925 }
8926 else
8927 {
8928 col = fp->col + 1;
8929#ifdef FEAT_VIRTUALEDIT
8930 /* col(".") when the cursor is on the NUL at the end of the line
8931 * because of "coladd" can be seen as an extra column. */
8932 if (virtual_active() && fp == &curwin->w_cursor)
8933 {
8934 char_u *p = ml_get_cursor();
8935
8936 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8937 curwin->w_virtcol - curwin->w_cursor.coladd))
8938 {
8939# ifdef FEAT_MBYTE
8940 int l;
8941
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008942 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 col += l;
8944# else
8945 if (*p != NUL && p[1] == NUL)
8946 ++col;
8947# endif
8948 }
8949 }
8950#endif
8951 }
8952 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008953 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954}
8955
Bram Moolenaar572cb562005-08-05 21:35:02 +00008956#if defined(FEAT_INS_EXPAND)
8957/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008958 * "complete()" function
8959 */
Bram Moolenaarade00832006-03-10 21:46:58 +00008960 static void
8961f_complete(argvars, rettv)
8962 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008963 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00008964{
8965 int startcol;
8966
8967 if ((State & INSERT) == 0)
8968 {
8969 EMSG(_("E785: complete() can only be used in Insert mode"));
8970 return;
8971 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008972
8973 /* Check for undo allowed here, because if something was already inserted
8974 * the line was already saved for undo and this check isn't done. */
8975 if (!undo_allowed())
8976 return;
8977
Bram Moolenaarade00832006-03-10 21:46:58 +00008978 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8979 {
8980 EMSG(_(e_invarg));
8981 return;
8982 }
8983
8984 startcol = get_tv_number_chk(&argvars[0], NULL);
8985 if (startcol <= 0)
8986 return;
8987
8988 set_completion(startcol - 1, argvars[1].vval.v_list);
8989}
8990
8991/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008992 * "complete_add()" function
8993 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00008994 static void
8995f_complete_add(argvars, rettv)
8996 typval_T *argvars;
8997 typval_T *rettv;
8998{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008999 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009000}
9001
9002/*
9003 * "complete_check()" function
9004 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009005 static void
9006f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009007 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009008 typval_T *rettv;
9009{
9010 int saved = RedrawingDisabled;
9011
9012 RedrawingDisabled = 0;
9013 ins_compl_check_keys(0);
9014 rettv->vval.v_number = compl_interrupted;
9015 RedrawingDisabled = saved;
9016}
9017#endif
9018
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019/*
9020 * "confirm(message, buttons[, default [, type]])" function
9021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009023f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009024 typval_T *argvars UNUSED;
9025 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026{
9027#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9028 char_u *message;
9029 char_u *buttons = NULL;
9030 char_u buf[NUMBUFLEN];
9031 char_u buf2[NUMBUFLEN];
9032 int def = 1;
9033 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009034 char_u *typestr;
9035 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009037 message = get_tv_string_chk(&argvars[0]);
9038 if (message == NULL)
9039 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009040 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009042 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9043 if (buttons == NULL)
9044 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009045 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009047 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009048 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009050 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9051 if (typestr == NULL)
9052 error = TRUE;
9053 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009055 switch (TOUPPER_ASC(*typestr))
9056 {
9057 case 'E': type = VIM_ERROR; break;
9058 case 'Q': type = VIM_QUESTION; break;
9059 case 'I': type = VIM_INFO; break;
9060 case 'W': type = VIM_WARNING; break;
9061 case 'G': type = VIM_GENERIC; break;
9062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 }
9064 }
9065 }
9066 }
9067
9068 if (buttons == NULL || *buttons == NUL)
9069 buttons = (char_u *)_("&Ok");
9070
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009071 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009072 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074#endif
9075}
9076
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009077/*
9078 * "copy()" function
9079 */
9080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009082 typval_T *argvars;
9083 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009084{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009085 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009086}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009088#ifdef FEAT_FLOAT
9089/*
9090 * "cos()" function
9091 */
9092 static void
9093f_cos(argvars, rettv)
9094 typval_T *argvars;
9095 typval_T *rettv;
9096{
9097 float_T f;
9098
9099 rettv->v_type = VAR_FLOAT;
9100 if (get_float_arg(argvars, &f) == OK)
9101 rettv->vval.v_float = cos(f);
9102 else
9103 rettv->vval.v_float = 0.0;
9104}
9105#endif
9106
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009108 * "count()" function
9109 */
9110 static void
9111f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009112 typval_T *argvars;
9113 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009114{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009115 long n = 0;
9116 int ic = FALSE;
9117
Bram Moolenaare9a41262005-01-15 22:18:47 +00009118 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009119 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009120 listitem_T *li;
9121 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009122 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009123
Bram Moolenaare9a41262005-01-15 22:18:47 +00009124 if ((l = argvars[0].vval.v_list) != NULL)
9125 {
9126 li = l->lv_first;
9127 if (argvars[2].v_type != VAR_UNKNOWN)
9128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009129 int error = FALSE;
9130
9131 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009132 if (argvars[3].v_type != VAR_UNKNOWN)
9133 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009134 idx = get_tv_number_chk(&argvars[3], &error);
9135 if (!error)
9136 {
9137 li = list_find(l, idx);
9138 if (li == NULL)
9139 EMSGN(_(e_listidx), idx);
9140 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009141 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009142 if (error)
9143 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009144 }
9145
9146 for ( ; li != NULL; li = li->li_next)
9147 if (tv_equal(&li->li_tv, &argvars[1], ic))
9148 ++n;
9149 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009150 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009151 else if (argvars[0].v_type == VAR_DICT)
9152 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009153 int todo;
9154 dict_T *d;
9155 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009156
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009157 if ((d = argvars[0].vval.v_dict) != NULL)
9158 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009159 int error = FALSE;
9160
Bram Moolenaare9a41262005-01-15 22:18:47 +00009161 if (argvars[2].v_type != VAR_UNKNOWN)
9162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009163 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009164 if (argvars[3].v_type != VAR_UNKNOWN)
9165 EMSG(_(e_invarg));
9166 }
9167
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009168 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009169 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009170 {
9171 if (!HASHITEM_EMPTY(hi))
9172 {
9173 --todo;
9174 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9175 ++n;
9176 }
9177 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009178 }
9179 }
9180 else
9181 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009182 rettv->vval.v_number = n;
9183}
9184
9185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9187 *
9188 * Checks the existence of a cscope connection.
9189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009191f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009192 typval_T *argvars UNUSED;
9193 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194{
9195#ifdef FEAT_CSCOPE
9196 int num = 0;
9197 char_u *dbpath = NULL;
9198 char_u *prepend = NULL;
9199 char_u buf[NUMBUFLEN];
9200
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009201 if (argvars[0].v_type != VAR_UNKNOWN
9202 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009204 num = (int)get_tv_number(&argvars[0]);
9205 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009206 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009207 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 }
9209
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009210 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211#endif
9212}
9213
9214/*
9215 * "cursor(lnum, col)" function
9216 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009217 * Moves the cursor to the specified line and column.
9218 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009221f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009222 typval_T *argvars;
9223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224{
9225 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009226#ifdef FEAT_VIRTUALEDIT
9227 long coladd = 0;
9228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009230 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009231 if (argvars[1].v_type == VAR_UNKNOWN)
9232 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009233 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009234
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009235 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009236 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009237 line = pos.lnum;
9238 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009239#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009240 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009241#endif
9242 }
9243 else
9244 {
9245 line = get_tv_lnum(argvars);
9246 col = get_tv_number_chk(&argvars[1], NULL);
9247#ifdef FEAT_VIRTUALEDIT
9248 if (argvars[2].v_type != VAR_UNKNOWN)
9249 coladd = get_tv_number_chk(&argvars[2], NULL);
9250#endif
9251 }
9252 if (line < 0 || col < 0
9253#ifdef FEAT_VIRTUALEDIT
9254 || coladd < 0
9255#endif
9256 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009257 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 if (line > 0)
9259 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 if (col > 0)
9261 curwin->w_cursor.col = col - 1;
9262#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009263 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264#endif
9265
9266 /* Make sure the cursor is in a valid position. */
9267 check_cursor();
9268#ifdef FEAT_MBYTE
9269 /* Correct cursor for multi-byte character. */
9270 if (has_mbyte)
9271 mb_adjust_cursor();
9272#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009273
9274 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009275 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276}
9277
9278/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009279 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 */
9281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009282f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009283 typval_T *argvars;
9284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009286 int noref = 0;
9287
9288 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009289 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009290 if (noref < 0 || noref > 1)
9291 EMSG(_(e_invarg));
9292 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009293 {
9294 current_copyID += COPYID_INC;
9295 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297}
9298
9299/*
9300 * "delete()" function
9301 */
9302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009304 typval_T *argvars;
9305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306{
9307 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009308 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009310 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311}
9312
9313/*
9314 * "did_filetype()" function
9315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009317f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009318 typval_T *argvars UNUSED;
9319 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320{
9321#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009322 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323#endif
9324}
9325
9326/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009327 * "diff_filler()" function
9328 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009330f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009331 typval_T *argvars UNUSED;
9332 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009333{
9334#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009335 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009336#endif
9337}
9338
9339/*
9340 * "diff_hlID()" function
9341 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009343f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009344 typval_T *argvars UNUSED;
9345 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009346{
9347#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009348 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009349 static linenr_T prev_lnum = 0;
9350 static int changedtick = 0;
9351 static int fnum = 0;
9352 static int change_start = 0;
9353 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009354 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009355 int filler_lines;
9356 int col;
9357
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009358 if (lnum < 0) /* ignore type error in {lnum} arg */
9359 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009360 if (lnum != prev_lnum
9361 || changedtick != curbuf->b_changedtick
9362 || fnum != curbuf->b_fnum)
9363 {
9364 /* New line, buffer, change: need to get the values. */
9365 filler_lines = diff_check(curwin, lnum);
9366 if (filler_lines < 0)
9367 {
9368 if (filler_lines == -1)
9369 {
9370 change_start = MAXCOL;
9371 change_end = -1;
9372 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9373 hlID = HLF_ADD; /* added line */
9374 else
9375 hlID = HLF_CHD; /* changed line */
9376 }
9377 else
9378 hlID = HLF_ADD; /* added line */
9379 }
9380 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009381 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009382 prev_lnum = lnum;
9383 changedtick = curbuf->b_changedtick;
9384 fnum = curbuf->b_fnum;
9385 }
9386
9387 if (hlID == HLF_CHD || hlID == HLF_TXD)
9388 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009389 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009390 if (col >= change_start && col <= change_end)
9391 hlID = HLF_TXD; /* changed text */
9392 else
9393 hlID = HLF_CHD; /* changed line */
9394 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009395 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009396#endif
9397}
9398
9399/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009400 * "empty({expr})" function
9401 */
9402 static void
9403f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009404 typval_T *argvars;
9405 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009406{
9407 int n;
9408
9409 switch (argvars[0].v_type)
9410 {
9411 case VAR_STRING:
9412 case VAR_FUNC:
9413 n = argvars[0].vval.v_string == NULL
9414 || *argvars[0].vval.v_string == NUL;
9415 break;
9416 case VAR_NUMBER:
9417 n = argvars[0].vval.v_number == 0;
9418 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009419#ifdef FEAT_FLOAT
9420 case VAR_FLOAT:
9421 n = argvars[0].vval.v_float == 0.0;
9422 break;
9423#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009424 case VAR_LIST:
9425 n = argvars[0].vval.v_list == NULL
9426 || argvars[0].vval.v_list->lv_first == NULL;
9427 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009428 case VAR_DICT:
9429 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009430 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009431 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009432 default:
9433 EMSG2(_(e_intern2), "f_empty()");
9434 n = 0;
9435 }
9436
9437 rettv->vval.v_number = n;
9438}
9439
9440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 * "escape({string}, {chars})" function
9442 */
9443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009445 typval_T *argvars;
9446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447{
9448 char_u buf[NUMBUFLEN];
9449
Bram Moolenaar758711c2005-02-02 23:11:38 +00009450 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9451 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453}
9454
9455/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009456 * "eval()" function
9457 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009458 static void
9459f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009460 typval_T *argvars;
9461 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009462{
9463 char_u *s;
9464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009465 s = get_tv_string_chk(&argvars[0]);
9466 if (s != NULL)
9467 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009468
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009469 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9470 {
9471 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009472 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009473 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009474 else if (*s != NUL)
9475 EMSG(_(e_trailing));
9476}
9477
9478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 * "eventhandler()" function
9480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009483 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009486 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487}
9488
9489/*
9490 * "executable()" function
9491 */
9492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009493f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009494 typval_T *argvars;
9495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498}
9499
9500/*
9501 * "exists()" function
9502 */
9503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009504f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009505 typval_T *argvars;
9506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507{
9508 char_u *p;
9509 char_u *name;
9510 int n = FALSE;
9511 int len = 0;
9512
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009513 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 if (*p == '$') /* environment variable */
9515 {
9516 /* first try "normal" environment variables (fast) */
9517 if (mch_getenv(p + 1) != NULL)
9518 n = TRUE;
9519 else
9520 {
9521 /* try expanding things like $VIM and ${HOME} */
9522 p = expand_env_save(p);
9523 if (p != NULL && *p != '$')
9524 n = TRUE;
9525 vim_free(p);
9526 }
9527 }
9528 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009530 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009531 if (*skipwhite(p) != NUL)
9532 n = FALSE; /* trailing garbage */
9533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 else if (*p == '*') /* internal or user defined function */
9535 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009536 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 }
9538 else if (*p == ':')
9539 {
9540 n = cmd_exists(p + 1);
9541 }
9542 else if (*p == '#')
9543 {
9544#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009545 if (p[1] == '#')
9546 n = autocmd_supported(p + 2);
9547 else
9548 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549#endif
9550 }
9551 else /* internal variable */
9552 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009553 char_u *tofree;
9554 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009556 /* get_name_len() takes care of expanding curly braces */
9557 name = p;
9558 len = get_name_len(&p, &tofree, TRUE, FALSE);
9559 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009561 if (tofree != NULL)
9562 name = tofree;
9563 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9564 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009566 /* handle d.key, l[idx], f(expr) */
9567 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9568 if (n)
9569 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 }
9571 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009572 if (*p != NUL)
9573 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009575 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 }
9577
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009578 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579}
9580
9581/*
9582 * "expand()" function
9583 */
9584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009585f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009586 typval_T *argvars;
9587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588{
9589 char_u *s;
9590 int len;
9591 char_u *errormsg;
9592 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9593 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009594 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596 rettv->v_type = VAR_STRING;
9597 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 if (*s == '%' || *s == '#' || *s == '<')
9599 {
9600 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009601 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 --emsg_off;
9603 }
9604 else
9605 {
9606 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009607 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009608 if (argvars[1].v_type != VAR_UNKNOWN
9609 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009611 if (!error)
9612 {
9613 ExpandInit(&xpc);
9614 xpc.xp_context = EXPAND_FILES;
9615 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009616 }
9617 else
9618 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 }
9620}
9621
9622/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009623 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009624 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009625 */
9626 static void
9627f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009628 typval_T *argvars;
9629 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009630{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009631 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009632 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009633 list_T *l1, *l2;
9634 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009635 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009636 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009637
Bram Moolenaare9a41262005-01-15 22:18:47 +00009638 l1 = argvars[0].vval.v_list;
9639 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009640 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9641 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009642 {
9643 if (argvars[2].v_type != VAR_UNKNOWN)
9644 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009645 before = get_tv_number_chk(&argvars[2], &error);
9646 if (error)
9647 return; /* type error; errmsg already given */
9648
Bram Moolenaar758711c2005-02-02 23:11:38 +00009649 if (before == l1->lv_len)
9650 item = NULL;
9651 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009652 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009653 item = list_find(l1, before);
9654 if (item == NULL)
9655 {
9656 EMSGN(_(e_listidx), before);
9657 return;
9658 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009659 }
9660 }
9661 else
9662 item = NULL;
9663 list_extend(l1, l2, item);
9664
Bram Moolenaare9a41262005-01-15 22:18:47 +00009665 copy_tv(&argvars[0], rettv);
9666 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009667 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009668 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9669 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009670 dict_T *d1, *d2;
9671 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009672 char_u *action;
9673 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009674 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009675 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009676
9677 d1 = argvars[0].vval.v_dict;
9678 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009679 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9680 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009681 {
9682 /* Check the third argument. */
9683 if (argvars[2].v_type != VAR_UNKNOWN)
9684 {
9685 static char *(av[]) = {"keep", "force", "error"};
9686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009687 action = get_tv_string_chk(&argvars[2]);
9688 if (action == NULL)
9689 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009690 for (i = 0; i < 3; ++i)
9691 if (STRCMP(action, av[i]) == 0)
9692 break;
9693 if (i == 3)
9694 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009695 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009696 return;
9697 }
9698 }
9699 else
9700 action = (char_u *)"force";
9701
9702 /* Go over all entries in the second dict and add them to the
9703 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009704 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009705 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009706 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009707 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009708 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009709 --todo;
9710 di1 = dict_find(d1, hi2->hi_key, -1);
9711 if (di1 == NULL)
9712 {
9713 di1 = dictitem_copy(HI2DI(hi2));
9714 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9715 dictitem_free(di1);
9716 }
9717 else if (*action == 'e')
9718 {
9719 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9720 break;
9721 }
9722 else if (*action == 'f')
9723 {
9724 clear_tv(&di1->di_tv);
9725 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9726 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009727 }
9728 }
9729
Bram Moolenaare9a41262005-01-15 22:18:47 +00009730 copy_tv(&argvars[0], rettv);
9731 }
9732 }
9733 else
9734 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009735}
9736
9737/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009738 * "feedkeys()" function
9739 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009740 static void
9741f_feedkeys(argvars, rettv)
9742 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009743 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009744{
9745 int remap = TRUE;
9746 char_u *keys, *flags;
9747 char_u nbuf[NUMBUFLEN];
9748 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009749 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009750
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009751 /* This is not allowed in the sandbox. If the commands would still be
9752 * executed in the sandbox it would be OK, but it probably happens later,
9753 * when "sandbox" is no longer set. */
9754 if (check_secure())
9755 return;
9756
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009757 keys = get_tv_string(&argvars[0]);
9758 if (*keys != NUL)
9759 {
9760 if (argvars[1].v_type != VAR_UNKNOWN)
9761 {
9762 flags = get_tv_string_buf(&argvars[1], nbuf);
9763 for ( ; *flags != NUL; ++flags)
9764 {
9765 switch (*flags)
9766 {
9767 case 'n': remap = FALSE; break;
9768 case 'm': remap = TRUE; break;
9769 case 't': typed = TRUE; break;
9770 }
9771 }
9772 }
9773
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009774 /* Need to escape K_SPECIAL and CSI before putting the string in the
9775 * typeahead buffer. */
9776 keys_esc = vim_strsave_escape_csi(keys);
9777 if (keys_esc != NULL)
9778 {
9779 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009780 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009781 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009782 if (vgetc_busy)
9783 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009784 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009785 }
9786}
9787
9788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 * "filereadable()" function
9790 */
9791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009793 typval_T *argvars;
9794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795{
Bram Moolenaarc236c162008-07-13 17:41:49 +00009796 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 char_u *p;
9798 int n;
9799
Bram Moolenaarc236c162008-07-13 17:41:49 +00009800#ifndef O_NONBLOCK
9801# define O_NONBLOCK 0
9802#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +00009804 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
9805 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 {
9807 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009808 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 }
9810 else
9811 n = FALSE;
9812
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009813 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814}
9815
9816/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009817 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 * rights to write into.
9819 */
9820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009821f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009822 typval_T *argvars;
9823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009825 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826}
9827
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009828static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009829
9830 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009831findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009832 typval_T *argvars;
9833 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009834 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009835{
9836#ifdef FEAT_SEARCHPATH
9837 char_u *fname;
9838 char_u *fresult = NULL;
9839 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9840 char_u *p;
9841 char_u pathbuf[NUMBUFLEN];
9842 int count = 1;
9843 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009844 int error = FALSE;
9845#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009846
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009847 rettv->vval.v_string = NULL;
9848 rettv->v_type = VAR_STRING;
9849
9850#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009851 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009852
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009853 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009854 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009855 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9856 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009857 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009858 else
9859 {
9860 if (*p != NUL)
9861 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009863 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009864 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009865 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009866 }
9867
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009868 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9869 error = TRUE;
9870
9871 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009873 do
9874 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009875 if (rettv->v_type == VAR_STRING)
9876 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009877 fresult = find_file_in_path_option(first ? fname : NULL,
9878 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009879 0, first, path,
9880 find_what,
9881 curbuf->b_ffname,
9882 find_what == FINDFILE_DIR
9883 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009884 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009885
9886 if (fresult != NULL && rettv->v_type == VAR_LIST)
9887 list_append_string(rettv->vval.v_list, fresult, -1);
9888
9889 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009890 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009891
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009892 if (rettv->v_type == VAR_STRING)
9893 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009894#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009895}
9896
Bram Moolenaar33570922005-01-25 22:26:29 +00009897static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9898static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009899
9900/*
9901 * Implementation of map() and filter().
9902 */
9903 static void
9904filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009905 typval_T *argvars;
9906 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009907 int map;
9908{
9909 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009910 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009911 listitem_T *li, *nli;
9912 list_T *l = NULL;
9913 dictitem_T *di;
9914 hashtab_T *ht;
9915 hashitem_T *hi;
9916 dict_T *d = NULL;
9917 typval_T save_val;
9918 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009919 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009920 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009921 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009922 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009923
Bram Moolenaare9a41262005-01-15 22:18:47 +00009924 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009925 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009926 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009927 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 return;
9929 }
9930 else if (argvars[0].v_type == VAR_DICT)
9931 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009932 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009933 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009934 return;
9935 }
9936 else
9937 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009938 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009939 return;
9940 }
9941
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009942 expr = get_tv_string_buf_chk(&argvars[1], buf);
9943 /* On type errors, the preceding call has already displayed an error
9944 * message. Avoid a misleading error message for an empty string that
9945 * was not passed as argument. */
9946 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009948 prepare_vimvar(VV_VAL, &save_val);
9949 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009950
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009951 /* We reset "did_emsg" to be able to detect whether an error
9952 * occurred during evaluation of the expression. */
9953 save_did_emsg = did_emsg;
9954 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009956 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009958 prepare_vimvar(VV_KEY, &save_key);
9959 vimvars[VV_KEY].vv_type = VAR_STRING;
9960
9961 ht = &d->dv_hashtab;
9962 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009963 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009964 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009965 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009966 if (!HASHITEM_EMPTY(hi))
9967 {
9968 --todo;
9969 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009970 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009971 break;
9972 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009973 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009974 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009975 break;
9976 if (!map && rem)
9977 dictitem_remove(d, di);
9978 clear_tv(&vimvars[VV_KEY].vv_tv);
9979 }
9980 }
9981 hash_unlock(ht);
9982
9983 restore_vimvar(VV_KEY, &save_key);
9984 }
9985 else
9986 {
9987 for (li = l->lv_first; li != NULL; li = nli)
9988 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009989 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009990 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009991 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009992 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009993 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009994 break;
9995 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009996 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009997 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009998 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009999
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010000 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010001
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010002 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010003 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010004
10005 copy_tv(&argvars[0], rettv);
10006}
10007
10008 static int
10009filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010010 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010011 char_u *expr;
10012 int map;
10013 int *remp;
10014{
Bram Moolenaar33570922005-01-25 22:26:29 +000010015 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010016 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010017 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010018
Bram Moolenaar33570922005-01-25 22:26:29 +000010019 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010020 s = expr;
10021 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010022 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010023 if (*s != NUL) /* check for trailing chars after expr */
10024 {
10025 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010026 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010027 }
10028 if (map)
10029 {
10030 /* map(): replace the list item value */
10031 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010032 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010033 *tv = rettv;
10034 }
10035 else
10036 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010037 int error = FALSE;
10038
Bram Moolenaare9a41262005-01-15 22:18:47 +000010039 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010040 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010041 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010042 /* On type error, nothing has been removed; return FAIL to stop the
10043 * loop. The error message was given by get_tv_number_chk(). */
10044 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010045 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010046 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010047 retval = OK;
10048theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010049 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010050 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010051}
10052
10053/*
10054 * "filter()" function
10055 */
10056 static void
10057f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010058 typval_T *argvars;
10059 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010060{
10061 filter_map(argvars, rettv, FALSE);
10062}
10063
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010064/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010065 * "finddir({fname}[, {path}[, {count}]])" function
10066 */
10067 static void
10068f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010069 typval_T *argvars;
10070 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010071{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010072 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010073}
10074
10075/*
10076 * "findfile({fname}[, {path}[, {count}]])" function
10077 */
10078 static void
10079f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010080 typval_T *argvars;
10081 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010082{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010083 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010084}
10085
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010086#ifdef FEAT_FLOAT
10087/*
10088 * "float2nr({float})" function
10089 */
10090 static void
10091f_float2nr(argvars, rettv)
10092 typval_T *argvars;
10093 typval_T *rettv;
10094{
10095 float_T f;
10096
10097 if (get_float_arg(argvars, &f) == OK)
10098 {
10099 if (f < -0x7fffffff)
10100 rettv->vval.v_number = -0x7fffffff;
10101 else if (f > 0x7fffffff)
10102 rettv->vval.v_number = 0x7fffffff;
10103 else
10104 rettv->vval.v_number = (varnumber_T)f;
10105 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010106}
10107
10108/*
10109 * "floor({float})" function
10110 */
10111 static void
10112f_floor(argvars, rettv)
10113 typval_T *argvars;
10114 typval_T *rettv;
10115{
10116 float_T f;
10117
10118 rettv->v_type = VAR_FLOAT;
10119 if (get_float_arg(argvars, &f) == OK)
10120 rettv->vval.v_float = floor(f);
10121 else
10122 rettv->vval.v_float = 0.0;
10123}
10124#endif
10125
Bram Moolenaar0d660222005-01-07 21:51:51 +000010126/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010127 * "fnameescape({string})" function
10128 */
10129 static void
10130f_fnameescape(argvars, rettv)
10131 typval_T *argvars;
10132 typval_T *rettv;
10133{
10134 rettv->vval.v_string = vim_strsave_fnameescape(
10135 get_tv_string(&argvars[0]), FALSE);
10136 rettv->v_type = VAR_STRING;
10137}
10138
10139/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 * "fnamemodify({fname}, {mods})" function
10141 */
10142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010144 typval_T *argvars;
10145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146{
10147 char_u *fname;
10148 char_u *mods;
10149 int usedlen = 0;
10150 int len;
10151 char_u *fbuf = NULL;
10152 char_u buf[NUMBUFLEN];
10153
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010154 fname = get_tv_string_chk(&argvars[0]);
10155 mods = get_tv_string_buf_chk(&argvars[1], buf);
10156 if (fname == NULL || mods == NULL)
10157 fname = NULL;
10158 else
10159 {
10160 len = (int)STRLEN(fname);
10161 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010164 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010166 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010168 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 vim_free(fbuf);
10170}
10171
Bram Moolenaar33570922005-01-25 22:26:29 +000010172static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173
10174/*
10175 * "foldclosed()" function
10176 */
10177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010178foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010179 typval_T *argvars;
10180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 int end;
10182{
10183#ifdef FEAT_FOLDING
10184 linenr_T lnum;
10185 linenr_T first, last;
10186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010187 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10189 {
10190 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10191 {
10192 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010193 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010195 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 return;
10197 }
10198 }
10199#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010200 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201}
10202
10203/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010204 * "foldclosed()" function
10205 */
10206 static void
10207f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010208 typval_T *argvars;
10209 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010210{
10211 foldclosed_both(argvars, rettv, FALSE);
10212}
10213
10214/*
10215 * "foldclosedend()" function
10216 */
10217 static void
10218f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010219 typval_T *argvars;
10220 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010221{
10222 foldclosed_both(argvars, rettv, TRUE);
10223}
10224
10225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 * "foldlevel()" function
10227 */
10228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010229f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010230 typval_T *argvars;
10231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232{
10233#ifdef FEAT_FOLDING
10234 linenr_T lnum;
10235
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010236 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010238 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240}
10241
10242/*
10243 * "foldtext()" function
10244 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010246f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010247 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249{
10250#ifdef FEAT_FOLDING
10251 linenr_T lnum;
10252 char_u *s;
10253 char_u *r;
10254 int len;
10255 char *txt;
10256#endif
10257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010258 rettv->v_type = VAR_STRING;
10259 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010261 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10262 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10263 <= curbuf->b_ml.ml_line_count
10264 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 {
10266 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010267 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10268 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 {
10270 if (!linewhite(lnum))
10271 break;
10272 ++lnum;
10273 }
10274
10275 /* Find interesting text in this line. */
10276 s = skipwhite(ml_get(lnum));
10277 /* skip C comment-start */
10278 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010281 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010282 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010283 {
10284 s = skipwhite(ml_get(lnum + 1));
10285 if (*s == '*')
10286 s = skipwhite(s + 1);
10287 }
10288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 txt = _("+-%s%3ld lines: ");
10290 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010291 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 + 20 /* for %3ld */
10293 + STRLEN(s))); /* concatenated */
10294 if (r != NULL)
10295 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010296 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10297 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10298 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 len = (int)STRLEN(r);
10300 STRCAT(r, s);
10301 /* remove 'foldmarker' and 'commentstring' */
10302 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010303 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 }
10305 }
10306#endif
10307}
10308
10309/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010310 * "foldtextresult(lnum)" function
10311 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010313f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010314 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010315 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010316{
10317#ifdef FEAT_FOLDING
10318 linenr_T lnum;
10319 char_u *text;
10320 char_u buf[51];
10321 foldinfo_T foldinfo;
10322 int fold_count;
10323#endif
10324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010325 rettv->v_type = VAR_STRING;
10326 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010327#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010328 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010329 /* treat illegal types and illegal string values for {lnum} the same */
10330 if (lnum < 0)
10331 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010332 fold_count = foldedCount(curwin, lnum, &foldinfo);
10333 if (fold_count > 0)
10334 {
10335 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10336 &foldinfo, buf);
10337 if (text == buf)
10338 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010339 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010340 }
10341#endif
10342}
10343
10344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 * "foreground()" function
10346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010348f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010349 typval_T *argvars UNUSED;
10350 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352#ifdef FEAT_GUI
10353 if (gui.in_use)
10354 gui_mch_set_foreground();
10355#else
10356# ifdef WIN32
10357 win32_set_foreground();
10358# endif
10359#endif
10360}
10361
10362/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010363 * "function()" function
10364 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010366f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010367 typval_T *argvars;
10368 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010369{
10370 char_u *s;
10371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010372 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010373 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010374 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010375 /* Don't check an autoload name for existence here. */
10376 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010377 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010378 else
10379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010380 rettv->vval.v_string = vim_strsave(s);
10381 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010382 }
10383}
10384
10385/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010386 * "garbagecollect()" function
10387 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010388 static void
10389f_garbagecollect(argvars, rettv)
10390 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010391 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010392{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010393 /* This is postponed until we are back at the toplevel, because we may be
10394 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10395 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010396
10397 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10398 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010399}
10400
10401/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010402 * "get()" function
10403 */
10404 static void
10405f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010406 typval_T *argvars;
10407 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010408{
Bram Moolenaar33570922005-01-25 22:26:29 +000010409 listitem_T *li;
10410 list_T *l;
10411 dictitem_T *di;
10412 dict_T *d;
10413 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010414
Bram Moolenaare9a41262005-01-15 22:18:47 +000010415 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010416 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010417 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010419 int error = FALSE;
10420
10421 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10422 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010423 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010424 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010425 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010426 else if (argvars[0].v_type == VAR_DICT)
10427 {
10428 if ((d = argvars[0].vval.v_dict) != NULL)
10429 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010430 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010431 if (di != NULL)
10432 tv = &di->di_tv;
10433 }
10434 }
10435 else
10436 EMSG2(_(e_listdictarg), "get()");
10437
10438 if (tv == NULL)
10439 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010440 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010441 copy_tv(&argvars[2], rettv);
10442 }
10443 else
10444 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010445}
10446
Bram Moolenaar342337a2005-07-21 21:11:17 +000010447static 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 +000010448
10449/*
10450 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010451 * Return a range (from start to end) of lines in rettv from the specified
10452 * buffer.
10453 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010454 */
10455 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010456get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010457 buf_T *buf;
10458 linenr_T start;
10459 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010460 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010461 typval_T *rettv;
10462{
10463 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010464
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010465 if (retlist && rettv_list_alloc(rettv) == FAIL)
10466 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010467
10468 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10469 return;
10470
10471 if (!retlist)
10472 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010473 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10474 p = ml_get_buf(buf, start, FALSE);
10475 else
10476 p = (char_u *)"";
10477
10478 rettv->v_type = VAR_STRING;
10479 rettv->vval.v_string = vim_strsave(p);
10480 }
10481 else
10482 {
10483 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010484 return;
10485
10486 if (start < 1)
10487 start = 1;
10488 if (end > buf->b_ml.ml_line_count)
10489 end = buf->b_ml.ml_line_count;
10490 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010491 if (list_append_string(rettv->vval.v_list,
10492 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010493 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010494 }
10495}
10496
10497/*
10498 * "getbufline()" function
10499 */
10500 static void
10501f_getbufline(argvars, rettv)
10502 typval_T *argvars;
10503 typval_T *rettv;
10504{
10505 linenr_T lnum;
10506 linenr_T end;
10507 buf_T *buf;
10508
10509 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10510 ++emsg_off;
10511 buf = get_buf_tv(&argvars[0]);
10512 --emsg_off;
10513
Bram Moolenaar661b1822005-07-28 22:36:45 +000010514 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010515 if (argvars[2].v_type == VAR_UNKNOWN)
10516 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010517 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010518 end = get_tv_lnum_buf(&argvars[2], buf);
10519
Bram Moolenaar342337a2005-07-21 21:11:17 +000010520 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010521}
10522
Bram Moolenaar0d660222005-01-07 21:51:51 +000010523/*
10524 * "getbufvar()" function
10525 */
10526 static void
10527f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010528 typval_T *argvars;
10529 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010530{
10531 buf_T *buf;
10532 buf_T *save_curbuf;
10533 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010534 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010535
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10537 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010538 ++emsg_off;
10539 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010540
10541 rettv->v_type = VAR_STRING;
10542 rettv->vval.v_string = NULL;
10543
10544 if (buf != NULL && varname != NULL)
10545 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010546 /* set curbuf to be our buf, temporarily */
10547 save_curbuf = curbuf;
10548 curbuf = buf;
10549
Bram Moolenaar0d660222005-01-07 21:51:51 +000010550 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010551 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010552 else
10553 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010554 if (*varname == NUL)
10555 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10556 * scope prefix before the NUL byte is required by
10557 * find_var_in_ht(). */
10558 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010559 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010560 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010561 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010562 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010563 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010564
10565 /* restore previous notion of curbuf */
10566 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010567 }
10568
10569 --emsg_off;
10570}
10571
10572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 * "getchar()" function
10574 */
10575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010576f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010577 typval_T *argvars;
10578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579{
10580 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010581 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010583 /* Position the cursor. Needed after a message that ends in a space. */
10584 windgoto(msg_row, msg_col);
10585
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 ++no_mapping;
10587 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010588 for (;;)
10589 {
10590 if (argvars[0].v_type == VAR_UNKNOWN)
10591 /* getchar(): blocking wait. */
10592 n = safe_vgetc();
10593 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10594 /* getchar(1): only check if char avail */
10595 n = vpeekc();
10596 else if (error || vpeekc() == NUL)
10597 /* illegal argument or getchar(0) and no char avail: return zero */
10598 n = 0;
10599 else
10600 /* getchar(0) and char avail: return char */
10601 n = safe_vgetc();
10602 if (n == K_IGNORE)
10603 continue;
10604 break;
10605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606 --no_mapping;
10607 --allow_keys;
10608
Bram Moolenaar219b8702006-11-01 14:32:36 +000010609 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10610 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10611 vimvars[VV_MOUSE_COL].vv_nr = 0;
10612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010613 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 if (IS_SPECIAL(n) || mod_mask != 0)
10615 {
10616 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10617 int i = 0;
10618
10619 /* Turn a special key into three bytes, plus modifier. */
10620 if (mod_mask != 0)
10621 {
10622 temp[i++] = K_SPECIAL;
10623 temp[i++] = KS_MODIFIER;
10624 temp[i++] = mod_mask;
10625 }
10626 if (IS_SPECIAL(n))
10627 {
10628 temp[i++] = K_SPECIAL;
10629 temp[i++] = K_SECOND(n);
10630 temp[i++] = K_THIRD(n);
10631 }
10632#ifdef FEAT_MBYTE
10633 else if (has_mbyte)
10634 i += (*mb_char2bytes)(n, temp + i);
10635#endif
10636 else
10637 temp[i++] = n;
10638 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010639 rettv->v_type = VAR_STRING;
10640 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010641
10642#ifdef FEAT_MOUSE
10643 if (n == K_LEFTMOUSE
10644 || n == K_LEFTMOUSE_NM
10645 || n == K_LEFTDRAG
10646 || n == K_LEFTRELEASE
10647 || n == K_LEFTRELEASE_NM
10648 || n == K_MIDDLEMOUSE
10649 || n == K_MIDDLEDRAG
10650 || n == K_MIDDLERELEASE
10651 || n == K_RIGHTMOUSE
10652 || n == K_RIGHTDRAG
10653 || n == K_RIGHTRELEASE
10654 || n == K_X1MOUSE
10655 || n == K_X1DRAG
10656 || n == K_X1RELEASE
10657 || n == K_X2MOUSE
10658 || n == K_X2DRAG
10659 || n == K_X2RELEASE
10660 || n == K_MOUSEDOWN
10661 || n == K_MOUSEUP)
10662 {
10663 int row = mouse_row;
10664 int col = mouse_col;
10665 win_T *win;
10666 linenr_T lnum;
10667# ifdef FEAT_WINDOWS
10668 win_T *wp;
10669# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010670 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010671
10672 if (row >= 0 && col >= 0)
10673 {
10674 /* Find the window at the mouse coordinates and compute the
10675 * text position. */
10676 win = mouse_find_win(&row, &col);
10677 (void)mouse_comp_pos(win, &row, &col, &lnum);
10678# ifdef FEAT_WINDOWS
10679 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010680 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010681# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010682 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010683 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10684 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10685 }
10686 }
10687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 }
10689}
10690
10691/*
10692 * "getcharmod()" function
10693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010696 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010699 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700}
10701
10702/*
10703 * "getcmdline()" function
10704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010706f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010707 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010710 rettv->v_type = VAR_STRING;
10711 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712}
10713
10714/*
10715 * "getcmdpos()" function
10716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010718f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010719 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010722 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723}
10724
10725/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010726 * "getcmdtype()" function
10727 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010728 static void
10729f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010730 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010731 typval_T *rettv;
10732{
10733 rettv->v_type = VAR_STRING;
10734 rettv->vval.v_string = alloc(2);
10735 if (rettv->vval.v_string != NULL)
10736 {
10737 rettv->vval.v_string[0] = get_cmdline_type();
10738 rettv->vval.v_string[1] = NUL;
10739 }
10740}
10741
10742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 * "getcwd()" function
10744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010746f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010747 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749{
10750 char_u cwd[MAXPATHL];
10751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 else
10756 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010757 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010759 if (rettv->vval.v_string != NULL)
10760 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761#endif
10762 }
10763}
10764
10765/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010766 * "getfontname()" function
10767 */
10768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010769f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010770 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010771 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010772{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010773 rettv->v_type = VAR_STRING;
10774 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010775#ifdef FEAT_GUI
10776 if (gui.in_use)
10777 {
10778 GuiFont font;
10779 char_u *name = NULL;
10780
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010781 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010782 {
10783 /* Get the "Normal" font. Either the name saved by
10784 * hl_set_font_name() or from the font ID. */
10785 font = gui.norm_font;
10786 name = hl_get_font_name();
10787 }
10788 else
10789 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010790 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010791 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10792 return;
10793 font = gui_mch_get_font(name, FALSE);
10794 if (font == NOFONT)
10795 return; /* Invalid font name, return empty string. */
10796 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010797 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010798 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010799 gui_mch_free_font(font);
10800 }
10801#endif
10802}
10803
10804/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010805 * "getfperm({fname})" function
10806 */
10807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010808f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010809 typval_T *argvars;
10810 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010811{
10812 char_u *fname;
10813 struct stat st;
10814 char_u *perm = NULL;
10815 char_u flags[] = "rwx";
10816 int i;
10817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010818 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010820 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010821 if (mch_stat((char *)fname, &st) >= 0)
10822 {
10823 perm = vim_strsave((char_u *)"---------");
10824 if (perm != NULL)
10825 {
10826 for (i = 0; i < 9; i++)
10827 {
10828 if (st.st_mode & (1 << (8 - i)))
10829 perm[i] = flags[i % 3];
10830 }
10831 }
10832 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010833 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010834}
10835
10836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837 * "getfsize({fname})" function
10838 */
10839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010841 typval_T *argvars;
10842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843{
10844 char_u *fname;
10845 struct stat st;
10846
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010847 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010849 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850
10851 if (mch_stat((char *)fname, &st) >= 0)
10852 {
10853 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010854 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010857 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010858
10859 /* non-perfect check for overflow */
10860 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10861 rettv->vval.v_number = -2;
10862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 }
10864 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010865 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866}
10867
10868/*
10869 * "getftime({fname})" function
10870 */
10871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010873 typval_T *argvars;
10874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875{
10876 char_u *fname;
10877 struct stat st;
10878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010879 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880
10881 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010882 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010884 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885}
10886
10887/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010888 * "getftype({fname})" function
10889 */
10890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010891f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010892 typval_T *argvars;
10893 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010894{
10895 char_u *fname;
10896 struct stat st;
10897 char_u *type = NULL;
10898 char *t;
10899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010900 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010902 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010903 if (mch_lstat((char *)fname, &st) >= 0)
10904 {
10905#ifdef S_ISREG
10906 if (S_ISREG(st.st_mode))
10907 t = "file";
10908 else if (S_ISDIR(st.st_mode))
10909 t = "dir";
10910# ifdef S_ISLNK
10911 else if (S_ISLNK(st.st_mode))
10912 t = "link";
10913# endif
10914# ifdef S_ISBLK
10915 else if (S_ISBLK(st.st_mode))
10916 t = "bdev";
10917# endif
10918# ifdef S_ISCHR
10919 else if (S_ISCHR(st.st_mode))
10920 t = "cdev";
10921# endif
10922# ifdef S_ISFIFO
10923 else if (S_ISFIFO(st.st_mode))
10924 t = "fifo";
10925# endif
10926# ifdef S_ISSOCK
10927 else if (S_ISSOCK(st.st_mode))
10928 t = "fifo";
10929# endif
10930 else
10931 t = "other";
10932#else
10933# ifdef S_IFMT
10934 switch (st.st_mode & S_IFMT)
10935 {
10936 case S_IFREG: t = "file"; break;
10937 case S_IFDIR: t = "dir"; break;
10938# ifdef S_IFLNK
10939 case S_IFLNK: t = "link"; break;
10940# endif
10941# ifdef S_IFBLK
10942 case S_IFBLK: t = "bdev"; break;
10943# endif
10944# ifdef S_IFCHR
10945 case S_IFCHR: t = "cdev"; break;
10946# endif
10947# ifdef S_IFIFO
10948 case S_IFIFO: t = "fifo"; break;
10949# endif
10950# ifdef S_IFSOCK
10951 case S_IFSOCK: t = "socket"; break;
10952# endif
10953 default: t = "other";
10954 }
10955# else
10956 if (mch_isdir(fname))
10957 t = "dir";
10958 else
10959 t = "file";
10960# endif
10961#endif
10962 type = vim_strsave((char_u *)t);
10963 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010965}
10966
10967/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010968 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010969 */
10970 static void
10971f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010972 typval_T *argvars;
10973 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010974{
10975 linenr_T lnum;
10976 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010977 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010978
10979 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010980 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010981 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010982 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010983 retlist = FALSE;
10984 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010985 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010986 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010987 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010988 retlist = TRUE;
10989 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010990
Bram Moolenaar342337a2005-07-21 21:11:17 +000010991 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010992}
10993
10994/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010995 * "getmatches()" function
10996 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010997 static void
10998f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010999 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011000 typval_T *rettv;
11001{
11002#ifdef FEAT_SEARCH_EXTRA
11003 dict_T *dict;
11004 matchitem_T *cur = curwin->w_match_head;
11005
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011006 if (rettv_list_alloc(rettv) == OK)
11007 {
11008 while (cur != NULL)
11009 {
11010 dict = dict_alloc();
11011 if (dict == NULL)
11012 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011013 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11014 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11015 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11016 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11017 list_append_dict(rettv->vval.v_list, dict);
11018 cur = cur->next;
11019 }
11020 }
11021#endif
11022}
11023
11024/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011025 * "getpid()" function
11026 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011027 static void
11028f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011029 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011030 typval_T *rettv;
11031{
11032 rettv->vval.v_number = mch_get_pid();
11033}
11034
11035/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011036 * "getpos(string)" function
11037 */
11038 static void
11039f_getpos(argvars, rettv)
11040 typval_T *argvars;
11041 typval_T *rettv;
11042{
11043 pos_T *fp;
11044 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011045 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011046
11047 if (rettv_list_alloc(rettv) == OK)
11048 {
11049 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011050 fp = var2fpos(&argvars[0], TRUE, &fnum);
11051 if (fnum != -1)
11052 list_append_number(l, (varnumber_T)fnum);
11053 else
11054 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011055 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11056 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011057 list_append_number(l, (fp != NULL)
11058 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011059 : (varnumber_T)0);
11060 list_append_number(l,
11061#ifdef FEAT_VIRTUALEDIT
11062 (fp != NULL) ? (varnumber_T)fp->coladd :
11063#endif
11064 (varnumber_T)0);
11065 }
11066 else
11067 rettv->vval.v_number = FALSE;
11068}
11069
11070/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011071 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011072 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011073 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011074f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011075 typval_T *argvars UNUSED;
11076 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011077{
11078#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011079 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011080#endif
11081
Bram Moolenaar2641f772005-03-25 21:58:17 +000011082#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011083 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011084 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011085 wp = NULL;
11086 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11087 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011088 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011089 if (wp == NULL)
11090 return;
11091 }
11092
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011093 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011094 }
11095#endif
11096}
11097
11098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 * "getreg()" function
11100 */
11101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011102f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011103 typval_T *argvars;
11104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105{
11106 char_u *strregname;
11107 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011108 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011109 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011111 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011112 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011113 strregname = get_tv_string_chk(&argvars[0]);
11114 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011115 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011116 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011119 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 regname = (strregname == NULL ? '"' : *strregname);
11121 if (regname == 0)
11122 regname = '"';
11123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011124 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011125 rettv->vval.v_string = error ? NULL :
11126 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127}
11128
11129/*
11130 * "getregtype()" function
11131 */
11132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011134 typval_T *argvars;
11135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136{
11137 char_u *strregname;
11138 int regname;
11139 char_u buf[NUMBUFLEN + 2];
11140 long reglen = 0;
11141
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011142 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011143 {
11144 strregname = get_tv_string_chk(&argvars[0]);
11145 if (strregname == NULL) /* type error; errmsg already given */
11146 {
11147 rettv->v_type = VAR_STRING;
11148 rettv->vval.v_string = NULL;
11149 return;
11150 }
11151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 else
11153 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011154 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155
11156 regname = (strregname == NULL ? '"' : *strregname);
11157 if (regname == 0)
11158 regname = '"';
11159
11160 buf[0] = NUL;
11161 buf[1] = NUL;
11162 switch (get_reg_type(regname, &reglen))
11163 {
11164 case MLINE: buf[0] = 'V'; break;
11165 case MCHAR: buf[0] = 'v'; break;
11166#ifdef FEAT_VISUAL
11167 case MBLOCK:
11168 buf[0] = Ctrl_V;
11169 sprintf((char *)buf + 1, "%ld", reglen + 1);
11170 break;
11171#endif
11172 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011173 rettv->v_type = VAR_STRING;
11174 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175}
11176
11177/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011178 * "gettabwinvar()" function
11179 */
11180 static void
11181f_gettabwinvar(argvars, rettv)
11182 typval_T *argvars;
11183 typval_T *rettv;
11184{
11185 getwinvar(argvars, rettv, 1);
11186}
11187
11188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189 * "getwinposx()" function
11190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011192f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011193 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011196 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197#ifdef FEAT_GUI
11198 if (gui.in_use)
11199 {
11200 int x, y;
11201
11202 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011203 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 }
11205#endif
11206}
11207
11208/*
11209 * "getwinposy()" function
11210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011212f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011213 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011214 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011216 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217#ifdef FEAT_GUI
11218 if (gui.in_use)
11219 {
11220 int x, y;
11221
11222 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011223 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 }
11225#endif
11226}
11227
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011228/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011229 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011230 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011231 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011232find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011233 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011234 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011235{
11236#ifdef FEAT_WINDOWS
11237 win_T *wp;
11238#endif
11239 int nr;
11240
11241 nr = get_tv_number_chk(vp, NULL);
11242
11243#ifdef FEAT_WINDOWS
11244 if (nr < 0)
11245 return NULL;
11246 if (nr == 0)
11247 return curwin;
11248
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011249 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11250 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011251 if (--nr <= 0)
11252 break;
11253 return wp;
11254#else
11255 if (nr == 0 || nr == 1)
11256 return curwin;
11257 return NULL;
11258#endif
11259}
11260
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261/*
11262 * "getwinvar()" function
11263 */
11264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011265f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011266 typval_T *argvars;
11267 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011269 getwinvar(argvars, rettv, 0);
11270}
11271
11272/*
11273 * getwinvar() and gettabwinvar()
11274 */
11275 static void
11276getwinvar(argvars, rettv, off)
11277 typval_T *argvars;
11278 typval_T *rettv;
11279 int off; /* 1 for gettabwinvar() */
11280{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 win_T *win, *oldcurwin;
11282 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011283 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011284 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011286#ifdef FEAT_WINDOWS
11287 if (off == 1)
11288 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11289 else
11290 tp = curtab;
11291#endif
11292 win = find_win_by_nr(&argvars[off], tp);
11293 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011294 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296 rettv->v_type = VAR_STRING;
11297 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298
11299 if (win != NULL && varname != NULL)
11300 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011301 /* Set curwin to be our win, temporarily. Also set curbuf, so
11302 * that we can get buffer-local options. */
11303 oldcurwin = curwin;
11304 curwin = win;
11305 curbuf = win->w_buffer;
11306
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309 else
11310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011311 if (*varname == NUL)
11312 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11313 * scope prefix before the NUL byte is required by
11314 * find_var_in_ht(). */
11315 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011317 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011319 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011321
11322 /* restore previous notion of curwin */
11323 curwin = oldcurwin;
11324 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 }
11326
11327 --emsg_off;
11328}
11329
11330/*
11331 * "glob()" function
11332 */
11333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011334f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011335 typval_T *argvars;
11336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011338 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011340 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011342 /* When the optional second argument is non-zero, don't remove matches
11343 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11344 if (argvars[1].v_type != VAR_UNKNOWN
11345 && get_tv_number_chk(&argvars[1], &error))
11346 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011348 if (!error)
11349 {
11350 ExpandInit(&xpc);
11351 xpc.xp_context = EXPAND_FILES;
11352 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11353 NULL, flags, WILD_ALL);
11354 }
11355 else
11356 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357}
11358
11359/*
11360 * "globpath()" function
11361 */
11362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011363f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011364 typval_T *argvars;
11365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011367 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011369 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011370 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011372 /* When the optional second argument is non-zero, don't remove matches
11373 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11374 if (argvars[2].v_type != VAR_UNKNOWN
11375 && get_tv_number_chk(&argvars[2], &error))
11376 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011378 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011379 rettv->vval.v_string = NULL;
11380 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011381 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11382 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383}
11384
11385/*
11386 * "has()" function
11387 */
11388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011390 typval_T *argvars;
11391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392{
11393 int i;
11394 char_u *name;
11395 int n = FALSE;
11396 static char *(has_list[]) =
11397 {
11398#ifdef AMIGA
11399 "amiga",
11400# ifdef FEAT_ARP
11401 "arp",
11402# endif
11403#endif
11404#ifdef __BEOS__
11405 "beos",
11406#endif
11407#ifdef MSDOS
11408# ifdef DJGPP
11409 "dos32",
11410# else
11411 "dos16",
11412# endif
11413#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011414#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 "mac",
11416#endif
11417#if defined(MACOS_X_UNIX)
11418 "macunix",
11419#endif
11420#ifdef OS2
11421 "os2",
11422#endif
11423#ifdef __QNX__
11424 "qnx",
11425#endif
11426#ifdef RISCOS
11427 "riscos",
11428#endif
11429#ifdef UNIX
11430 "unix",
11431#endif
11432#ifdef VMS
11433 "vms",
11434#endif
11435#ifdef WIN16
11436 "win16",
11437#endif
11438#ifdef WIN32
11439 "win32",
11440#endif
11441#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11442 "win32unix",
11443#endif
11444#ifdef WIN64
11445 "win64",
11446#endif
11447#ifdef EBCDIC
11448 "ebcdic",
11449#endif
11450#ifndef CASE_INSENSITIVE_FILENAME
11451 "fname_case",
11452#endif
11453#ifdef FEAT_ARABIC
11454 "arabic",
11455#endif
11456#ifdef FEAT_AUTOCMD
11457 "autocmd",
11458#endif
11459#ifdef FEAT_BEVAL
11460 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011461# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11462 "balloon_multiline",
11463# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464#endif
11465#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11466 "builtin_terms",
11467# ifdef ALL_BUILTIN_TCAPS
11468 "all_builtin_terms",
11469# endif
11470#endif
11471#ifdef FEAT_BYTEOFF
11472 "byte_offset",
11473#endif
11474#ifdef FEAT_CINDENT
11475 "cindent",
11476#endif
11477#ifdef FEAT_CLIENTSERVER
11478 "clientserver",
11479#endif
11480#ifdef FEAT_CLIPBOARD
11481 "clipboard",
11482#endif
11483#ifdef FEAT_CMDL_COMPL
11484 "cmdline_compl",
11485#endif
11486#ifdef FEAT_CMDHIST
11487 "cmdline_hist",
11488#endif
11489#ifdef FEAT_COMMENTS
11490 "comments",
11491#endif
11492#ifdef FEAT_CRYPT
11493 "cryptv",
11494#endif
11495#ifdef FEAT_CSCOPE
11496 "cscope",
11497#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011498#ifdef CURSOR_SHAPE
11499 "cursorshape",
11500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501#ifdef DEBUG
11502 "debug",
11503#endif
11504#ifdef FEAT_CON_DIALOG
11505 "dialog_con",
11506#endif
11507#ifdef FEAT_GUI_DIALOG
11508 "dialog_gui",
11509#endif
11510#ifdef FEAT_DIFF
11511 "diff",
11512#endif
11513#ifdef FEAT_DIGRAPHS
11514 "digraphs",
11515#endif
11516#ifdef FEAT_DND
11517 "dnd",
11518#endif
11519#ifdef FEAT_EMACS_TAGS
11520 "emacs_tags",
11521#endif
11522 "eval", /* always present, of course! */
11523#ifdef FEAT_EX_EXTRA
11524 "ex_extra",
11525#endif
11526#ifdef FEAT_SEARCH_EXTRA
11527 "extra_search",
11528#endif
11529#ifdef FEAT_FKMAP
11530 "farsi",
11531#endif
11532#ifdef FEAT_SEARCHPATH
11533 "file_in_path",
11534#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011535#if defined(UNIX) && !defined(USE_SYSTEM)
11536 "filterpipe",
11537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538#ifdef FEAT_FIND_ID
11539 "find_in_path",
11540#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011541#ifdef FEAT_FLOAT
11542 "float",
11543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544#ifdef FEAT_FOLDING
11545 "folding",
11546#endif
11547#ifdef FEAT_FOOTER
11548 "footer",
11549#endif
11550#if !defined(USE_SYSTEM) && defined(UNIX)
11551 "fork",
11552#endif
11553#ifdef FEAT_GETTEXT
11554 "gettext",
11555#endif
11556#ifdef FEAT_GUI
11557 "gui",
11558#endif
11559#ifdef FEAT_GUI_ATHENA
11560# ifdef FEAT_GUI_NEXTAW
11561 "gui_neXtaw",
11562# else
11563 "gui_athena",
11564# endif
11565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566#ifdef FEAT_GUI_GTK
11567 "gui_gtk",
11568# ifdef HAVE_GTK2
11569 "gui_gtk2",
11570# endif
11571#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011572#ifdef FEAT_GUI_GNOME
11573 "gui_gnome",
11574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575#ifdef FEAT_GUI_MAC
11576 "gui_mac",
11577#endif
11578#ifdef FEAT_GUI_MOTIF
11579 "gui_motif",
11580#endif
11581#ifdef FEAT_GUI_PHOTON
11582 "gui_photon",
11583#endif
11584#ifdef FEAT_GUI_W16
11585 "gui_win16",
11586#endif
11587#ifdef FEAT_GUI_W32
11588 "gui_win32",
11589#endif
11590#ifdef FEAT_HANGULIN
11591 "hangul_input",
11592#endif
11593#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11594 "iconv",
11595#endif
11596#ifdef FEAT_INS_EXPAND
11597 "insert_expand",
11598#endif
11599#ifdef FEAT_JUMPLIST
11600 "jumplist",
11601#endif
11602#ifdef FEAT_KEYMAP
11603 "keymap",
11604#endif
11605#ifdef FEAT_LANGMAP
11606 "langmap",
11607#endif
11608#ifdef FEAT_LIBCALL
11609 "libcall",
11610#endif
11611#ifdef FEAT_LINEBREAK
11612 "linebreak",
11613#endif
11614#ifdef FEAT_LISP
11615 "lispindent",
11616#endif
11617#ifdef FEAT_LISTCMDS
11618 "listcmds",
11619#endif
11620#ifdef FEAT_LOCALMAP
11621 "localmap",
11622#endif
11623#ifdef FEAT_MENU
11624 "menu",
11625#endif
11626#ifdef FEAT_SESSION
11627 "mksession",
11628#endif
11629#ifdef FEAT_MODIFY_FNAME
11630 "modify_fname",
11631#endif
11632#ifdef FEAT_MOUSE
11633 "mouse",
11634#endif
11635#ifdef FEAT_MOUSESHAPE
11636 "mouseshape",
11637#endif
11638#if defined(UNIX) || defined(VMS)
11639# ifdef FEAT_MOUSE_DEC
11640 "mouse_dec",
11641# endif
11642# ifdef FEAT_MOUSE_GPM
11643 "mouse_gpm",
11644# endif
11645# ifdef FEAT_MOUSE_JSB
11646 "mouse_jsbterm",
11647# endif
11648# ifdef FEAT_MOUSE_NET
11649 "mouse_netterm",
11650# endif
11651# ifdef FEAT_MOUSE_PTERM
11652 "mouse_pterm",
11653# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011654# ifdef FEAT_SYSMOUSE
11655 "mouse_sysmouse",
11656# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657# ifdef FEAT_MOUSE_XTERM
11658 "mouse_xterm",
11659# endif
11660#endif
11661#ifdef FEAT_MBYTE
11662 "multi_byte",
11663#endif
11664#ifdef FEAT_MBYTE_IME
11665 "multi_byte_ime",
11666#endif
11667#ifdef FEAT_MULTI_LANG
11668 "multi_lang",
11669#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011670#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011671#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011672 "mzscheme",
11673#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675#ifdef FEAT_OLE
11676 "ole",
11677#endif
11678#ifdef FEAT_OSFILETYPE
11679 "osfiletype",
11680#endif
11681#ifdef FEAT_PATH_EXTRA
11682 "path_extra",
11683#endif
11684#ifdef FEAT_PERL
11685#ifndef DYNAMIC_PERL
11686 "perl",
11687#endif
11688#endif
11689#ifdef FEAT_PYTHON
11690#ifndef DYNAMIC_PYTHON
11691 "python",
11692#endif
11693#endif
11694#ifdef FEAT_POSTSCRIPT
11695 "postscript",
11696#endif
11697#ifdef FEAT_PRINTER
11698 "printer",
11699#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011700#ifdef FEAT_PROFILE
11701 "profile",
11702#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011703#ifdef FEAT_RELTIME
11704 "reltime",
11705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706#ifdef FEAT_QUICKFIX
11707 "quickfix",
11708#endif
11709#ifdef FEAT_RIGHTLEFT
11710 "rightleft",
11711#endif
11712#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11713 "ruby",
11714#endif
11715#ifdef FEAT_SCROLLBIND
11716 "scrollbind",
11717#endif
11718#ifdef FEAT_CMDL_INFO
11719 "showcmd",
11720 "cmdline_info",
11721#endif
11722#ifdef FEAT_SIGNS
11723 "signs",
11724#endif
11725#ifdef FEAT_SMARTINDENT
11726 "smartindent",
11727#endif
11728#ifdef FEAT_SNIFF
11729 "sniff",
11730#endif
11731#ifdef FEAT_STL_OPT
11732 "statusline",
11733#endif
11734#ifdef FEAT_SUN_WORKSHOP
11735 "sun_workshop",
11736#endif
11737#ifdef FEAT_NETBEANS_INTG
11738 "netbeans_intg",
11739#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011740#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011741 "spell",
11742#endif
11743#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744 "syntax",
11745#endif
11746#if defined(USE_SYSTEM) || !defined(UNIX)
11747 "system",
11748#endif
11749#ifdef FEAT_TAG_BINS
11750 "tag_binary",
11751#endif
11752#ifdef FEAT_TAG_OLDSTATIC
11753 "tag_old_static",
11754#endif
11755#ifdef FEAT_TAG_ANYWHITE
11756 "tag_any_white",
11757#endif
11758#ifdef FEAT_TCL
11759# ifndef DYNAMIC_TCL
11760 "tcl",
11761# endif
11762#endif
11763#ifdef TERMINFO
11764 "terminfo",
11765#endif
11766#ifdef FEAT_TERMRESPONSE
11767 "termresponse",
11768#endif
11769#ifdef FEAT_TEXTOBJ
11770 "textobjects",
11771#endif
11772#ifdef HAVE_TGETENT
11773 "tgetent",
11774#endif
11775#ifdef FEAT_TITLE
11776 "title",
11777#endif
11778#ifdef FEAT_TOOLBAR
11779 "toolbar",
11780#endif
11781#ifdef FEAT_USR_CMDS
11782 "user-commands", /* was accidentally included in 5.4 */
11783 "user_commands",
11784#endif
11785#ifdef FEAT_VIMINFO
11786 "viminfo",
11787#endif
11788#ifdef FEAT_VERTSPLIT
11789 "vertsplit",
11790#endif
11791#ifdef FEAT_VIRTUALEDIT
11792 "virtualedit",
11793#endif
11794#ifdef FEAT_VISUAL
11795 "visual",
11796#endif
11797#ifdef FEAT_VISUALEXTRA
11798 "visualextra",
11799#endif
11800#ifdef FEAT_VREPLACE
11801 "vreplace",
11802#endif
11803#ifdef FEAT_WILDIGN
11804 "wildignore",
11805#endif
11806#ifdef FEAT_WILDMENU
11807 "wildmenu",
11808#endif
11809#ifdef FEAT_WINDOWS
11810 "windows",
11811#endif
11812#ifdef FEAT_WAK
11813 "winaltkeys",
11814#endif
11815#ifdef FEAT_WRITEBACKUP
11816 "writebackup",
11817#endif
11818#ifdef FEAT_XIM
11819 "xim",
11820#endif
11821#ifdef FEAT_XFONTSET
11822 "xfontset",
11823#endif
11824#ifdef USE_XSMP
11825 "xsmp",
11826#endif
11827#ifdef USE_XSMP_INTERACT
11828 "xsmp_interact",
11829#endif
11830#ifdef FEAT_XCLIPBOARD
11831 "xterm_clipboard",
11832#endif
11833#ifdef FEAT_XTERM_SAVE
11834 "xterm_save",
11835#endif
11836#if defined(UNIX) && defined(FEAT_X11)
11837 "X11",
11838#endif
11839 NULL
11840 };
11841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011842 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 for (i = 0; has_list[i] != NULL; ++i)
11844 if (STRICMP(name, has_list[i]) == 0)
11845 {
11846 n = TRUE;
11847 break;
11848 }
11849
11850 if (n == FALSE)
11851 {
11852 if (STRNICMP(name, "patch", 5) == 0)
11853 n = has_patch(atoi((char *)name + 5));
11854 else if (STRICMP(name, "vim_starting") == 0)
11855 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000011856#ifdef FEAT_MBYTE
11857 else if (STRICMP(name, "multi_byte_encoding") == 0)
11858 n = has_mbyte;
11859#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000011860#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11861 else if (STRICMP(name, "balloon_multiline") == 0)
11862 n = multiline_balloon_available();
11863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864#ifdef DYNAMIC_TCL
11865 else if (STRICMP(name, "tcl") == 0)
11866 n = tcl_enabled(FALSE);
11867#endif
11868#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11869 else if (STRICMP(name, "iconv") == 0)
11870 n = iconv_enabled(FALSE);
11871#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011872#ifdef DYNAMIC_MZSCHEME
11873 else if (STRICMP(name, "mzscheme") == 0)
11874 n = mzscheme_enabled(FALSE);
11875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876#ifdef DYNAMIC_RUBY
11877 else if (STRICMP(name, "ruby") == 0)
11878 n = ruby_enabled(FALSE);
11879#endif
11880#ifdef DYNAMIC_PYTHON
11881 else if (STRICMP(name, "python") == 0)
11882 n = python_enabled(FALSE);
11883#endif
11884#ifdef DYNAMIC_PERL
11885 else if (STRICMP(name, "perl") == 0)
11886 n = perl_enabled(FALSE);
11887#endif
11888#ifdef FEAT_GUI
11889 else if (STRICMP(name, "gui_running") == 0)
11890 n = (gui.in_use || gui.starting);
11891# ifdef FEAT_GUI_W32
11892 else if (STRICMP(name, "gui_win32s") == 0)
11893 n = gui_is_win32s();
11894# endif
11895# ifdef FEAT_BROWSE
11896 else if (STRICMP(name, "browse") == 0)
11897 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11898# endif
11899#endif
11900#ifdef FEAT_SYN_HL
11901 else if (STRICMP(name, "syntax_items") == 0)
11902 n = syntax_present(curbuf);
11903#endif
11904#if defined(WIN3264)
11905 else if (STRICMP(name, "win95") == 0)
11906 n = mch_windows95();
11907#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011908#ifdef FEAT_NETBEANS_INTG
11909 else if (STRICMP(name, "netbeans_enabled") == 0)
11910 n = usingNetbeans;
11911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912 }
11913
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011914 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915}
11916
11917/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011918 * "has_key()" function
11919 */
11920 static void
11921f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011922 typval_T *argvars;
11923 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011924{
Bram Moolenaare9a41262005-01-15 22:18:47 +000011925 if (argvars[0].v_type != VAR_DICT)
11926 {
11927 EMSG(_(e_dictreq));
11928 return;
11929 }
11930 if (argvars[0].vval.v_dict == NULL)
11931 return;
11932
11933 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011934 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011935}
11936
11937/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011938 * "haslocaldir()" function
11939 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011940 static void
11941f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011942 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011943 typval_T *rettv;
11944{
11945 rettv->vval.v_number = (curwin->w_localdir != NULL);
11946}
11947
11948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949 * "hasmapto()" function
11950 */
11951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011952f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011953 typval_T *argvars;
11954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955{
11956 char_u *name;
11957 char_u *mode;
11958 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011959 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011961 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011962 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963 mode = (char_u *)"nvo";
11964 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011965 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011966 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011967 if (argvars[2].v_type != VAR_UNKNOWN)
11968 abbr = get_tv_number(&argvars[2]);
11969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970
Bram Moolenaar2c932302006-03-18 21:42:09 +000011971 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011972 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011974 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975}
11976
11977/*
11978 * "histadd()" function
11979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011981f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011982 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984{
11985#ifdef FEAT_CMDHIST
11986 int histype;
11987 char_u *str;
11988 char_u buf[NUMBUFLEN];
11989#endif
11990
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 if (check_restricted() || check_secure())
11993 return;
11994#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011995 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11996 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 if (histype >= 0)
11998 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011999 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 if (*str != NUL)
12001 {
12002 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012003 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004 return;
12005 }
12006 }
12007#endif
12008}
12009
12010/*
12011 * "histdel()" function
12012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012014f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012015 typval_T *argvars UNUSED;
12016 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017{
12018#ifdef FEAT_CMDHIST
12019 int n;
12020 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012021 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012023 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12024 if (str == NULL)
12025 n = 0;
12026 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012028 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012029 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012031 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012032 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 else
12034 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012035 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012036 get_tv_string_buf(&argvars[1], buf));
12037 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038#endif
12039}
12040
12041/*
12042 * "histget()" function
12043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012045f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012046 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048{
12049#ifdef FEAT_CMDHIST
12050 int type;
12051 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012052 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012054 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12055 if (str == NULL)
12056 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012058 {
12059 type = get_histtype(str);
12060 if (argvars[1].v_type == VAR_UNKNOWN)
12061 idx = get_history_idx(type);
12062 else
12063 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12064 /* -1 on type error */
12065 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012068 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012070 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071}
12072
12073/*
12074 * "histnr()" function
12075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012077f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012078 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080{
12081 int i;
12082
12083#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012084 char_u *history = get_tv_string_chk(&argvars[0]);
12085
12086 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087 if (i >= HIST_CMD && i < HIST_COUNT)
12088 i = get_history_idx(i);
12089 else
12090#endif
12091 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012092 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093}
12094
12095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096 * "highlightID(name)" function
12097 */
12098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012099f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012100 typval_T *argvars;
12101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012103 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104}
12105
12106/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012107 * "highlight_exists()" function
12108 */
12109 static void
12110f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012111 typval_T *argvars;
12112 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012113{
12114 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12115}
12116
12117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 * "hostname()" function
12119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012121f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012122 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124{
12125 char_u hostname[256];
12126
12127 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012128 rettv->v_type = VAR_STRING;
12129 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130}
12131
12132/*
12133 * iconv() function
12134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012136f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012137 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139{
12140#ifdef FEAT_MBYTE
12141 char_u buf1[NUMBUFLEN];
12142 char_u buf2[NUMBUFLEN];
12143 char_u *from, *to, *str;
12144 vimconv_T vimconv;
12145#endif
12146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012147 rettv->v_type = VAR_STRING;
12148 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149
12150#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012151 str = get_tv_string(&argvars[0]);
12152 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12153 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154 vimconv.vc_type = CONV_NONE;
12155 convert_setup(&vimconv, from, to);
12156
12157 /* If the encodings are equal, no conversion needed. */
12158 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012159 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012161 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162
12163 convert_setup(&vimconv, NULL, NULL);
12164 vim_free(from);
12165 vim_free(to);
12166#endif
12167}
12168
12169/*
12170 * "indent()" function
12171 */
12172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012173f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012174 typval_T *argvars;
12175 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176{
12177 linenr_T lnum;
12178
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012179 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012181 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012183 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184}
12185
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012186/*
12187 * "index()" function
12188 */
12189 static void
12190f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012191 typval_T *argvars;
12192 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012193{
Bram Moolenaar33570922005-01-25 22:26:29 +000012194 list_T *l;
12195 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012196 long idx = 0;
12197 int ic = FALSE;
12198
12199 rettv->vval.v_number = -1;
12200 if (argvars[0].v_type != VAR_LIST)
12201 {
12202 EMSG(_(e_listreq));
12203 return;
12204 }
12205 l = argvars[0].vval.v_list;
12206 if (l != NULL)
12207 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012208 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012209 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012211 int error = FALSE;
12212
Bram Moolenaar758711c2005-02-02 23:11:38 +000012213 /* Start at specified item. Use the cached index that list_find()
12214 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012215 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012216 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012217 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012218 ic = get_tv_number_chk(&argvars[3], &error);
12219 if (error)
12220 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012221 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012222
Bram Moolenaar758711c2005-02-02 23:11:38 +000012223 for ( ; item != NULL; item = item->li_next, ++idx)
12224 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012225 {
12226 rettv->vval.v_number = idx;
12227 break;
12228 }
12229 }
12230}
12231
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232static int inputsecret_flag = 0;
12233
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012234static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12235
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012237 * This function is used by f_input() and f_inputdialog() functions. The third
12238 * argument to f_input() specifies the type of completion to use at the
12239 * prompt. The third argument to f_inputdialog() specifies the value to return
12240 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241 */
12242 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012243get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012244 typval_T *argvars;
12245 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012246 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012248 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249 char_u *p = NULL;
12250 int c;
12251 char_u buf[NUMBUFLEN];
12252 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012253 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012254 int xp_type = EXPAND_NOTHING;
12255 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012257 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012258 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259
12260#ifdef NO_CONSOLE_INPUT
12261 /* While starting up, there is no place to enter text. */
12262 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264#endif
12265
12266 cmd_silent = FALSE; /* Want to see the prompt. */
12267 if (prompt != NULL)
12268 {
12269 /* Only the part of the message after the last NL is considered as
12270 * prompt for the command line */
12271 p = vim_strrchr(prompt, '\n');
12272 if (p == NULL)
12273 p = prompt;
12274 else
12275 {
12276 ++p;
12277 c = *p;
12278 *p = NUL;
12279 msg_start();
12280 msg_clr_eos();
12281 msg_puts_attr(prompt, echo_attr);
12282 msg_didout = FALSE;
12283 msg_starthere();
12284 *p = c;
12285 }
12286 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012288 if (argvars[1].v_type != VAR_UNKNOWN)
12289 {
12290 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12291 if (defstr != NULL)
12292 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012294 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012295 {
12296 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012297 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012298 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012299
Bram Moolenaar4463f292005-09-25 22:20:24 +000012300 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012301
Bram Moolenaar4463f292005-09-25 22:20:24 +000012302 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12303 if (xp_name == NULL)
12304 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012305
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012306 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012307
Bram Moolenaar4463f292005-09-25 22:20:24 +000012308 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12309 &xp_arg) == FAIL)
12310 return;
12311 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012312 }
12313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012314 if (defstr != NULL)
12315 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012316 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12317 xp_type, xp_arg);
12318
12319 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012321 /* since the user typed this, no need to wait for return */
12322 need_wait_return = FALSE;
12323 msg_didout = FALSE;
12324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325 cmd_silent = cmd_silent_save;
12326}
12327
12328/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012329 * "input()" function
12330 * Also handles inputsecret() when inputsecret is set.
12331 */
12332 static void
12333f_input(argvars, rettv)
12334 typval_T *argvars;
12335 typval_T *rettv;
12336{
12337 get_user_input(argvars, rettv, FALSE);
12338}
12339
12340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341 * "inputdialog()" function
12342 */
12343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012344f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012345 typval_T *argvars;
12346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347{
12348#if defined(FEAT_GUI_TEXTDIALOG)
12349 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12350 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12351 {
12352 char_u *message;
12353 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012354 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012356 message = get_tv_string_chk(&argvars[0]);
12357 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012358 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012359 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360 else
12361 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012362 if (message != NULL && defstr != NULL
12363 && do_dialog(VIM_QUESTION, NULL, message,
12364 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012365 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366 else
12367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012368 if (message != NULL && defstr != NULL
12369 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012370 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012371 rettv->vval.v_string = vim_strsave(
12372 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012374 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012376 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 }
12378 else
12379#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012380 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381}
12382
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012383/*
12384 * "inputlist()" function
12385 */
12386 static void
12387f_inputlist(argvars, rettv)
12388 typval_T *argvars;
12389 typval_T *rettv;
12390{
12391 listitem_T *li;
12392 int selected;
12393 int mouse_used;
12394
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012395#ifdef NO_CONSOLE_INPUT
12396 /* While starting up, there is no place to enter text. */
12397 if (no_console_input())
12398 return;
12399#endif
12400 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12401 {
12402 EMSG2(_(e_listarg), "inputlist()");
12403 return;
12404 }
12405
12406 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012407 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012408 lines_left = Rows; /* avoid more prompt */
12409 msg_scroll = TRUE;
12410 msg_clr_eos();
12411
12412 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12413 {
12414 msg_puts(get_tv_string(&li->li_tv));
12415 msg_putchar('\n');
12416 }
12417
12418 /* Ask for choice. */
12419 selected = prompt_for_number(&mouse_used);
12420 if (mouse_used)
12421 selected -= lines_left;
12422
12423 rettv->vval.v_number = selected;
12424}
12425
12426
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12428
12429/*
12430 * "inputrestore()" function
12431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012433f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012434 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436{
12437 if (ga_userinput.ga_len > 0)
12438 {
12439 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12441 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012442 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012443 }
12444 else if (p_verbose > 1)
12445 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012446 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012447 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012448 }
12449}
12450
12451/*
12452 * "inputsave()" function
12453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012455f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012456 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012459 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460 if (ga_grow(&ga_userinput, 1) == OK)
12461 {
12462 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12463 + ga_userinput.ga_len);
12464 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012465 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466 }
12467 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012468 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469}
12470
12471/*
12472 * "inputsecret()" function
12473 */
12474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012475f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012476 typval_T *argvars;
12477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478{
12479 ++cmdline_star;
12480 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012481 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482 --cmdline_star;
12483 --inputsecret_flag;
12484}
12485
12486/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012487 * "insert()" function
12488 */
12489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012490f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012491 typval_T *argvars;
12492 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012493{
12494 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012495 listitem_T *item;
12496 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012497 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012498
12499 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012500 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012501 else if ((l = argvars[0].vval.v_list) != NULL
12502 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012503 {
12504 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012505 before = get_tv_number_chk(&argvars[2], &error);
12506 if (error)
12507 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012508
Bram Moolenaar758711c2005-02-02 23:11:38 +000012509 if (before == l->lv_len)
12510 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012511 else
12512 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012513 item = list_find(l, before);
12514 if (item == NULL)
12515 {
12516 EMSGN(_(e_listidx), before);
12517 l = NULL;
12518 }
12519 }
12520 if (l != NULL)
12521 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012522 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012523 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012524 }
12525 }
12526}
12527
12528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529 * "isdirectory()" function
12530 */
12531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012532f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012533 typval_T *argvars;
12534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012536 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537}
12538
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012539/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012540 * "islocked()" function
12541 */
12542 static void
12543f_islocked(argvars, rettv)
12544 typval_T *argvars;
12545 typval_T *rettv;
12546{
12547 lval_T lv;
12548 char_u *end;
12549 dictitem_T *di;
12550
12551 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012552 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12553 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012554 if (end != NULL && lv.ll_name != NULL)
12555 {
12556 if (*end != NUL)
12557 EMSG(_(e_trailing));
12558 else
12559 {
12560 if (lv.ll_tv == NULL)
12561 {
12562 if (check_changedtick(lv.ll_name))
12563 rettv->vval.v_number = 1; /* always locked */
12564 else
12565 {
12566 di = find_var(lv.ll_name, NULL);
12567 if (di != NULL)
12568 {
12569 /* Consider a variable locked when:
12570 * 1. the variable itself is locked
12571 * 2. the value of the variable is locked.
12572 * 3. the List or Dict value is locked.
12573 */
12574 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12575 || tv_islocked(&di->di_tv));
12576 }
12577 }
12578 }
12579 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012580 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012581 else if (lv.ll_newkey != NULL)
12582 EMSG2(_(e_dictkey), lv.ll_newkey);
12583 else if (lv.ll_list != NULL)
12584 /* List item. */
12585 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12586 else
12587 /* Dictionary item. */
12588 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12589 }
12590 }
12591
12592 clear_lval(&lv);
12593}
12594
Bram Moolenaar33570922005-01-25 22:26:29 +000012595static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012596
12597/*
12598 * Turn a dict into a list:
12599 * "what" == 0: list of keys
12600 * "what" == 1: list of values
12601 * "what" == 2: list of items
12602 */
12603 static void
12604dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012605 typval_T *argvars;
12606 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012607 int what;
12608{
Bram Moolenaar33570922005-01-25 22:26:29 +000012609 list_T *l2;
12610 dictitem_T *di;
12611 hashitem_T *hi;
12612 listitem_T *li;
12613 listitem_T *li2;
12614 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012615 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012616
Bram Moolenaar8c711452005-01-14 21:53:12 +000012617 if (argvars[0].v_type != VAR_DICT)
12618 {
12619 EMSG(_(e_dictreq));
12620 return;
12621 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012622 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012623 return;
12624
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012625 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012626 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012627
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012628 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012629 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012630 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012631 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012632 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012633 --todo;
12634 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012635
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012636 li = listitem_alloc();
12637 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012638 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012639 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012640
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012641 if (what == 0)
12642 {
12643 /* keys() */
12644 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012645 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012646 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12647 }
12648 else if (what == 1)
12649 {
12650 /* values() */
12651 copy_tv(&di->di_tv, &li->li_tv);
12652 }
12653 else
12654 {
12655 /* items() */
12656 l2 = list_alloc();
12657 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012658 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012659 li->li_tv.vval.v_list = l2;
12660 if (l2 == NULL)
12661 break;
12662 ++l2->lv_refcount;
12663
12664 li2 = listitem_alloc();
12665 if (li2 == NULL)
12666 break;
12667 list_append(l2, li2);
12668 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012669 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012670 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12671
12672 li2 = listitem_alloc();
12673 if (li2 == NULL)
12674 break;
12675 list_append(l2, li2);
12676 copy_tv(&di->di_tv, &li2->li_tv);
12677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012678 }
12679 }
12680}
12681
12682/*
12683 * "items(dict)" function
12684 */
12685 static void
12686f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012687 typval_T *argvars;
12688 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012689{
12690 dict_list(argvars, rettv, 2);
12691}
12692
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012694 * "join()" function
12695 */
12696 static void
12697f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012698 typval_T *argvars;
12699 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012700{
12701 garray_T ga;
12702 char_u *sep;
12703
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012704 if (argvars[0].v_type != VAR_LIST)
12705 {
12706 EMSG(_(e_listreq));
12707 return;
12708 }
12709 if (argvars[0].vval.v_list == NULL)
12710 return;
12711 if (argvars[1].v_type == VAR_UNKNOWN)
12712 sep = (char_u *)" ";
12713 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012714 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012715
12716 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012717
12718 if (sep != NULL)
12719 {
12720 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012721 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012722 ga_append(&ga, NUL);
12723 rettv->vval.v_string = (char_u *)ga.ga_data;
12724 }
12725 else
12726 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012727}
12728
12729/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012730 * "keys()" function
12731 */
12732 static void
12733f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012734 typval_T *argvars;
12735 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012736{
12737 dict_list(argvars, rettv, 0);
12738}
12739
12740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741 * "last_buffer_nr()" function.
12742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012744f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012745 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747{
12748 int n = 0;
12749 buf_T *buf;
12750
12751 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12752 if (n < buf->b_fnum)
12753 n = buf->b_fnum;
12754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012755 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012756}
12757
12758/*
12759 * "len()" function
12760 */
12761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012763 typval_T *argvars;
12764 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012765{
12766 switch (argvars[0].v_type)
12767 {
12768 case VAR_STRING:
12769 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770 rettv->vval.v_number = (varnumber_T)STRLEN(
12771 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012772 break;
12773 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012775 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012776 case VAR_DICT:
12777 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12778 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012779 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012780 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012781 break;
12782 }
12783}
12784
Bram Moolenaar33570922005-01-25 22:26:29 +000012785static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012786
12787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012788libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012789 typval_T *argvars;
12790 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012791 int type;
12792{
12793#ifdef FEAT_LIBCALL
12794 char_u *string_in;
12795 char_u **string_result;
12796 int nr_result;
12797#endif
12798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012799 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012800 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012801 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012802
12803 if (check_restricted() || check_secure())
12804 return;
12805
12806#ifdef FEAT_LIBCALL
12807 /* The first two args must be strings, otherwise its meaningless */
12808 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12809 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012810 string_in = NULL;
12811 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012812 string_in = argvars[2].vval.v_string;
12813 if (type == VAR_NUMBER)
12814 string_result = NULL;
12815 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012817 if (mch_libcall(argvars[0].vval.v_string,
12818 argvars[1].vval.v_string,
12819 string_in,
12820 argvars[2].vval.v_number,
12821 string_result,
12822 &nr_result) == OK
12823 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012824 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012825 }
12826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827}
12828
12829/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012830 * "libcall()" function
12831 */
12832 static void
12833f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012834 typval_T *argvars;
12835 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012836{
12837 libcall_common(argvars, rettv, VAR_STRING);
12838}
12839
12840/*
12841 * "libcallnr()" function
12842 */
12843 static void
12844f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012845 typval_T *argvars;
12846 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012847{
12848 libcall_common(argvars, rettv, VAR_NUMBER);
12849}
12850
12851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 * "line(string)" function
12853 */
12854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012855f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012856 typval_T *argvars;
12857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858{
12859 linenr_T lnum = 0;
12860 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012861 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012863 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864 if (fp != NULL)
12865 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012866 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867}
12868
12869/*
12870 * "line2byte(lnum)" function
12871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012873f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012874 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876{
12877#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012878 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879#else
12880 linenr_T lnum;
12881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012882 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012884 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012885 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012886 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12887 if (rettv->vval.v_number >= 0)
12888 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889#endif
12890}
12891
12892/*
12893 * "lispindent(lnum)" function
12894 */
12895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012896f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012897 typval_T *argvars;
12898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899{
12900#ifdef FEAT_LISP
12901 pos_T pos;
12902 linenr_T lnum;
12903
12904 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012905 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12907 {
12908 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012909 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910 curwin->w_cursor = pos;
12911 }
12912 else
12913#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012914 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915}
12916
12917/*
12918 * "localtime()" function
12919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012921f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012922 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012925 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012926}
12927
Bram Moolenaar33570922005-01-25 22:26:29 +000012928static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929
12930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012931get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012932 typval_T *argvars;
12933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934 int exact;
12935{
12936 char_u *keys;
12937 char_u *which;
12938 char_u buf[NUMBUFLEN];
12939 char_u *keys_buf = NULL;
12940 char_u *rhs;
12941 int mode;
12942 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012943 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944
12945 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946 rettv->v_type = VAR_STRING;
12947 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012949 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012950 if (*keys == NUL)
12951 return;
12952
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012953 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012955 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012956 if (argvars[2].v_type != VAR_UNKNOWN)
12957 abbr = get_tv_number(&argvars[2]);
12958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959 else
12960 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012961 if (which == NULL)
12962 return;
12963
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964 mode = get_map_mode(&which, 0);
12965
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012966 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012967 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968 vim_free(keys_buf);
12969 if (rhs != NULL)
12970 {
12971 ga_init(&ga);
12972 ga.ga_itemsize = 1;
12973 ga.ga_growsize = 40;
12974
12975 while (*rhs != NUL)
12976 ga_concat(&ga, str2special(&rhs, FALSE));
12977
12978 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012979 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012980 }
12981}
12982
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012983#ifdef FEAT_FLOAT
12984/*
12985 * "log10()" function
12986 */
12987 static void
12988f_log10(argvars, rettv)
12989 typval_T *argvars;
12990 typval_T *rettv;
12991{
12992 float_T f;
12993
12994 rettv->v_type = VAR_FLOAT;
12995 if (get_float_arg(argvars, &f) == OK)
12996 rettv->vval.v_float = log10(f);
12997 else
12998 rettv->vval.v_float = 0.0;
12999}
13000#endif
13001
Bram Moolenaar071d4272004-06-13 20:20:40 +000013002/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013003 * "map()" function
13004 */
13005 static void
13006f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013007 typval_T *argvars;
13008 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013009{
13010 filter_map(argvars, rettv, TRUE);
13011}
13012
13013/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013014 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013015 */
13016 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013017f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013018 typval_T *argvars;
13019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013021 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022}
13023
13024/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013025 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026 */
13027 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013028f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013029 typval_T *argvars;
13030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013032 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033}
13034
Bram Moolenaar33570922005-01-25 22:26:29 +000013035static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036
13037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013038find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013039 typval_T *argvars;
13040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041 int type;
13042{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013043 char_u *str = NULL;
13044 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045 char_u *pat;
13046 regmatch_T regmatch;
13047 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013048 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049 char_u *save_cpo;
13050 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013051 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013052 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013053 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013054 list_T *l = NULL;
13055 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013056 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013057 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058
13059 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13060 save_cpo = p_cpo;
13061 p_cpo = (char_u *)"";
13062
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013063 rettv->vval.v_number = -1;
13064 if (type == 3)
13065 {
13066 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013067 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013068 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013069 }
13070 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013072 rettv->v_type = VAR_STRING;
13073 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013076 if (argvars[0].v_type == VAR_LIST)
13077 {
13078 if ((l = argvars[0].vval.v_list) == NULL)
13079 goto theend;
13080 li = l->lv_first;
13081 }
13082 else
13083 expr = str = get_tv_string(&argvars[0]);
13084
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013085 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13086 if (pat == NULL)
13087 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013088
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013089 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013091 int error = FALSE;
13092
13093 start = get_tv_number_chk(&argvars[2], &error);
13094 if (error)
13095 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013096 if (l != NULL)
13097 {
13098 li = list_find(l, start);
13099 if (li == NULL)
13100 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013101 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013102 }
13103 else
13104 {
13105 if (start < 0)
13106 start = 0;
13107 if (start > (long)STRLEN(str))
13108 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013109 /* When "count" argument is there ignore matches before "start",
13110 * otherwise skip part of the string. Differs when pattern is "^"
13111 * or "\<". */
13112 if (argvars[3].v_type != VAR_UNKNOWN)
13113 startcol = start;
13114 else
13115 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013116 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013117
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013118 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013119 nth = get_tv_number_chk(&argvars[3], &error);
13120 if (error)
13121 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122 }
13123
13124 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13125 if (regmatch.regprog != NULL)
13126 {
13127 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013128
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013129 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013130 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013131 if (l != NULL)
13132 {
13133 if (li == NULL)
13134 {
13135 match = FALSE;
13136 break;
13137 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013138 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013139 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013140 if (str == NULL)
13141 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013142 }
13143
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013144 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013145
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013146 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013147 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013148 if (l == NULL && !match)
13149 break;
13150
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013151 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013152 if (l != NULL)
13153 {
13154 li = li->li_next;
13155 ++idx;
13156 }
13157 else
13158 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013159#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013160 startcol = (colnr_T)(regmatch.startp[0]
13161 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013162#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013163 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013164#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013165 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013166 }
13167
13168 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013170 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013171 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013172 int i;
13173
13174 /* return list with matched string and submatches */
13175 for (i = 0; i < NSUBEXP; ++i)
13176 {
13177 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013178 {
13179 if (list_append_string(rettv->vval.v_list,
13180 (char_u *)"", 0) == FAIL)
13181 break;
13182 }
13183 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013184 regmatch.startp[i],
13185 (int)(regmatch.endp[i] - regmatch.startp[i]))
13186 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013187 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013188 }
13189 }
13190 else if (type == 2)
13191 {
13192 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013193 if (l != NULL)
13194 copy_tv(&li->li_tv, rettv);
13195 else
13196 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013198 }
13199 else if (l != NULL)
13200 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201 else
13202 {
13203 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013204 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 (varnumber_T)(regmatch.startp[0] - str);
13206 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013209 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210 }
13211 }
13212 vim_free(regmatch.regprog);
13213 }
13214
13215theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013216 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217 p_cpo = save_cpo;
13218}
13219
13220/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013221 * "match()" function
13222 */
13223 static void
13224f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013225 typval_T *argvars;
13226 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013227{
13228 find_some_match(argvars, rettv, 1);
13229}
13230
13231/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013232 * "matchadd()" function
13233 */
13234 static void
13235f_matchadd(argvars, rettv)
13236 typval_T *argvars;
13237 typval_T *rettv;
13238{
13239#ifdef FEAT_SEARCH_EXTRA
13240 char_u buf[NUMBUFLEN];
13241 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13242 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13243 int prio = 10; /* default priority */
13244 int id = -1;
13245 int error = FALSE;
13246
13247 rettv->vval.v_number = -1;
13248
13249 if (grp == NULL || pat == NULL)
13250 return;
13251 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013252 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013253 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013254 if (argvars[3].v_type != VAR_UNKNOWN)
13255 id = get_tv_number_chk(&argvars[3], &error);
13256 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013257 if (error == TRUE)
13258 return;
13259 if (id >= 1 && id <= 3)
13260 {
13261 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13262 return;
13263 }
13264
13265 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13266#endif
13267}
13268
13269/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013270 * "matcharg()" function
13271 */
13272 static void
13273f_matcharg(argvars, rettv)
13274 typval_T *argvars;
13275 typval_T *rettv;
13276{
13277 if (rettv_list_alloc(rettv) == OK)
13278 {
13279#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013280 int id = get_tv_number(&argvars[0]);
13281 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013282
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013283 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013284 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013285 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13286 {
13287 list_append_string(rettv->vval.v_list,
13288 syn_id2name(m->hlg_id), -1);
13289 list_append_string(rettv->vval.v_list, m->pattern, -1);
13290 }
13291 else
13292 {
13293 list_append_string(rettv->vval.v_list, NUL, -1);
13294 list_append_string(rettv->vval.v_list, NUL, -1);
13295 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013296 }
13297#endif
13298 }
13299}
13300
13301/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013302 * "matchdelete()" function
13303 */
13304 static void
13305f_matchdelete(argvars, rettv)
13306 typval_T *argvars;
13307 typval_T *rettv;
13308{
13309#ifdef FEAT_SEARCH_EXTRA
13310 rettv->vval.v_number = match_delete(curwin,
13311 (int)get_tv_number(&argvars[0]), TRUE);
13312#endif
13313}
13314
13315/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013316 * "matchend()" function
13317 */
13318 static void
13319f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013320 typval_T *argvars;
13321 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013322{
13323 find_some_match(argvars, rettv, 0);
13324}
13325
13326/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013327 * "matchlist()" function
13328 */
13329 static void
13330f_matchlist(argvars, rettv)
13331 typval_T *argvars;
13332 typval_T *rettv;
13333{
13334 find_some_match(argvars, rettv, 3);
13335}
13336
13337/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013338 * "matchstr()" function
13339 */
13340 static void
13341f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013342 typval_T *argvars;
13343 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013344{
13345 find_some_match(argvars, rettv, 2);
13346}
13347
Bram Moolenaar33570922005-01-25 22:26:29 +000013348static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013349
13350 static void
13351max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013352 typval_T *argvars;
13353 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013354 int domax;
13355{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013356 long n = 0;
13357 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013358 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013359
13360 if (argvars[0].v_type == VAR_LIST)
13361 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013362 list_T *l;
13363 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013364
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013365 l = argvars[0].vval.v_list;
13366 if (l != NULL)
13367 {
13368 li = l->lv_first;
13369 if (li != NULL)
13370 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013371 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013372 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013373 {
13374 li = li->li_next;
13375 if (li == NULL)
13376 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013377 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013378 if (domax ? i > n : i < n)
13379 n = i;
13380 }
13381 }
13382 }
13383 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013384 else if (argvars[0].v_type == VAR_DICT)
13385 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013386 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013387 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013388 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013389 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013390
13391 d = argvars[0].vval.v_dict;
13392 if (d != NULL)
13393 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013394 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013395 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013396 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013397 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013398 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013399 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013400 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013401 if (first)
13402 {
13403 n = i;
13404 first = FALSE;
13405 }
13406 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013407 n = i;
13408 }
13409 }
13410 }
13411 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013412 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013413 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013414 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013415}
13416
13417/*
13418 * "max()" function
13419 */
13420 static void
13421f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013422 typval_T *argvars;
13423 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013424{
13425 max_min(argvars, rettv, TRUE);
13426}
13427
13428/*
13429 * "min()" function
13430 */
13431 static void
13432f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013433 typval_T *argvars;
13434 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013435{
13436 max_min(argvars, rettv, FALSE);
13437}
13438
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013439static int mkdir_recurse __ARGS((char_u *dir, int prot));
13440
13441/*
13442 * Create the directory in which "dir" is located, and higher levels when
13443 * needed.
13444 */
13445 static int
13446mkdir_recurse(dir, prot)
13447 char_u *dir;
13448 int prot;
13449{
13450 char_u *p;
13451 char_u *updir;
13452 int r = FAIL;
13453
13454 /* Get end of directory name in "dir".
13455 * We're done when it's "/" or "c:/". */
13456 p = gettail_sep(dir);
13457 if (p <= get_past_head(dir))
13458 return OK;
13459
13460 /* If the directory exists we're done. Otherwise: create it.*/
13461 updir = vim_strnsave(dir, (int)(p - dir));
13462 if (updir == NULL)
13463 return FAIL;
13464 if (mch_isdir(updir))
13465 r = OK;
13466 else if (mkdir_recurse(updir, prot) == OK)
13467 r = vim_mkdir_emsg(updir, prot);
13468 vim_free(updir);
13469 return r;
13470}
13471
13472#ifdef vim_mkdir
13473/*
13474 * "mkdir()" function
13475 */
13476 static void
13477f_mkdir(argvars, rettv)
13478 typval_T *argvars;
13479 typval_T *rettv;
13480{
13481 char_u *dir;
13482 char_u buf[NUMBUFLEN];
13483 int prot = 0755;
13484
13485 rettv->vval.v_number = FAIL;
13486 if (check_restricted() || check_secure())
13487 return;
13488
13489 dir = get_tv_string_buf(&argvars[0], buf);
13490 if (argvars[1].v_type != VAR_UNKNOWN)
13491 {
13492 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013493 prot = get_tv_number_chk(&argvars[2], NULL);
13494 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013495 mkdir_recurse(dir, prot);
13496 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013497 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013498}
13499#endif
13500
Bram Moolenaar0d660222005-01-07 21:51:51 +000013501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502 * "mode()" function
13503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013505f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013506 typval_T *argvars;
13507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013508{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013509 char_u buf[3];
13510
13511 buf[1] = NUL;
13512 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513
13514#ifdef FEAT_VISUAL
13515 if (VIsual_active)
13516 {
13517 if (VIsual_select)
13518 buf[0] = VIsual_mode + 's' - 'v';
13519 else
13520 buf[0] = VIsual_mode;
13521 }
13522 else
13523#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013524 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13525 || State == CONFIRM)
13526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013528 if (State == ASKMORE)
13529 buf[1] = 'm';
13530 else if (State == CONFIRM)
13531 buf[1] = '?';
13532 }
13533 else if (State == EXTERNCMD)
13534 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535 else if (State & INSERT)
13536 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013537#ifdef FEAT_VREPLACE
13538 if (State & VREPLACE_FLAG)
13539 {
13540 buf[0] = 'R';
13541 buf[1] = 'v';
13542 }
13543 else
13544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545 if (State & REPLACE_FLAG)
13546 buf[0] = 'R';
13547 else
13548 buf[0] = 'i';
13549 }
13550 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013553 if (exmode_active)
13554 buf[1] = 'v';
13555 }
13556 else if (exmode_active)
13557 {
13558 buf[0] = 'c';
13559 buf[1] = 'e';
13560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013562 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013564 if (finish_op)
13565 buf[1] = 'o';
13566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013568 /* Clear out the minor mode when the argument is not a non-zero number or
13569 * non-empty string. */
13570 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013571 buf[1] = NUL;
13572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013573 rettv->vval.v_string = vim_strsave(buf);
13574 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575}
13576
13577/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013578 * "nextnonblank()" function
13579 */
13580 static void
13581f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013582 typval_T *argvars;
13583 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013584{
13585 linenr_T lnum;
13586
13587 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13588 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013589 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013590 {
13591 lnum = 0;
13592 break;
13593 }
13594 if (*skipwhite(ml_get(lnum)) != NUL)
13595 break;
13596 }
13597 rettv->vval.v_number = lnum;
13598}
13599
13600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601 * "nr2char()" function
13602 */
13603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013604f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013605 typval_T *argvars;
13606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607{
13608 char_u buf[NUMBUFLEN];
13609
13610#ifdef FEAT_MBYTE
13611 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013612 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613 else
13614#endif
13615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013616 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617 buf[1] = NUL;
13618 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619 rettv->v_type = VAR_STRING;
13620 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013621}
13622
13623/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013624 * "pathshorten()" function
13625 */
13626 static void
13627f_pathshorten(argvars, rettv)
13628 typval_T *argvars;
13629 typval_T *rettv;
13630{
13631 char_u *p;
13632
13633 rettv->v_type = VAR_STRING;
13634 p = get_tv_string_chk(&argvars[0]);
13635 if (p == NULL)
13636 rettv->vval.v_string = NULL;
13637 else
13638 {
13639 p = vim_strsave(p);
13640 rettv->vval.v_string = p;
13641 if (p != NULL)
13642 shorten_dir(p);
13643 }
13644}
13645
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013646#ifdef FEAT_FLOAT
13647/*
13648 * "pow()" function
13649 */
13650 static void
13651f_pow(argvars, rettv)
13652 typval_T *argvars;
13653 typval_T *rettv;
13654{
13655 float_T fx, fy;
13656
13657 rettv->v_type = VAR_FLOAT;
13658 if (get_float_arg(argvars, &fx) == OK
13659 && get_float_arg(&argvars[1], &fy) == OK)
13660 rettv->vval.v_float = pow(fx, fy);
13661 else
13662 rettv->vval.v_float = 0.0;
13663}
13664#endif
13665
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013666/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013667 * "prevnonblank()" function
13668 */
13669 static void
13670f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013671 typval_T *argvars;
13672 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013673{
13674 linenr_T lnum;
13675
13676 lnum = get_tv_lnum(argvars);
13677 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13678 lnum = 0;
13679 else
13680 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13681 --lnum;
13682 rettv->vval.v_number = lnum;
13683}
13684
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013685#ifdef HAVE_STDARG_H
13686/* This dummy va_list is here because:
13687 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13688 * - locally in the function results in a "used before set" warning
13689 * - using va_start() to initialize it gives "function with fixed args" error */
13690static va_list ap;
13691#endif
13692
Bram Moolenaar8c711452005-01-14 21:53:12 +000013693/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013694 * "printf()" function
13695 */
13696 static void
13697f_printf(argvars, rettv)
13698 typval_T *argvars;
13699 typval_T *rettv;
13700{
13701 rettv->v_type = VAR_STRING;
13702 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013703#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013704 {
13705 char_u buf[NUMBUFLEN];
13706 int len;
13707 char_u *s;
13708 int saved_did_emsg = did_emsg;
13709 char *fmt;
13710
13711 /* Get the required length, allocate the buffer and do it for real. */
13712 did_emsg = FALSE;
13713 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013714 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013715 if (!did_emsg)
13716 {
13717 s = alloc(len + 1);
13718 if (s != NULL)
13719 {
13720 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013721 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013722 }
13723 }
13724 did_emsg |= saved_did_emsg;
13725 }
13726#endif
13727}
13728
13729/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013730 * "pumvisible()" function
13731 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013732 static void
13733f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013734 typval_T *argvars UNUSED;
13735 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013736{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013737#ifdef FEAT_INS_EXPAND
13738 if (pum_visible())
13739 rettv->vval.v_number = 1;
13740#endif
13741}
13742
13743/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013744 * "range()" function
13745 */
13746 static void
13747f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013748 typval_T *argvars;
13749 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013750{
13751 long start;
13752 long end;
13753 long stride = 1;
13754 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013755 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013756
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013757 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013758 if (argvars[1].v_type == VAR_UNKNOWN)
13759 {
13760 end = start - 1;
13761 start = 0;
13762 }
13763 else
13764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013765 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013766 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013767 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013768 }
13769
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013770 if (error)
13771 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013772 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013773 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013774 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013775 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013776 else
13777 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013778 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013779 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013780 if (list_append_number(rettv->vval.v_list,
13781 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013782 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013783 }
13784}
13785
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013786/*
13787 * "readfile()" function
13788 */
13789 static void
13790f_readfile(argvars, rettv)
13791 typval_T *argvars;
13792 typval_T *rettv;
13793{
13794 int binary = FALSE;
13795 char_u *fname;
13796 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013797 listitem_T *li;
13798#define FREAD_SIZE 200 /* optimized for text lines */
13799 char_u buf[FREAD_SIZE];
13800 int readlen; /* size of last fread() */
13801 int buflen; /* nr of valid chars in buf[] */
13802 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13803 int tolist; /* first byte in buf[] still to be put in list */
13804 int chop; /* how many CR to chop off */
13805 char_u *prev = NULL; /* previously read bytes, if any */
13806 int prevlen = 0; /* length of "prev" if not NULL */
13807 char_u *s;
13808 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013809 long maxline = MAXLNUM;
13810 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013811
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013812 if (argvars[1].v_type != VAR_UNKNOWN)
13813 {
13814 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13815 binary = TRUE;
13816 if (argvars[2].v_type != VAR_UNKNOWN)
13817 maxline = get_tv_number(&argvars[2]);
13818 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013819
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013820 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013821 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013822
13823 /* Always open the file in binary mode, library functions have a mind of
13824 * their own about CR-LF conversion. */
13825 fname = get_tv_string(&argvars[0]);
13826 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13827 {
13828 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13829 return;
13830 }
13831
13832 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013833 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013834 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013835 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013836 buflen = filtd + readlen;
13837 tolist = 0;
13838 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13839 {
13840 if (buf[filtd] == '\n' || readlen <= 0)
13841 {
13842 /* Only when in binary mode add an empty list item when the
13843 * last line ends in a '\n'. */
13844 if (!binary && readlen == 0 && filtd == 0)
13845 break;
13846
13847 /* Found end-of-line or end-of-file: add a text line to the
13848 * list. */
13849 chop = 0;
13850 if (!binary)
13851 while (filtd - chop - 1 >= tolist
13852 && buf[filtd - chop - 1] == '\r')
13853 ++chop;
13854 len = filtd - tolist - chop;
13855 if (prev == NULL)
13856 s = vim_strnsave(buf + tolist, len);
13857 else
13858 {
13859 s = alloc((unsigned)(prevlen + len + 1));
13860 if (s != NULL)
13861 {
13862 mch_memmove(s, prev, prevlen);
13863 vim_free(prev);
13864 prev = NULL;
13865 mch_memmove(s + prevlen, buf + tolist, len);
13866 s[prevlen + len] = NUL;
13867 }
13868 }
13869 tolist = filtd + 1;
13870
13871 li = listitem_alloc();
13872 if (li == NULL)
13873 {
13874 vim_free(s);
13875 break;
13876 }
13877 li->li_tv.v_type = VAR_STRING;
13878 li->li_tv.v_lock = 0;
13879 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013880 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013881
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013882 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013883 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013884 if (readlen <= 0)
13885 break;
13886 }
13887 else if (buf[filtd] == NUL)
13888 buf[filtd] = '\n';
13889 }
13890 if (readlen <= 0)
13891 break;
13892
13893 if (tolist == 0)
13894 {
13895 /* "buf" is full, need to move text to an allocated buffer */
13896 if (prev == NULL)
13897 {
13898 prev = vim_strnsave(buf, buflen);
13899 prevlen = buflen;
13900 }
13901 else
13902 {
13903 s = alloc((unsigned)(prevlen + buflen));
13904 if (s != NULL)
13905 {
13906 mch_memmove(s, prev, prevlen);
13907 mch_memmove(s + prevlen, buf, buflen);
13908 vim_free(prev);
13909 prev = s;
13910 prevlen += buflen;
13911 }
13912 }
13913 filtd = 0;
13914 }
13915 else
13916 {
13917 mch_memmove(buf, buf + tolist, buflen - tolist);
13918 filtd -= tolist;
13919 }
13920 }
13921
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013922 /*
13923 * For a negative line count use only the lines at the end of the file,
13924 * free the rest.
13925 */
13926 if (maxline < 0)
13927 while (cnt > -maxline)
13928 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013929 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013930 --cnt;
13931 }
13932
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013933 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013934 fclose(fd);
13935}
13936
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013937#if defined(FEAT_RELTIME)
13938static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13939
13940/*
13941 * Convert a List to proftime_T.
13942 * Return FAIL when there is something wrong.
13943 */
13944 static int
13945list2proftime(arg, tm)
13946 typval_T *arg;
13947 proftime_T *tm;
13948{
13949 long n1, n2;
13950 int error = FALSE;
13951
13952 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13953 || arg->vval.v_list->lv_len != 2)
13954 return FAIL;
13955 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13956 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13957# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013958 tm->HighPart = n1;
13959 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013960# else
13961 tm->tv_sec = n1;
13962 tm->tv_usec = n2;
13963# endif
13964 return error ? FAIL : OK;
13965}
13966#endif /* FEAT_RELTIME */
13967
13968/*
13969 * "reltime()" function
13970 */
13971 static void
13972f_reltime(argvars, rettv)
13973 typval_T *argvars;
13974 typval_T *rettv;
13975{
13976#ifdef FEAT_RELTIME
13977 proftime_T res;
13978 proftime_T start;
13979
13980 if (argvars[0].v_type == VAR_UNKNOWN)
13981 {
13982 /* No arguments: get current time. */
13983 profile_start(&res);
13984 }
13985 else if (argvars[1].v_type == VAR_UNKNOWN)
13986 {
13987 if (list2proftime(&argvars[0], &res) == FAIL)
13988 return;
13989 profile_end(&res);
13990 }
13991 else
13992 {
13993 /* Two arguments: compute the difference. */
13994 if (list2proftime(&argvars[0], &start) == FAIL
13995 || list2proftime(&argvars[1], &res) == FAIL)
13996 return;
13997 profile_sub(&res, &start);
13998 }
13999
14000 if (rettv_list_alloc(rettv) == OK)
14001 {
14002 long n1, n2;
14003
14004# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014005 n1 = res.HighPart;
14006 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014007# else
14008 n1 = res.tv_sec;
14009 n2 = res.tv_usec;
14010# endif
14011 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14012 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14013 }
14014#endif
14015}
14016
14017/*
14018 * "reltimestr()" function
14019 */
14020 static void
14021f_reltimestr(argvars, rettv)
14022 typval_T *argvars;
14023 typval_T *rettv;
14024{
14025#ifdef FEAT_RELTIME
14026 proftime_T tm;
14027#endif
14028
14029 rettv->v_type = VAR_STRING;
14030 rettv->vval.v_string = NULL;
14031#ifdef FEAT_RELTIME
14032 if (list2proftime(&argvars[0], &tm) == OK)
14033 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14034#endif
14035}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014036
Bram Moolenaar0d660222005-01-07 21:51:51 +000014037#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14038static void make_connection __ARGS((void));
14039static int check_connection __ARGS((void));
14040
14041 static void
14042make_connection()
14043{
14044 if (X_DISPLAY == NULL
14045# ifdef FEAT_GUI
14046 && !gui.in_use
14047# endif
14048 )
14049 {
14050 x_force_connect = TRUE;
14051 setup_term_clip();
14052 x_force_connect = FALSE;
14053 }
14054}
14055
14056 static int
14057check_connection()
14058{
14059 make_connection();
14060 if (X_DISPLAY == NULL)
14061 {
14062 EMSG(_("E240: No connection to Vim server"));
14063 return FAIL;
14064 }
14065 return OK;
14066}
14067#endif
14068
14069#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014070static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014071
14072 static void
14073remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014074 typval_T *argvars;
14075 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014076 int expr;
14077{
14078 char_u *server_name;
14079 char_u *keys;
14080 char_u *r = NULL;
14081 char_u buf[NUMBUFLEN];
14082# ifdef WIN32
14083 HWND w;
14084# else
14085 Window w;
14086# endif
14087
14088 if (check_restricted() || check_secure())
14089 return;
14090
14091# ifdef FEAT_X11
14092 if (check_connection() == FAIL)
14093 return;
14094# endif
14095
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014096 server_name = get_tv_string_chk(&argvars[0]);
14097 if (server_name == NULL)
14098 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014099 keys = get_tv_string_buf(&argvars[1], buf);
14100# ifdef WIN32
14101 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14102# else
14103 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14104 < 0)
14105# endif
14106 {
14107 if (r != NULL)
14108 EMSG(r); /* sending worked but evaluation failed */
14109 else
14110 EMSG2(_("E241: Unable to send to %s"), server_name);
14111 return;
14112 }
14113
14114 rettv->vval.v_string = r;
14115
14116 if (argvars[2].v_type != VAR_UNKNOWN)
14117 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014118 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014119 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014120 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014121
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014122 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014123 v.di_tv.v_type = VAR_STRING;
14124 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014125 idvar = get_tv_string_chk(&argvars[2]);
14126 if (idvar != NULL)
14127 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014128 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014129 }
14130}
14131#endif
14132
14133/*
14134 * "remote_expr()" function
14135 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014136 static void
14137f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014138 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014139 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014140{
14141 rettv->v_type = VAR_STRING;
14142 rettv->vval.v_string = NULL;
14143#ifdef FEAT_CLIENTSERVER
14144 remote_common(argvars, rettv, TRUE);
14145#endif
14146}
14147
14148/*
14149 * "remote_foreground()" function
14150 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014151 static void
14152f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014153 typval_T *argvars UNUSED;
14154 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014155{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014156#ifdef FEAT_CLIENTSERVER
14157# ifdef WIN32
14158 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014159 {
14160 char_u *server_name = get_tv_string_chk(&argvars[0]);
14161
14162 if (server_name != NULL)
14163 serverForeground(server_name);
14164 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014165# else
14166 /* Send a foreground() expression to the server. */
14167 argvars[1].v_type = VAR_STRING;
14168 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14169 argvars[2].v_type = VAR_UNKNOWN;
14170 remote_common(argvars, rettv, TRUE);
14171 vim_free(argvars[1].vval.v_string);
14172# endif
14173#endif
14174}
14175
Bram Moolenaar0d660222005-01-07 21:51:51 +000014176 static void
14177f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014178 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014179 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014180{
14181#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014182 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014183 char_u *s = NULL;
14184# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014185 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014186# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014187 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014188
14189 if (check_restricted() || check_secure())
14190 {
14191 rettv->vval.v_number = -1;
14192 return;
14193 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014194 serverid = get_tv_string_chk(&argvars[0]);
14195 if (serverid == NULL)
14196 {
14197 rettv->vval.v_number = -1;
14198 return; /* type error; errmsg already given */
14199 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014200# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014201 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014202 if (n == 0)
14203 rettv->vval.v_number = -1;
14204 else
14205 {
14206 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14207 rettv->vval.v_number = (s != NULL);
14208 }
14209# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014210 if (check_connection() == FAIL)
14211 return;
14212
14213 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014214 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014215# endif
14216
14217 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014219 char_u *retvar;
14220
Bram Moolenaar33570922005-01-25 22:26:29 +000014221 v.di_tv.v_type = VAR_STRING;
14222 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014223 retvar = get_tv_string_chk(&argvars[1]);
14224 if (retvar != NULL)
14225 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014226 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014227 }
14228#else
14229 rettv->vval.v_number = -1;
14230#endif
14231}
14232
Bram Moolenaar0d660222005-01-07 21:51:51 +000014233 static void
14234f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014235 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014236 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014237{
14238 char_u *r = NULL;
14239
14240#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014241 char_u *serverid = get_tv_string_chk(&argvars[0]);
14242
14243 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014244 {
14245# ifdef WIN32
14246 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014247 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014248
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014249 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014250 if (n != 0)
14251 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14252 if (r == NULL)
14253# else
14254 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014255 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014256# endif
14257 EMSG(_("E277: Unable to read a server reply"));
14258 }
14259#endif
14260 rettv->v_type = VAR_STRING;
14261 rettv->vval.v_string = r;
14262}
14263
14264/*
14265 * "remote_send()" function
14266 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014267 static void
14268f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014269 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014270 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014271{
14272 rettv->v_type = VAR_STRING;
14273 rettv->vval.v_string = NULL;
14274#ifdef FEAT_CLIENTSERVER
14275 remote_common(argvars, rettv, FALSE);
14276#endif
14277}
14278
14279/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014280 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014281 */
14282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014283f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014284 typval_T *argvars;
14285 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014286{
Bram Moolenaar33570922005-01-25 22:26:29 +000014287 list_T *l;
14288 listitem_T *item, *item2;
14289 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014290 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014291 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014292 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014293 dict_T *d;
14294 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014295
Bram Moolenaar8c711452005-01-14 21:53:12 +000014296 if (argvars[0].v_type == VAR_DICT)
14297 {
14298 if (argvars[2].v_type != VAR_UNKNOWN)
14299 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014300 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014301 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014303 key = get_tv_string_chk(&argvars[1]);
14304 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014306 di = dict_find(d, key, -1);
14307 if (di == NULL)
14308 EMSG2(_(e_dictkey), key);
14309 else
14310 {
14311 *rettv = di->di_tv;
14312 init_tv(&di->di_tv);
14313 dictitem_remove(d, di);
14314 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014315 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014316 }
14317 }
14318 else if (argvars[0].v_type != VAR_LIST)
14319 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014320 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014321 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014323 int error = FALSE;
14324
14325 idx = get_tv_number_chk(&argvars[1], &error);
14326 if (error)
14327 ; /* type error: do nothing, errmsg already given */
14328 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014329 EMSGN(_(e_listidx), idx);
14330 else
14331 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014332 if (argvars[2].v_type == VAR_UNKNOWN)
14333 {
14334 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014335 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014336 *rettv = item->li_tv;
14337 vim_free(item);
14338 }
14339 else
14340 {
14341 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014342 end = get_tv_number_chk(&argvars[2], &error);
14343 if (error)
14344 ; /* type error: do nothing */
14345 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014346 EMSGN(_(e_listidx), end);
14347 else
14348 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014349 int cnt = 0;
14350
14351 for (li = item; li != NULL; li = li->li_next)
14352 {
14353 ++cnt;
14354 if (li == item2)
14355 break;
14356 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014357 if (li == NULL) /* didn't find "item2" after "item" */
14358 EMSG(_(e_invrange));
14359 else
14360 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014361 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014362 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014363 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014364 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014365 l->lv_first = item;
14366 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014367 item->li_prev = NULL;
14368 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014369 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014370 }
14371 }
14372 }
14373 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014374 }
14375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376}
14377
14378/*
14379 * "rename({from}, {to})" function
14380 */
14381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014382f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014383 typval_T *argvars;
14384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385{
14386 char_u buf[NUMBUFLEN];
14387
14388 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014389 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014391 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14392 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393}
14394
14395/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014396 * "repeat()" function
14397 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014398 static void
14399f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014400 typval_T *argvars;
14401 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014402{
14403 char_u *p;
14404 int n;
14405 int slen;
14406 int len;
14407 char_u *r;
14408 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014409
14410 n = get_tv_number(&argvars[1]);
14411 if (argvars[0].v_type == VAR_LIST)
14412 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014413 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014414 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014415 if (list_extend(rettv->vval.v_list,
14416 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014417 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014418 }
14419 else
14420 {
14421 p = get_tv_string(&argvars[0]);
14422 rettv->v_type = VAR_STRING;
14423 rettv->vval.v_string = NULL;
14424
14425 slen = (int)STRLEN(p);
14426 len = slen * n;
14427 if (len <= 0)
14428 return;
14429
14430 r = alloc(len + 1);
14431 if (r != NULL)
14432 {
14433 for (i = 0; i < n; i++)
14434 mch_memmove(r + i * slen, p, (size_t)slen);
14435 r[len] = NUL;
14436 }
14437
14438 rettv->vval.v_string = r;
14439 }
14440}
14441
14442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443 * "resolve()" function
14444 */
14445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014446f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014447 typval_T *argvars;
14448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449{
14450 char_u *p;
14451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014452 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453#ifdef FEAT_SHORTCUT
14454 {
14455 char_u *v = NULL;
14456
14457 v = mch_resolve_shortcut(p);
14458 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014459 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014461 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462 }
14463#else
14464# ifdef HAVE_READLINK
14465 {
14466 char_u buf[MAXPATHL + 1];
14467 char_u *cpy;
14468 int len;
14469 char_u *remain = NULL;
14470 char_u *q;
14471 int is_relative_to_current = FALSE;
14472 int has_trailing_pathsep = FALSE;
14473 int limit = 100;
14474
14475 p = vim_strsave(p);
14476
14477 if (p[0] == '.' && (vim_ispathsep(p[1])
14478 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14479 is_relative_to_current = TRUE;
14480
14481 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014482 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483 has_trailing_pathsep = TRUE;
14484
14485 q = getnextcomp(p);
14486 if (*q != NUL)
14487 {
14488 /* Separate the first path component in "p", and keep the
14489 * remainder (beginning with the path separator). */
14490 remain = vim_strsave(q - 1);
14491 q[-1] = NUL;
14492 }
14493
14494 for (;;)
14495 {
14496 for (;;)
14497 {
14498 len = readlink((char *)p, (char *)buf, MAXPATHL);
14499 if (len <= 0)
14500 break;
14501 buf[len] = NUL;
14502
14503 if (limit-- == 0)
14504 {
14505 vim_free(p);
14506 vim_free(remain);
14507 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014508 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 goto fail;
14510 }
14511
14512 /* Ensure that the result will have a trailing path separator
14513 * if the argument has one. */
14514 if (remain == NULL && has_trailing_pathsep)
14515 add_pathsep(buf);
14516
14517 /* Separate the first path component in the link value and
14518 * concatenate the remainders. */
14519 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14520 if (*q != NUL)
14521 {
14522 if (remain == NULL)
14523 remain = vim_strsave(q - 1);
14524 else
14525 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014526 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527 if (cpy != NULL)
14528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529 vim_free(remain);
14530 remain = cpy;
14531 }
14532 }
14533 q[-1] = NUL;
14534 }
14535
14536 q = gettail(p);
14537 if (q > p && *q == NUL)
14538 {
14539 /* Ignore trailing path separator. */
14540 q[-1] = NUL;
14541 q = gettail(p);
14542 }
14543 if (q > p && !mch_isFullName(buf))
14544 {
14545 /* symlink is relative to directory of argument */
14546 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14547 if (cpy != NULL)
14548 {
14549 STRCPY(cpy, p);
14550 STRCPY(gettail(cpy), buf);
14551 vim_free(p);
14552 p = cpy;
14553 }
14554 }
14555 else
14556 {
14557 vim_free(p);
14558 p = vim_strsave(buf);
14559 }
14560 }
14561
14562 if (remain == NULL)
14563 break;
14564
14565 /* Append the first path component of "remain" to "p". */
14566 q = getnextcomp(remain + 1);
14567 len = q - remain - (*q != NUL);
14568 cpy = vim_strnsave(p, STRLEN(p) + len);
14569 if (cpy != NULL)
14570 {
14571 STRNCAT(cpy, remain, len);
14572 vim_free(p);
14573 p = cpy;
14574 }
14575 /* Shorten "remain". */
14576 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014577 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 else
14579 {
14580 vim_free(remain);
14581 remain = NULL;
14582 }
14583 }
14584
14585 /* If the result is a relative path name, make it explicitly relative to
14586 * the current directory if and only if the argument had this form. */
14587 if (!vim_ispathsep(*p))
14588 {
14589 if (is_relative_to_current
14590 && *p != NUL
14591 && !(p[0] == '.'
14592 && (p[1] == NUL
14593 || vim_ispathsep(p[1])
14594 || (p[1] == '.'
14595 && (p[2] == NUL
14596 || vim_ispathsep(p[2]))))))
14597 {
14598 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014599 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600 if (cpy != NULL)
14601 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602 vim_free(p);
14603 p = cpy;
14604 }
14605 }
14606 else if (!is_relative_to_current)
14607 {
14608 /* Strip leading "./". */
14609 q = p;
14610 while (q[0] == '.' && vim_ispathsep(q[1]))
14611 q += 2;
14612 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014613 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614 }
14615 }
14616
14617 /* Ensure that the result will have no trailing path separator
14618 * if the argument had none. But keep "/" or "//". */
14619 if (!has_trailing_pathsep)
14620 {
14621 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014622 if (after_pathsep(p, q))
14623 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624 }
14625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014626 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627 }
14628# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014629 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630# endif
14631#endif
14632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014633 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634
14635#ifdef HAVE_READLINK
14636fail:
14637#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014638 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639}
14640
14641/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014642 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014643 */
14644 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014645f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014646 typval_T *argvars;
14647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648{
Bram Moolenaar33570922005-01-25 22:26:29 +000014649 list_T *l;
14650 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014651
Bram Moolenaar0d660222005-01-07 21:51:51 +000014652 if (argvars[0].v_type != VAR_LIST)
14653 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014654 else if ((l = argvars[0].vval.v_list) != NULL
14655 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014656 {
14657 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014658 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014659 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014660 while (li != NULL)
14661 {
14662 ni = li->li_prev;
14663 list_append(l, li);
14664 li = ni;
14665 }
14666 rettv->vval.v_list = l;
14667 rettv->v_type = VAR_LIST;
14668 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014669 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014671}
14672
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014673#define SP_NOMOVE 0x01 /* don't move cursor */
14674#define SP_REPEAT 0x02 /* repeat to find outer pair */
14675#define SP_RETCOUNT 0x04 /* return matchcount */
14676#define SP_SETPCMARK 0x08 /* set previous context mark */
14677#define SP_START 0x10 /* accept match at start position */
14678#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14679#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014680
Bram Moolenaar33570922005-01-25 22:26:29 +000014681static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014682
14683/*
14684 * Get flags for a search function.
14685 * Possibly sets "p_ws".
14686 * Returns BACKWARD, FORWARD or zero (for an error).
14687 */
14688 static int
14689get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014690 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014691 int *flagsp;
14692{
14693 int dir = FORWARD;
14694 char_u *flags;
14695 char_u nbuf[NUMBUFLEN];
14696 int mask;
14697
14698 if (varp->v_type != VAR_UNKNOWN)
14699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014700 flags = get_tv_string_buf_chk(varp, nbuf);
14701 if (flags == NULL)
14702 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014703 while (*flags != NUL)
14704 {
14705 switch (*flags)
14706 {
14707 case 'b': dir = BACKWARD; break;
14708 case 'w': p_ws = TRUE; break;
14709 case 'W': p_ws = FALSE; break;
14710 default: mask = 0;
14711 if (flagsp != NULL)
14712 switch (*flags)
14713 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014714 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014715 case 'e': mask = SP_END; break;
14716 case 'm': mask = SP_RETCOUNT; break;
14717 case 'n': mask = SP_NOMOVE; break;
14718 case 'p': mask = SP_SUBPAT; break;
14719 case 'r': mask = SP_REPEAT; break;
14720 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014721 }
14722 if (mask == 0)
14723 {
14724 EMSG2(_(e_invarg2), flags);
14725 dir = 0;
14726 }
14727 else
14728 *flagsp |= mask;
14729 }
14730 if (dir == 0)
14731 break;
14732 ++flags;
14733 }
14734 }
14735 return dir;
14736}
14737
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014739 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014741 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014742search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014743 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014744 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014745 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014747 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748 char_u *pat;
14749 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014750 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751 int save_p_ws = p_ws;
14752 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014753 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014754 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014755 proftime_T tm;
14756#ifdef FEAT_RELTIME
14757 long time_limit = 0;
14758#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014759 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014760 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014762 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014763 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014764 if (dir == 0)
14765 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014766 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014767 if (flags & SP_START)
14768 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014769 if (flags & SP_END)
14770 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014771
Bram Moolenaar76929292008-01-06 19:07:36 +000014772 /* Optional arguments: line number to stop searching and timeout. */
14773 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014774 {
14775 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14776 if (lnum_stop < 0)
14777 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014778#ifdef FEAT_RELTIME
14779 if (argvars[3].v_type != VAR_UNKNOWN)
14780 {
14781 time_limit = get_tv_number_chk(&argvars[3], NULL);
14782 if (time_limit < 0)
14783 goto theend;
14784 }
14785#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014786 }
14787
Bram Moolenaar76929292008-01-06 19:07:36 +000014788#ifdef FEAT_RELTIME
14789 /* Set the time limit, if there is one. */
14790 profile_setlimit(time_limit, &tm);
14791#endif
14792
Bram Moolenaar231334e2005-07-25 20:46:57 +000014793 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014794 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014795 * Check to make sure only those flags are set.
14796 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14797 * flags cannot be set. Check for that condition also.
14798 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014799 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014800 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014802 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014803 goto theend;
14804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014805
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014806 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014807 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014808 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014809 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014811 if (flags & SP_SUBPAT)
14812 retval = subpatnum;
14813 else
14814 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014815 if (flags & SP_SETPCMARK)
14816 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014818 if (match_pos != NULL)
14819 {
14820 /* Store the match cursor position */
14821 match_pos->lnum = pos.lnum;
14822 match_pos->col = pos.col + 1;
14823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824 /* "/$" will put the cursor after the end of the line, may need to
14825 * correct that here */
14826 check_cursor();
14827 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014828
14829 /* If 'n' flag is used: restore cursor position. */
14830 if (flags & SP_NOMOVE)
14831 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014832 else
14833 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014834theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014836
14837 return retval;
14838}
14839
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014840#ifdef FEAT_FLOAT
14841/*
14842 * "round({float})" function
14843 */
14844 static void
14845f_round(argvars, rettv)
14846 typval_T *argvars;
14847 typval_T *rettv;
14848{
14849 float_T f;
14850
14851 rettv->v_type = VAR_FLOAT;
14852 if (get_float_arg(argvars, &f) == OK)
14853 /* round() is not in C90, use ceil() or floor() instead. */
14854 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
14855 else
14856 rettv->vval.v_float = 0.0;
14857}
14858#endif
14859
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014860/*
14861 * "search()" function
14862 */
14863 static void
14864f_search(argvars, rettv)
14865 typval_T *argvars;
14866 typval_T *rettv;
14867{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014868 int flags = 0;
14869
14870 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871}
14872
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014874 * "searchdecl()" function
14875 */
14876 static void
14877f_searchdecl(argvars, rettv)
14878 typval_T *argvars;
14879 typval_T *rettv;
14880{
14881 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014882 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014883 int error = FALSE;
14884 char_u *name;
14885
14886 rettv->vval.v_number = 1; /* default: FAIL */
14887
14888 name = get_tv_string_chk(&argvars[0]);
14889 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014890 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014891 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014892 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14893 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14894 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014895 if (!error && name != NULL)
14896 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014897 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014898}
14899
14900/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014901 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014902 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014903 static int
14904searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014905 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014906 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907{
14908 char_u *spat, *mpat, *epat;
14909 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911 int dir;
14912 int flags = 0;
14913 char_u nbuf1[NUMBUFLEN];
14914 char_u nbuf2[NUMBUFLEN];
14915 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014916 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014917 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014918 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919
Bram Moolenaar071d4272004-06-13 20:20:40 +000014920 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014921 spat = get_tv_string_chk(&argvars[0]);
14922 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14923 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14924 if (spat == NULL || mpat == NULL || epat == NULL)
14925 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926
Bram Moolenaar071d4272004-06-13 20:20:40 +000014927 /* Handle the optional fourth argument: flags */
14928 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014929 if (dir == 0)
14930 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014931
14932 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014933 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14934 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014935 if ((flags & (SP_END | SP_SUBPAT)) != 0
14936 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014937 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014938 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014939 goto theend;
14940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014942 /* Using 'r' implies 'W', otherwise it doesn't work. */
14943 if (flags & SP_REPEAT)
14944 p_ws = FALSE;
14945
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014946 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014947 if (argvars[3].v_type == VAR_UNKNOWN
14948 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949 skip = (char_u *)"";
14950 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014951 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014952 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014953 if (argvars[5].v_type != VAR_UNKNOWN)
14954 {
14955 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14956 if (lnum_stop < 0)
14957 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014958#ifdef FEAT_RELTIME
14959 if (argvars[6].v_type != VAR_UNKNOWN)
14960 {
14961 time_limit = get_tv_number_chk(&argvars[6], NULL);
14962 if (time_limit < 0)
14963 goto theend;
14964 }
14965#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014966 }
14967 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014968 if (skip == NULL)
14969 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014971 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000014972 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014973
14974theend:
14975 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014976
14977 return retval;
14978}
14979
14980/*
14981 * "searchpair()" function
14982 */
14983 static void
14984f_searchpair(argvars, rettv)
14985 typval_T *argvars;
14986 typval_T *rettv;
14987{
14988 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14989}
14990
14991/*
14992 * "searchpairpos()" function
14993 */
14994 static void
14995f_searchpairpos(argvars, rettv)
14996 typval_T *argvars;
14997 typval_T *rettv;
14998{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014999 pos_T match_pos;
15000 int lnum = 0;
15001 int col = 0;
15002
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015003 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015004 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015005
15006 if (searchpair_cmn(argvars, &match_pos) > 0)
15007 {
15008 lnum = match_pos.lnum;
15009 col = match_pos.col;
15010 }
15011
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015012 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15013 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015014}
15015
15016/*
15017 * Search for a start/middle/end thing.
15018 * Used by searchpair(), see its documentation for the details.
15019 * Returns 0 or -1 for no match,
15020 */
15021 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015022do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15023 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015024 char_u *spat; /* start pattern */
15025 char_u *mpat; /* middle pattern */
15026 char_u *epat; /* end pattern */
15027 int dir; /* BACKWARD or FORWARD */
15028 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015029 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015030 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015031 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015032 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015033{
15034 char_u *save_cpo;
15035 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15036 long retval = 0;
15037 pos_T pos;
15038 pos_T firstpos;
15039 pos_T foundpos;
15040 pos_T save_cursor;
15041 pos_T save_pos;
15042 int n;
15043 int r;
15044 int nest = 1;
15045 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015046 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015047 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015048
15049 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15050 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015051 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015052
Bram Moolenaar76929292008-01-06 19:07:36 +000015053#ifdef FEAT_RELTIME
15054 /* Set the time limit, if there is one. */
15055 profile_setlimit(time_limit, &tm);
15056#endif
15057
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015058 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15059 * start/middle/end (pat3, for the top pair). */
15060 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15061 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15062 if (pat2 == NULL || pat3 == NULL)
15063 goto theend;
15064 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15065 if (*mpat == NUL)
15066 STRCPY(pat3, pat2);
15067 else
15068 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15069 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015070 if (flags & SP_START)
15071 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015072
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073 save_cursor = curwin->w_cursor;
15074 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015075 clearpos(&firstpos);
15076 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077 pat = pat3;
15078 for (;;)
15079 {
15080 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015081 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15083 /* didn't find it or found the first match again: FAIL */
15084 break;
15085
15086 if (firstpos.lnum == 0)
15087 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015088 if (equalpos(pos, foundpos))
15089 {
15090 /* Found the same position again. Can happen with a pattern that
15091 * has "\zs" at the end and searching backwards. Advance one
15092 * character and try again. */
15093 if (dir == BACKWARD)
15094 decl(&pos);
15095 else
15096 incl(&pos);
15097 }
15098 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015099
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015100 /* clear the start flag to avoid getting stuck here */
15101 options &= ~SEARCH_START;
15102
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103 /* If the skip pattern matches, ignore this match. */
15104 if (*skip != NUL)
15105 {
15106 save_pos = curwin->w_cursor;
15107 curwin->w_cursor = pos;
15108 r = eval_to_bool(skip, &err, NULL, FALSE);
15109 curwin->w_cursor = save_pos;
15110 if (err)
15111 {
15112 /* Evaluating {skip} caused an error, break here. */
15113 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015114 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015115 break;
15116 }
15117 if (r)
15118 continue;
15119 }
15120
15121 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15122 {
15123 /* Found end when searching backwards or start when searching
15124 * forward: nested pair. */
15125 ++nest;
15126 pat = pat2; /* nested, don't search for middle */
15127 }
15128 else
15129 {
15130 /* Found end when searching forward or start when searching
15131 * backward: end of (nested) pair; or found middle in outer pair. */
15132 if (--nest == 1)
15133 pat = pat3; /* outer level, search for middle */
15134 }
15135
15136 if (nest == 0)
15137 {
15138 /* Found the match: return matchcount or line number. */
15139 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015140 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015142 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015143 if (flags & SP_SETPCMARK)
15144 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145 curwin->w_cursor = pos;
15146 if (!(flags & SP_REPEAT))
15147 break;
15148 nest = 1; /* search for next unmatched */
15149 }
15150 }
15151
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015152 if (match_pos != NULL)
15153 {
15154 /* Store the match cursor position */
15155 match_pos->lnum = curwin->w_cursor.lnum;
15156 match_pos->col = curwin->w_cursor.col + 1;
15157 }
15158
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015160 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161 curwin->w_cursor = save_cursor;
15162
15163theend:
15164 vim_free(pat2);
15165 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015166 if (p_cpo == empty_option)
15167 p_cpo = save_cpo;
15168 else
15169 /* Darn, evaluating the {skip} expression changed the value. */
15170 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015171
15172 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173}
15174
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015175/*
15176 * "searchpos()" function
15177 */
15178 static void
15179f_searchpos(argvars, rettv)
15180 typval_T *argvars;
15181 typval_T *rettv;
15182{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015183 pos_T match_pos;
15184 int lnum = 0;
15185 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015186 int n;
15187 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015188
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015189 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015190 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015191
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015192 n = search_cmn(argvars, &match_pos, &flags);
15193 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015194 {
15195 lnum = match_pos.lnum;
15196 col = match_pos.col;
15197 }
15198
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015199 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15200 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015201 if (flags & SP_SUBPAT)
15202 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015203}
15204
15205
Bram Moolenaar0d660222005-01-07 21:51:51 +000015206 static void
15207f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015208 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015210{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015211#ifdef FEAT_CLIENTSERVER
15212 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015213 char_u *server = get_tv_string_chk(&argvars[0]);
15214 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015215
Bram Moolenaar0d660222005-01-07 21:51:51 +000015216 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015217 if (server == NULL || reply == NULL)
15218 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015219 if (check_restricted() || check_secure())
15220 return;
15221# ifdef FEAT_X11
15222 if (check_connection() == FAIL)
15223 return;
15224# endif
15225
15226 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015227 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015228 EMSG(_("E258: Unable to send to client"));
15229 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015230 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015231 rettv->vval.v_number = 0;
15232#else
15233 rettv->vval.v_number = -1;
15234#endif
15235}
15236
Bram Moolenaar0d660222005-01-07 21:51:51 +000015237 static void
15238f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015239 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015240 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015241{
15242 char_u *r = NULL;
15243
15244#ifdef FEAT_CLIENTSERVER
15245# ifdef WIN32
15246 r = serverGetVimNames();
15247# else
15248 make_connection();
15249 if (X_DISPLAY != NULL)
15250 r = serverGetVimNames(X_DISPLAY);
15251# endif
15252#endif
15253 rettv->v_type = VAR_STRING;
15254 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015255}
15256
15257/*
15258 * "setbufvar()" function
15259 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015261f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015262 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015263 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264{
15265 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015266 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015268 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015269 char_u nbuf[NUMBUFLEN];
15270
15271 if (check_restricted() || check_secure())
15272 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015273 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15274 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015275 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276 varp = &argvars[2];
15277
15278 if (buf != NULL && varname != NULL && varp != NULL)
15279 {
15280 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282
15283 if (*varname == '&')
15284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015285 long numval;
15286 char_u *strval;
15287 int error = FALSE;
15288
Bram Moolenaar071d4272004-06-13 20:20:40 +000015289 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015290 numval = get_tv_number_chk(varp, &error);
15291 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015292 if (!error && strval != NULL)
15293 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015294 }
15295 else
15296 {
15297 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15298 if (bufvarname != NULL)
15299 {
15300 STRCPY(bufvarname, "b:");
15301 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015302 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303 vim_free(bufvarname);
15304 }
15305 }
15306
15307 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015308 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015310}
15311
15312/*
15313 * "setcmdpos()" function
15314 */
15315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015316f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015317 typval_T *argvars;
15318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015320 int pos = (int)get_tv_number(&argvars[0]) - 1;
15321
15322 if (pos >= 0)
15323 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324}
15325
15326/*
15327 * "setline()" function
15328 */
15329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015330f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015331 typval_T *argvars;
15332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333{
15334 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015335 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015336 list_T *l = NULL;
15337 listitem_T *li = NULL;
15338 long added = 0;
15339 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015341 lnum = get_tv_lnum(&argvars[0]);
15342 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015343 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015344 l = argvars[1].vval.v_list;
15345 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015347 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015348 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015349
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015350 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015351 for (;;)
15352 {
15353 if (l != NULL)
15354 {
15355 /* list argument, get next string */
15356 if (li == NULL)
15357 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015358 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015359 li = li->li_next;
15360 }
15361
15362 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015363 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015364 break;
15365 if (lnum <= curbuf->b_ml.ml_line_count)
15366 {
15367 /* existing line, replace it */
15368 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15369 {
15370 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015371 if (lnum == curwin->w_cursor.lnum)
15372 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015373 rettv->vval.v_number = 0; /* OK */
15374 }
15375 }
15376 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15377 {
15378 /* lnum is one past the last line, append the line */
15379 ++added;
15380 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15381 rettv->vval.v_number = 0; /* OK */
15382 }
15383
15384 if (l == NULL) /* only one string argument */
15385 break;
15386 ++lnum;
15387 }
15388
15389 if (added > 0)
15390 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391}
15392
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015393static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15394
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015396 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015397 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015398 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015399set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015400 win_T *wp UNUSED;
15401 typval_T *list_arg UNUSED;
15402 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015403 typval_T *rettv;
15404{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015405#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015406 char_u *act;
15407 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015408#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015409
Bram Moolenaar2641f772005-03-25 21:58:17 +000015410 rettv->vval.v_number = -1;
15411
15412#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015413 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015414 EMSG(_(e_listreq));
15415 else
15416 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015417 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015418
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015419 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015420 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015421 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015422 if (act == NULL)
15423 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015424 if (*act == 'a' || *act == 'r')
15425 action = *act;
15426 }
15427
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015428 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015429 rettv->vval.v_number = 0;
15430 }
15431#endif
15432}
15433
15434/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015435 * "setloclist()" function
15436 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015437 static void
15438f_setloclist(argvars, rettv)
15439 typval_T *argvars;
15440 typval_T *rettv;
15441{
15442 win_T *win;
15443
15444 rettv->vval.v_number = -1;
15445
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015446 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015447 if (win != NULL)
15448 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15449}
15450
15451/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015452 * "setmatches()" function
15453 */
15454 static void
15455f_setmatches(argvars, rettv)
15456 typval_T *argvars;
15457 typval_T *rettv;
15458{
15459#ifdef FEAT_SEARCH_EXTRA
15460 list_T *l;
15461 listitem_T *li;
15462 dict_T *d;
15463
15464 rettv->vval.v_number = -1;
15465 if (argvars[0].v_type != VAR_LIST)
15466 {
15467 EMSG(_(e_listreq));
15468 return;
15469 }
15470 if ((l = argvars[0].vval.v_list) != NULL)
15471 {
15472
15473 /* To some extent make sure that we are dealing with a list from
15474 * "getmatches()". */
15475 li = l->lv_first;
15476 while (li != NULL)
15477 {
15478 if (li->li_tv.v_type != VAR_DICT
15479 || (d = li->li_tv.vval.v_dict) == NULL)
15480 {
15481 EMSG(_(e_invarg));
15482 return;
15483 }
15484 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15485 && dict_find(d, (char_u *)"pattern", -1) != NULL
15486 && dict_find(d, (char_u *)"priority", -1) != NULL
15487 && dict_find(d, (char_u *)"id", -1) != NULL))
15488 {
15489 EMSG(_(e_invarg));
15490 return;
15491 }
15492 li = li->li_next;
15493 }
15494
15495 clear_matches(curwin);
15496 li = l->lv_first;
15497 while (li != NULL)
15498 {
15499 d = li->li_tv.vval.v_dict;
15500 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15501 get_dict_string(d, (char_u *)"pattern", FALSE),
15502 (int)get_dict_number(d, (char_u *)"priority"),
15503 (int)get_dict_number(d, (char_u *)"id"));
15504 li = li->li_next;
15505 }
15506 rettv->vval.v_number = 0;
15507 }
15508#endif
15509}
15510
15511/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015512 * "setpos()" function
15513 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015514 static void
15515f_setpos(argvars, rettv)
15516 typval_T *argvars;
15517 typval_T *rettv;
15518{
15519 pos_T pos;
15520 int fnum;
15521 char_u *name;
15522
Bram Moolenaar08250432008-02-13 11:42:46 +000015523 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015524 name = get_tv_string_chk(argvars);
15525 if (name != NULL)
15526 {
15527 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15528 {
15529 --pos.col;
Bram Moolenaar08250432008-02-13 11:42:46 +000015530 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015531 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015532 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015533 if (fnum == curbuf->b_fnum)
15534 {
15535 curwin->w_cursor = pos;
15536 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015537 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015538 }
15539 else
15540 EMSG(_(e_invarg));
15541 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015542 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15543 {
15544 /* set mark */
15545 if (setmark_pos(name[1], &pos, fnum) == OK)
15546 rettv->vval.v_number = 0;
15547 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015548 else
15549 EMSG(_(e_invarg));
15550 }
15551 }
15552}
15553
15554/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015555 * "setqflist()" function
15556 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015557 static void
15558f_setqflist(argvars, rettv)
15559 typval_T *argvars;
15560 typval_T *rettv;
15561{
15562 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15563}
15564
15565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566 * "setreg()" function
15567 */
15568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015569f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015570 typval_T *argvars;
15571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572{
15573 int regname;
15574 char_u *strregname;
15575 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015576 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577 int append;
15578 char_u yank_type;
15579 long block_len;
15580
15581 block_len = -1;
15582 yank_type = MAUTO;
15583 append = FALSE;
15584
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015585 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015586 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015588 if (strregname == NULL)
15589 return; /* type error; errmsg already given */
15590 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591 if (regname == 0 || regname == '@')
15592 regname = '"';
15593 else if (regname == '=')
15594 return;
15595
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015596 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015598 stropt = get_tv_string_chk(&argvars[2]);
15599 if (stropt == NULL)
15600 return; /* type error */
15601 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602 switch (*stropt)
15603 {
15604 case 'a': case 'A': /* append */
15605 append = TRUE;
15606 break;
15607 case 'v': case 'c': /* character-wise selection */
15608 yank_type = MCHAR;
15609 break;
15610 case 'V': case 'l': /* line-wise selection */
15611 yank_type = MLINE;
15612 break;
15613#ifdef FEAT_VISUAL
15614 case 'b': case Ctrl_V: /* block-wise selection */
15615 yank_type = MBLOCK;
15616 if (VIM_ISDIGIT(stropt[1]))
15617 {
15618 ++stropt;
15619 block_len = getdigits(&stropt) - 1;
15620 --stropt;
15621 }
15622 break;
15623#endif
15624 }
15625 }
15626
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015627 strval = get_tv_string_chk(&argvars[1]);
15628 if (strval != NULL)
15629 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015631 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632}
15633
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015634/*
15635 * "settabwinvar()" function
15636 */
15637 static void
15638f_settabwinvar(argvars, rettv)
15639 typval_T *argvars;
15640 typval_T *rettv;
15641{
15642 setwinvar(argvars, rettv, 1);
15643}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015644
15645/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015646 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015649f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015650 typval_T *argvars;
15651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015653 setwinvar(argvars, rettv, 0);
15654}
15655
15656/*
15657 * "setwinvar()" and "settabwinvar()" functions
15658 */
15659 static void
15660setwinvar(argvars, rettv, off)
15661 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015662 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015663 int off;
15664{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 win_T *win;
15666#ifdef FEAT_WINDOWS
15667 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015668 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669#endif
15670 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015671 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015673 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674
15675 if (check_restricted() || check_secure())
15676 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015677
15678#ifdef FEAT_WINDOWS
15679 if (off == 1)
15680 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15681 else
15682 tp = curtab;
15683#endif
15684 win = find_win_by_nr(&argvars[off], tp);
15685 varname = get_tv_string_chk(&argvars[off + 1]);
15686 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687
15688 if (win != NULL && varname != NULL && varp != NULL)
15689 {
15690#ifdef FEAT_WINDOWS
15691 /* set curwin to be our win, temporarily */
15692 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015693 save_curtab = curtab;
15694 goto_tabpage_tp(tp);
15695 if (!win_valid(win))
15696 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697 curwin = win;
15698 curbuf = curwin->w_buffer;
15699#endif
15700
15701 if (*varname == '&')
15702 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015703 long numval;
15704 char_u *strval;
15705 int error = FALSE;
15706
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015708 numval = get_tv_number_chk(varp, &error);
15709 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015710 if (!error && strval != NULL)
15711 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712 }
15713 else
15714 {
15715 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15716 if (winvarname != NULL)
15717 {
15718 STRCPY(winvarname, "w:");
15719 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015720 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721 vim_free(winvarname);
15722 }
15723 }
15724
15725#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015726 /* Restore current tabpage and window, if still valid (autocomands can
15727 * make them invalid). */
15728 if (valid_tabpage(save_curtab))
15729 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730 if (win_valid(save_curwin))
15731 {
15732 curwin = save_curwin;
15733 curbuf = curwin->w_buffer;
15734 }
15735#endif
15736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737}
15738
15739/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015740 * "shellescape({string})" function
15741 */
15742 static void
15743f_shellescape(argvars, rettv)
15744 typval_T *argvars;
15745 typval_T *rettv;
15746{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015747 rettv->vval.v_string = vim_strsave_shellescape(
15748 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015749 rettv->v_type = VAR_STRING;
15750}
15751
15752/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015753 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 */
15755 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015756f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015757 typval_T *argvars;
15758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015760 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761
Bram Moolenaar0d660222005-01-07 21:51:51 +000015762 p = get_tv_string(&argvars[0]);
15763 rettv->vval.v_string = vim_strsave(p);
15764 simplify_filename(rettv->vval.v_string); /* simplify in place */
15765 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015766}
15767
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015768#ifdef FEAT_FLOAT
15769/*
15770 * "sin()" function
15771 */
15772 static void
15773f_sin(argvars, rettv)
15774 typval_T *argvars;
15775 typval_T *rettv;
15776{
15777 float_T f;
15778
15779 rettv->v_type = VAR_FLOAT;
15780 if (get_float_arg(argvars, &f) == OK)
15781 rettv->vval.v_float = sin(f);
15782 else
15783 rettv->vval.v_float = 0.0;
15784}
15785#endif
15786
Bram Moolenaar0d660222005-01-07 21:51:51 +000015787static int
15788#ifdef __BORLANDC__
15789 _RTLENTRYF
15790#endif
15791 item_compare __ARGS((const void *s1, const void *s2));
15792static int
15793#ifdef __BORLANDC__
15794 _RTLENTRYF
15795#endif
15796 item_compare2 __ARGS((const void *s1, const void *s2));
15797
15798static int item_compare_ic;
15799static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015800static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015801#define ITEM_COMPARE_FAIL 999
15802
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015804 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015806 static int
15807#ifdef __BORLANDC__
15808_RTLENTRYF
15809#endif
15810item_compare(s1, s2)
15811 const void *s1;
15812 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015813{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015814 char_u *p1, *p2;
15815 char_u *tofree1, *tofree2;
15816 int res;
15817 char_u numbuf1[NUMBUFLEN];
15818 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015820 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15821 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015822 if (p1 == NULL)
15823 p1 = (char_u *)"";
15824 if (p2 == NULL)
15825 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015826 if (item_compare_ic)
15827 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015829 res = STRCMP(p1, p2);
15830 vim_free(tofree1);
15831 vim_free(tofree2);
15832 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833}
15834
15835 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015836#ifdef __BORLANDC__
15837_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015839item_compare2(s1, s2)
15840 const void *s1;
15841 const void *s2;
15842{
15843 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015844 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015845 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015846 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015847
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015848 /* shortcut after failure in previous call; compare all items equal */
15849 if (item_compare_func_err)
15850 return 0;
15851
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015852 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15853 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015854 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15855 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015856
15857 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015858 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015859 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015860 clear_tv(&argv[0]);
15861 clear_tv(&argv[1]);
15862
15863 if (res == FAIL)
15864 res = ITEM_COMPARE_FAIL;
15865 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015866 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15867 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000015868 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015869 clear_tv(&rettv);
15870 return res;
15871}
15872
15873/*
15874 * "sort({list})" function
15875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015877f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015878 typval_T *argvars;
15879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880{
Bram Moolenaar33570922005-01-25 22:26:29 +000015881 list_T *l;
15882 listitem_T *li;
15883 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015884 long len;
15885 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886
Bram Moolenaar0d660222005-01-07 21:51:51 +000015887 if (argvars[0].v_type != VAR_LIST)
15888 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015889 else
15890 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015891 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015892 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015893 return;
15894 rettv->vval.v_list = l;
15895 rettv->v_type = VAR_LIST;
15896 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897
Bram Moolenaar0d660222005-01-07 21:51:51 +000015898 len = list_len(l);
15899 if (len <= 1)
15900 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901
Bram Moolenaar0d660222005-01-07 21:51:51 +000015902 item_compare_ic = FALSE;
15903 item_compare_func = NULL;
15904 if (argvars[1].v_type != VAR_UNKNOWN)
15905 {
15906 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015907 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015908 else
15909 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015910 int error = FALSE;
15911
15912 i = get_tv_number_chk(&argvars[1], &error);
15913 if (error)
15914 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015915 if (i == 1)
15916 item_compare_ic = TRUE;
15917 else
15918 item_compare_func = get_tv_string(&argvars[1]);
15919 }
15920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015921
Bram Moolenaar0d660222005-01-07 21:51:51 +000015922 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015923 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015924 if (ptrs == NULL)
15925 return;
15926 i = 0;
15927 for (li = l->lv_first; li != NULL; li = li->li_next)
15928 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015930 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015931 /* test the compare function */
15932 if (item_compare_func != NULL
15933 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15934 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015935 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015936 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015937 {
15938 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015939 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015940 item_compare_func == NULL ? item_compare : item_compare2);
15941
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015942 if (!item_compare_func_err)
15943 {
15944 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000015945 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015946 l->lv_len = 0;
15947 for (i = 0; i < len; ++i)
15948 list_append(l, ptrs[i]);
15949 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015950 }
15951
15952 vim_free(ptrs);
15953 }
15954}
15955
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015956/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015957 * "soundfold({word})" function
15958 */
15959 static void
15960f_soundfold(argvars, rettv)
15961 typval_T *argvars;
15962 typval_T *rettv;
15963{
15964 char_u *s;
15965
15966 rettv->v_type = VAR_STRING;
15967 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015968#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015969 rettv->vval.v_string = eval_soundfold(s);
15970#else
15971 rettv->vval.v_string = vim_strsave(s);
15972#endif
15973}
15974
15975/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015976 * "spellbadword()" function
15977 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015978 static void
15979f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015980 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015981 typval_T *rettv;
15982{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015983 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015984 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015985 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015986
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015987 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015988 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015989
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015990#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015991 if (argvars[0].v_type == VAR_UNKNOWN)
15992 {
15993 /* Find the start and length of the badly spelled word. */
15994 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15995 if (len != 0)
15996 word = ml_get_cursor();
15997 }
15998 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15999 {
16000 char_u *str = get_tv_string_chk(&argvars[0]);
16001 int capcol = -1;
16002
16003 if (str != NULL)
16004 {
16005 /* Check the argument for spelling. */
16006 while (*str != NUL)
16007 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016008 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016009 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016010 {
16011 word = str;
16012 break;
16013 }
16014 str += len;
16015 }
16016 }
16017 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016018#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016019
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016020 list_append_string(rettv->vval.v_list, word, len);
16021 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016022 attr == HLF_SPB ? "bad" :
16023 attr == HLF_SPR ? "rare" :
16024 attr == HLF_SPL ? "local" :
16025 attr == HLF_SPC ? "caps" :
16026 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016027}
16028
16029/*
16030 * "spellsuggest()" function
16031 */
16032 static void
16033f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016034 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016035 typval_T *rettv;
16036{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016037#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016038 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016039 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016040 int maxcount;
16041 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016042 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016043 listitem_T *li;
16044 int need_capital = FALSE;
16045#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016046
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016047 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016048 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016049
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016050#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016051 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16052 {
16053 str = get_tv_string(&argvars[0]);
16054 if (argvars[1].v_type != VAR_UNKNOWN)
16055 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016056 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016057 if (maxcount <= 0)
16058 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016059 if (argvars[2].v_type != VAR_UNKNOWN)
16060 {
16061 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16062 if (typeerr)
16063 return;
16064 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016065 }
16066 else
16067 maxcount = 25;
16068
Bram Moolenaar4770d092006-01-12 23:22:24 +000016069 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016070
16071 for (i = 0; i < ga.ga_len; ++i)
16072 {
16073 str = ((char_u **)ga.ga_data)[i];
16074
16075 li = listitem_alloc();
16076 if (li == NULL)
16077 vim_free(str);
16078 else
16079 {
16080 li->li_tv.v_type = VAR_STRING;
16081 li->li_tv.v_lock = 0;
16082 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016083 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016084 }
16085 }
16086 ga_clear(&ga);
16087 }
16088#endif
16089}
16090
Bram Moolenaar0d660222005-01-07 21:51:51 +000016091 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016092f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016093 typval_T *argvars;
16094 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016095{
16096 char_u *str;
16097 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016098 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016099 regmatch_T regmatch;
16100 char_u patbuf[NUMBUFLEN];
16101 char_u *save_cpo;
16102 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016103 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016104 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016105 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016106
16107 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16108 save_cpo = p_cpo;
16109 p_cpo = (char_u *)"";
16110
16111 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016112 if (argvars[1].v_type != VAR_UNKNOWN)
16113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016114 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16115 if (pat == NULL)
16116 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016117 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016118 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016119 }
16120 if (pat == NULL || *pat == NUL)
16121 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016122
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016123 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016125 if (typeerr)
16126 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016127
Bram Moolenaar0d660222005-01-07 21:51:51 +000016128 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16129 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016131 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016132 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016133 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016134 if (*str == NUL)
16135 match = FALSE; /* empty item at the end */
16136 else
16137 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016138 if (match)
16139 end = regmatch.startp[0];
16140 else
16141 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016142 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16143 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016144 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016145 if (list_append_string(rettv->vval.v_list, str,
16146 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016147 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016148 }
16149 if (!match)
16150 break;
16151 /* Advance to just after the match. */
16152 if (regmatch.endp[0] > str)
16153 col = 0;
16154 else
16155 {
16156 /* Don't get stuck at the same match. */
16157#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016158 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016159#else
16160 col = 1;
16161#endif
16162 }
16163 str = regmatch.endp[0];
16164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165
Bram Moolenaar0d660222005-01-07 21:51:51 +000016166 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168
Bram Moolenaar0d660222005-01-07 21:51:51 +000016169 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170}
16171
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016172#ifdef FEAT_FLOAT
16173/*
16174 * "sqrt()" function
16175 */
16176 static void
16177f_sqrt(argvars, rettv)
16178 typval_T *argvars;
16179 typval_T *rettv;
16180{
16181 float_T f;
16182
16183 rettv->v_type = VAR_FLOAT;
16184 if (get_float_arg(argvars, &f) == OK)
16185 rettv->vval.v_float = sqrt(f);
16186 else
16187 rettv->vval.v_float = 0.0;
16188}
16189
16190/*
16191 * "str2float()" function
16192 */
16193 static void
16194f_str2float(argvars, rettv)
16195 typval_T *argvars;
16196 typval_T *rettv;
16197{
16198 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16199
16200 if (*p == '+')
16201 p = skipwhite(p + 1);
16202 (void)string2float(p, &rettv->vval.v_float);
16203 rettv->v_type = VAR_FLOAT;
16204}
16205#endif
16206
Bram Moolenaar2c932302006-03-18 21:42:09 +000016207/*
16208 * "str2nr()" function
16209 */
16210 static void
16211f_str2nr(argvars, rettv)
16212 typval_T *argvars;
16213 typval_T *rettv;
16214{
16215 int base = 10;
16216 char_u *p;
16217 long n;
16218
16219 if (argvars[1].v_type != VAR_UNKNOWN)
16220 {
16221 base = get_tv_number(&argvars[1]);
16222 if (base != 8 && base != 10 && base != 16)
16223 {
16224 EMSG(_(e_invarg));
16225 return;
16226 }
16227 }
16228
16229 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016230 if (*p == '+')
16231 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016232 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16233 rettv->vval.v_number = n;
16234}
16235
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236#ifdef HAVE_STRFTIME
16237/*
16238 * "strftime({format}[, {time}])" function
16239 */
16240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016241f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016242 typval_T *argvars;
16243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244{
16245 char_u result_buf[256];
16246 struct tm *curtime;
16247 time_t seconds;
16248 char_u *p;
16249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016250 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016252 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016253 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254 seconds = time(NULL);
16255 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016256 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016257 curtime = localtime(&seconds);
16258 /* MSVC returns NULL for an invalid value of seconds. */
16259 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016260 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261 else
16262 {
16263# ifdef FEAT_MBYTE
16264 vimconv_T conv;
16265 char_u *enc;
16266
16267 conv.vc_type = CONV_NONE;
16268 enc = enc_locale();
16269 convert_setup(&conv, p_enc, enc);
16270 if (conv.vc_type != CONV_NONE)
16271 p = string_convert(&conv, p, NULL);
16272# endif
16273 if (p != NULL)
16274 (void)strftime((char *)result_buf, sizeof(result_buf),
16275 (char *)p, curtime);
16276 else
16277 result_buf[0] = NUL;
16278
16279# ifdef FEAT_MBYTE
16280 if (conv.vc_type != CONV_NONE)
16281 vim_free(p);
16282 convert_setup(&conv, enc, p_enc);
16283 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016284 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285 else
16286# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016287 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288
16289# ifdef FEAT_MBYTE
16290 /* Release conversion descriptors */
16291 convert_setup(&conv, NULL, NULL);
16292 vim_free(enc);
16293# endif
16294 }
16295}
16296#endif
16297
16298/*
16299 * "stridx()" function
16300 */
16301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016302f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016303 typval_T *argvars;
16304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305{
16306 char_u buf[NUMBUFLEN];
16307 char_u *needle;
16308 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016309 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016311 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016313 needle = get_tv_string_chk(&argvars[1]);
16314 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016315 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016316 if (needle == NULL || haystack == NULL)
16317 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016318
Bram Moolenaar33570922005-01-25 22:26:29 +000016319 if (argvars[2].v_type != VAR_UNKNOWN)
16320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016321 int error = FALSE;
16322
16323 start_idx = get_tv_number_chk(&argvars[2], &error);
16324 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016325 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016326 if (start_idx >= 0)
16327 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016328 }
16329
16330 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16331 if (pos != NULL)
16332 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016333}
16334
16335/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016336 * "string()" function
16337 */
16338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016339f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016340 typval_T *argvars;
16341 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016342{
16343 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016344 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016345
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016346 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016347 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016348 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016349 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016350 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351}
16352
16353/*
16354 * "strlen()" function
16355 */
16356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016357f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016358 typval_T *argvars;
16359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016361 rettv->vval.v_number = (varnumber_T)(STRLEN(
16362 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363}
16364
16365/*
16366 * "strpart()" function
16367 */
16368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016369f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016370 typval_T *argvars;
16371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016372{
16373 char_u *p;
16374 int n;
16375 int len;
16376 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016377 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016379 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380 slen = (int)STRLEN(p);
16381
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016382 n = get_tv_number_chk(&argvars[1], &error);
16383 if (error)
16384 len = 0;
16385 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016386 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387 else
16388 len = slen - n; /* default len: all bytes that are available. */
16389
16390 /*
16391 * Only return the overlap between the specified part and the actual
16392 * string.
16393 */
16394 if (n < 0)
16395 {
16396 len += n;
16397 n = 0;
16398 }
16399 else if (n > slen)
16400 n = slen;
16401 if (len < 0)
16402 len = 0;
16403 else if (n + len > slen)
16404 len = slen - n;
16405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016406 rettv->v_type = VAR_STRING;
16407 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408}
16409
16410/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016411 * "strridx()" function
16412 */
16413 static void
16414f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016415 typval_T *argvars;
16416 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016417{
16418 char_u buf[NUMBUFLEN];
16419 char_u *needle;
16420 char_u *haystack;
16421 char_u *rest;
16422 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016423 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016425 needle = get_tv_string_chk(&argvars[1]);
16426 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016427
16428 rettv->vval.v_number = -1;
16429 if (needle == NULL || haystack == NULL)
16430 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016431
16432 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016433 if (argvars[2].v_type != VAR_UNKNOWN)
16434 {
16435 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016436 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016437 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016438 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016439 }
16440 else
16441 end_idx = haystack_len;
16442
Bram Moolenaar0d660222005-01-07 21:51:51 +000016443 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016444 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016445 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016446 lastmatch = haystack + end_idx;
16447 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016448 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016449 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016450 for (rest = haystack; *rest != '\0'; ++rest)
16451 {
16452 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016453 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016454 break;
16455 lastmatch = rest;
16456 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016457 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016458
16459 if (lastmatch == NULL)
16460 rettv->vval.v_number = -1;
16461 else
16462 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16463}
16464
16465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466 * "strtrans()" function
16467 */
16468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016469f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016470 typval_T *argvars;
16471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016473 rettv->v_type = VAR_STRING;
16474 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475}
16476
16477/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016478 * "submatch()" function
16479 */
16480 static void
16481f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016482 typval_T *argvars;
16483 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016484{
16485 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016486 rettv->vval.v_string =
16487 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016488}
16489
16490/*
16491 * "substitute()" function
16492 */
16493 static void
16494f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016495 typval_T *argvars;
16496 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016497{
16498 char_u patbuf[NUMBUFLEN];
16499 char_u subbuf[NUMBUFLEN];
16500 char_u flagsbuf[NUMBUFLEN];
16501
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016502 char_u *str = get_tv_string_chk(&argvars[0]);
16503 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16504 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16505 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16506
Bram Moolenaar0d660222005-01-07 21:51:51 +000016507 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016508 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16509 rettv->vval.v_string = NULL;
16510 else
16511 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016512}
16513
16514/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016515 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016518f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016519 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521{
16522 int id = 0;
16523#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016524 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525 long col;
16526 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016527 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016529 lnum = get_tv_lnum(argvars); /* -1 on type error */
16530 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16531 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016533 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016534 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016535 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536#endif
16537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016538 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539}
16540
16541/*
16542 * "synIDattr(id, what [, mode])" function
16543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016545f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016546 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548{
16549 char_u *p = NULL;
16550#ifdef FEAT_SYN_HL
16551 int id;
16552 char_u *what;
16553 char_u *mode;
16554 char_u modebuf[NUMBUFLEN];
16555 int modec;
16556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016557 id = get_tv_number(&argvars[0]);
16558 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016559 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016561 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562 modec = TOLOWER_ASC(mode[0]);
16563 if (modec != 't' && modec != 'c'
16564#ifdef FEAT_GUI
16565 && modec != 'g'
16566#endif
16567 )
16568 modec = 0; /* replace invalid with current */
16569 }
16570 else
16571 {
16572#ifdef FEAT_GUI
16573 if (gui.in_use)
16574 modec = 'g';
16575 else
16576#endif
16577 if (t_colors > 1)
16578 modec = 'c';
16579 else
16580 modec = 't';
16581 }
16582
16583
16584 switch (TOLOWER_ASC(what[0]))
16585 {
16586 case 'b':
16587 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16588 p = highlight_color(id, what, modec);
16589 else /* bold */
16590 p = highlight_has_attr(id, HL_BOLD, modec);
16591 break;
16592
16593 case 'f': /* fg[#] */
16594 p = highlight_color(id, what, modec);
16595 break;
16596
16597 case 'i':
16598 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16599 p = highlight_has_attr(id, HL_INVERSE, modec);
16600 else /* italic */
16601 p = highlight_has_attr(id, HL_ITALIC, modec);
16602 break;
16603
16604 case 'n': /* name */
16605 p = get_highlight_name(NULL, id - 1);
16606 break;
16607
16608 case 'r': /* reverse */
16609 p = highlight_has_attr(id, HL_INVERSE, modec);
16610 break;
16611
Bram Moolenaar6f507d62008-11-28 10:16:05 +000016612 case 's':
16613 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
16614 p = highlight_color(id, what, modec);
16615 else /* standout */
16616 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016617 break;
16618
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016619 case 'u':
16620 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16621 /* underline */
16622 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16623 else
16624 /* undercurl */
16625 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626 break;
16627 }
16628
16629 if (p != NULL)
16630 p = vim_strsave(p);
16631#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016632 rettv->v_type = VAR_STRING;
16633 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634}
16635
16636/*
16637 * "synIDtrans(id)" function
16638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016640f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016641 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643{
16644 int id;
16645
16646#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016647 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648
16649 if (id > 0)
16650 id = syn_get_final_id(id);
16651 else
16652#endif
16653 id = 0;
16654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016655 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656}
16657
16658/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016659 * "synstack(lnum, col)" function
16660 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016661 static void
16662f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016663 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016664 typval_T *rettv;
16665{
16666#ifdef FEAT_SYN_HL
16667 long lnum;
16668 long col;
16669 int i;
16670 int id;
16671#endif
16672
16673 rettv->v_type = VAR_LIST;
16674 rettv->vval.v_list = NULL;
16675
16676#ifdef FEAT_SYN_HL
16677 lnum = get_tv_lnum(argvars); /* -1 on type error */
16678 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16679
16680 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar6cad8bd2008-09-10 13:39:10 +000016681 && col >= 0 && (col == 0 || col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016682 && rettv_list_alloc(rettv) != FAIL)
16683 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016684 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016685 for (i = 0; ; ++i)
16686 {
16687 id = syn_get_stack_item(i);
16688 if (id < 0)
16689 break;
16690 if (list_append_number(rettv->vval.v_list, id) == FAIL)
16691 break;
16692 }
16693 }
16694#endif
16695}
16696
16697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016698 * "system()" function
16699 */
16700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016701f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016702 typval_T *argvars;
16703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016705 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016707 char_u *infile = NULL;
16708 char_u buf[NUMBUFLEN];
16709 int err = FALSE;
16710 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016712 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016713 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016714
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016715 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016716 {
16717 /*
16718 * Write the string to a temp file, to be used for input of the shell
16719 * command.
16720 */
16721 if ((infile = vim_tempname('i')) == NULL)
16722 {
16723 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016724 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016725 }
16726
16727 fd = mch_fopen((char *)infile, WRITEBIN);
16728 if (fd == NULL)
16729 {
16730 EMSG2(_(e_notopen), infile);
16731 goto done;
16732 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016733 p = get_tv_string_buf_chk(&argvars[1], buf);
16734 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016735 {
16736 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016737 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016738 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016739 if (fwrite(p, STRLEN(p), 1, fd) != 1)
16740 err = TRUE;
16741 if (fclose(fd) != 0)
16742 err = TRUE;
16743 if (err)
16744 {
16745 EMSG(_("E677: Error writing temp file"));
16746 goto done;
16747 }
16748 }
16749
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016750 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
16751 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016752
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753#ifdef USE_CR
16754 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016755 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 {
16757 char_u *s;
16758
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016759 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760 {
16761 if (*s == CAR)
16762 *s = NL;
16763 }
16764 }
16765#else
16766# ifdef USE_CRNL
16767 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016768 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769 {
16770 char_u *s, *d;
16771
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016772 d = res;
16773 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774 {
16775 if (s[0] == CAR && s[1] == NL)
16776 ++s;
16777 *d++ = *s;
16778 }
16779 *d = NUL;
16780 }
16781# endif
16782#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016783
16784done:
16785 if (infile != NULL)
16786 {
16787 mch_remove(infile);
16788 vim_free(infile);
16789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016790 rettv->v_type = VAR_STRING;
16791 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792}
16793
16794/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016795 * "tabpagebuflist()" function
16796 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016797 static void
16798f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016799 typval_T *argvars UNUSED;
16800 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016801{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016802#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016803 tabpage_T *tp;
16804 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016805
16806 if (argvars[0].v_type == VAR_UNKNOWN)
16807 wp = firstwin;
16808 else
16809 {
16810 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16811 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016812 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016813 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016814 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016815 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016816 for (; wp != NULL; wp = wp->w_next)
16817 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016818 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016819 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016820 }
16821#endif
16822}
16823
16824
16825/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016826 * "tabpagenr()" function
16827 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016828 static void
16829f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016830 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016831 typval_T *rettv;
16832{
16833 int nr = 1;
16834#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016835 char_u *arg;
16836
16837 if (argvars[0].v_type != VAR_UNKNOWN)
16838 {
16839 arg = get_tv_string_chk(&argvars[0]);
16840 nr = 0;
16841 if (arg != NULL)
16842 {
16843 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016844 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016845 else
16846 EMSG2(_(e_invexpr2), arg);
16847 }
16848 }
16849 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016850 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016851#endif
16852 rettv->vval.v_number = nr;
16853}
16854
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016855
16856#ifdef FEAT_WINDOWS
16857static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16858
16859/*
16860 * Common code for tabpagewinnr() and winnr().
16861 */
16862 static int
16863get_winnr(tp, argvar)
16864 tabpage_T *tp;
16865 typval_T *argvar;
16866{
16867 win_T *twin;
16868 int nr = 1;
16869 win_T *wp;
16870 char_u *arg;
16871
16872 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16873 if (argvar->v_type != VAR_UNKNOWN)
16874 {
16875 arg = get_tv_string_chk(argvar);
16876 if (arg == NULL)
16877 nr = 0; /* type error; errmsg already given */
16878 else if (STRCMP(arg, "$") == 0)
16879 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16880 else if (STRCMP(arg, "#") == 0)
16881 {
16882 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16883 if (twin == NULL)
16884 nr = 0;
16885 }
16886 else
16887 {
16888 EMSG2(_(e_invexpr2), arg);
16889 nr = 0;
16890 }
16891 }
16892
16893 if (nr > 0)
16894 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16895 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016896 {
16897 if (wp == NULL)
16898 {
16899 /* didn't find it in this tabpage */
16900 nr = 0;
16901 break;
16902 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016903 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016904 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016905 return nr;
16906}
16907#endif
16908
16909/*
16910 * "tabpagewinnr()" function
16911 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016912 static void
16913f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016914 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016915 typval_T *rettv;
16916{
16917 int nr = 1;
16918#ifdef FEAT_WINDOWS
16919 tabpage_T *tp;
16920
16921 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16922 if (tp == NULL)
16923 nr = 0;
16924 else
16925 nr = get_winnr(tp, &argvars[1]);
16926#endif
16927 rettv->vval.v_number = nr;
16928}
16929
16930
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016931/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016932 * "tagfiles()" function
16933 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016934 static void
16935f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016936 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016937 typval_T *rettv;
16938{
16939 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016940 tagname_T tn;
16941 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016942
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016943 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016944 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016945
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016946 for (first = TRUE; ; first = FALSE)
16947 if (get_tagfname(&tn, first, fname) == FAIL
16948 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016949 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016950 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016951}
16952
16953/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016954 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016955 */
16956 static void
16957f_taglist(argvars, rettv)
16958 typval_T *argvars;
16959 typval_T *rettv;
16960{
16961 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016962
16963 tag_pattern = get_tv_string(&argvars[0]);
16964
16965 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016966 if (*tag_pattern == NUL)
16967 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016968
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016969 if (rettv_list_alloc(rettv) == OK)
16970 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016971}
16972
16973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974 * "tempname()" function
16975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016977f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016978 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016980{
16981 static int x = 'A';
16982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016983 rettv->v_type = VAR_STRING;
16984 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985
16986 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16987 * names. Skip 'I' and 'O', they are used for shell redirection. */
16988 do
16989 {
16990 if (x == 'Z')
16991 x = '0';
16992 else if (x == '9')
16993 x = 'A';
16994 else
16995 {
16996#ifdef EBCDIC
16997 if (x == 'I')
16998 x = 'J';
16999 else if (x == 'R')
17000 x = 'S';
17001 else
17002#endif
17003 ++x;
17004 }
17005 } while (x == 'I' || x == 'O');
17006}
17007
17008/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017009 * "test(list)" function: Just checking the walls...
17010 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017011 static void
17012f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017013 typval_T *argvars UNUSED;
17014 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017015{
17016 /* Used for unit testing. Change the code below to your liking. */
17017#if 0
17018 listitem_T *li;
17019 list_T *l;
17020 char_u *bad, *good;
17021
17022 if (argvars[0].v_type != VAR_LIST)
17023 return;
17024 l = argvars[0].vval.v_list;
17025 if (l == NULL)
17026 return;
17027 li = l->lv_first;
17028 if (li == NULL)
17029 return;
17030 bad = get_tv_string(&li->li_tv);
17031 li = li->li_next;
17032 if (li == NULL)
17033 return;
17034 good = get_tv_string(&li->li_tv);
17035 rettv->vval.v_number = test_edit_score(bad, good);
17036#endif
17037}
17038
17039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040 * "tolower(string)" function
17041 */
17042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017043f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017044 typval_T *argvars;
17045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046{
17047 char_u *p;
17048
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017049 p = vim_strsave(get_tv_string(&argvars[0]));
17050 rettv->v_type = VAR_STRING;
17051 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052
17053 if (p != NULL)
17054 while (*p != NUL)
17055 {
17056#ifdef FEAT_MBYTE
17057 int l;
17058
17059 if (enc_utf8)
17060 {
17061 int c, lc;
17062
17063 c = utf_ptr2char(p);
17064 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017065 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066 /* TODO: reallocate string when byte count changes. */
17067 if (utf_char2len(lc) == l)
17068 utf_char2bytes(lc, p);
17069 p += l;
17070 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017071 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072 p += l; /* skip multi-byte character */
17073 else
17074#endif
17075 {
17076 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17077 ++p;
17078 }
17079 }
17080}
17081
17082/*
17083 * "toupper(string)" function
17084 */
17085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017086f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017087 typval_T *argvars;
17088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017090 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017091 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092}
17093
17094/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017095 * "tr(string, fromstr, tostr)" function
17096 */
17097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017098f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017099 typval_T *argvars;
17100 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017101{
17102 char_u *instr;
17103 char_u *fromstr;
17104 char_u *tostr;
17105 char_u *p;
17106#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017107 int inlen;
17108 int fromlen;
17109 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017110 int idx;
17111 char_u *cpstr;
17112 int cplen;
17113 int first = TRUE;
17114#endif
17115 char_u buf[NUMBUFLEN];
17116 char_u buf2[NUMBUFLEN];
17117 garray_T ga;
17118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017119 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017120 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17121 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017122
17123 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017124 rettv->v_type = VAR_STRING;
17125 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017126 if (fromstr == NULL || tostr == NULL)
17127 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017128 ga_init2(&ga, (int)sizeof(char), 80);
17129
17130#ifdef FEAT_MBYTE
17131 if (!has_mbyte)
17132#endif
17133 /* not multi-byte: fromstr and tostr must be the same length */
17134 if (STRLEN(fromstr) != STRLEN(tostr))
17135 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017136#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017137error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017138#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017139 EMSG2(_(e_invarg2), fromstr);
17140 ga_clear(&ga);
17141 return;
17142 }
17143
17144 /* fromstr and tostr have to contain the same number of chars */
17145 while (*instr != NUL)
17146 {
17147#ifdef FEAT_MBYTE
17148 if (has_mbyte)
17149 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017150 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017151 cpstr = instr;
17152 cplen = inlen;
17153 idx = 0;
17154 for (p = fromstr; *p != NUL; p += fromlen)
17155 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017156 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017157 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17158 {
17159 for (p = tostr; *p != NUL; p += tolen)
17160 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017161 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017162 if (idx-- == 0)
17163 {
17164 cplen = tolen;
17165 cpstr = p;
17166 break;
17167 }
17168 }
17169 if (*p == NUL) /* tostr is shorter than fromstr */
17170 goto error;
17171 break;
17172 }
17173 ++idx;
17174 }
17175
17176 if (first && cpstr == instr)
17177 {
17178 /* Check that fromstr and tostr have the same number of
17179 * (multi-byte) characters. Done only once when a character
17180 * of instr doesn't appear in fromstr. */
17181 first = FALSE;
17182 for (p = tostr; *p != NUL; p += tolen)
17183 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017184 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017185 --idx;
17186 }
17187 if (idx != 0)
17188 goto error;
17189 }
17190
17191 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017192 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017193 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017194
17195 instr += inlen;
17196 }
17197 else
17198#endif
17199 {
17200 /* When not using multi-byte chars we can do it faster. */
17201 p = vim_strchr(fromstr, *instr);
17202 if (p != NULL)
17203 ga_append(&ga, tostr[p - fromstr]);
17204 else
17205 ga_append(&ga, *instr);
17206 ++instr;
17207 }
17208 }
17209
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017210 /* add a terminating NUL */
17211 ga_grow(&ga, 1);
17212 ga_append(&ga, NUL);
17213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017214 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017215}
17216
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017217#ifdef FEAT_FLOAT
17218/*
17219 * "trunc({float})" function
17220 */
17221 static void
17222f_trunc(argvars, rettv)
17223 typval_T *argvars;
17224 typval_T *rettv;
17225{
17226 float_T f;
17227
17228 rettv->v_type = VAR_FLOAT;
17229 if (get_float_arg(argvars, &f) == OK)
17230 /* trunc() is not in C90, use floor() or ceil() instead. */
17231 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17232 else
17233 rettv->vval.v_float = 0.0;
17234}
17235#endif
17236
Bram Moolenaar8299df92004-07-10 09:47:34 +000017237/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238 * "type(expr)" function
17239 */
17240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017241f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017242 typval_T *argvars;
17243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017245 int n;
17246
17247 switch (argvars[0].v_type)
17248 {
17249 case VAR_NUMBER: n = 0; break;
17250 case VAR_STRING: n = 1; break;
17251 case VAR_FUNC: n = 2; break;
17252 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017253 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017254#ifdef FEAT_FLOAT
17255 case VAR_FLOAT: n = 5; break;
17256#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017257 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17258 }
17259 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260}
17261
17262/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017263 * "values(dict)" function
17264 */
17265 static void
17266f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017267 typval_T *argvars;
17268 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017269{
17270 dict_list(argvars, rettv, 1);
17271}
17272
17273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274 * "virtcol(string)" function
17275 */
17276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017277f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017278 typval_T *argvars;
17279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280{
17281 colnr_T vcol = 0;
17282 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017283 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017285 fp = var2fpos(&argvars[0], FALSE, &fnum);
17286 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17287 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288 {
17289 getvvcol(curwin, fp, NULL, NULL, &vcol);
17290 ++vcol;
17291 }
17292
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017293 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294}
17295
17296/*
17297 * "visualmode()" function
17298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017300f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017301 typval_T *argvars UNUSED;
17302 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303{
17304#ifdef FEAT_VISUAL
17305 char_u str[2];
17306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017307 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308 str[0] = curbuf->b_visual_mode_eval;
17309 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017310 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311
17312 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017313 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315#endif
17316}
17317
17318/*
17319 * "winbufnr(nr)" function
17320 */
17321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017322f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017323 typval_T *argvars;
17324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017325{
17326 win_T *wp;
17327
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017328 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017330 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017332 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333}
17334
17335/*
17336 * "wincol()" function
17337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017339f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017340 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342{
17343 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017344 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345}
17346
17347/*
17348 * "winheight(nr)" function
17349 */
17350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017351f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017352 typval_T *argvars;
17353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354{
17355 win_T *wp;
17356
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017357 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017359 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017361 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362}
17363
17364/*
17365 * "winline()" function
17366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017368f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017369 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371{
17372 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017373 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374}
17375
17376/*
17377 * "winnr()" function
17378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017380f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017381 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383{
17384 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017385
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017387 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017389 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390}
17391
17392/*
17393 * "winrestcmd()" function
17394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017396f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017397 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399{
17400#ifdef FEAT_WINDOWS
17401 win_T *wp;
17402 int winnr = 1;
17403 garray_T ga;
17404 char_u buf[50];
17405
17406 ga_init2(&ga, (int)sizeof(char), 70);
17407 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17408 {
17409 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17410 ga_concat(&ga, buf);
17411# ifdef FEAT_VERTSPLIT
17412 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17413 ga_concat(&ga, buf);
17414# endif
17415 ++winnr;
17416 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017417 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017419 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017421 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017423 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424}
17425
17426/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017427 * "winrestview()" function
17428 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017429 static void
17430f_winrestview(argvars, rettv)
17431 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017432 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017433{
17434 dict_T *dict;
17435
17436 if (argvars[0].v_type != VAR_DICT
17437 || (dict = argvars[0].vval.v_dict) == NULL)
17438 EMSG(_(e_invarg));
17439 else
17440 {
17441 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17442 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17443#ifdef FEAT_VIRTUALEDIT
17444 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17445#endif
17446 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017447 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017448
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017449 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017450#ifdef FEAT_DIFF
17451 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17452#endif
17453 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17454 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17455
17456 check_cursor();
17457 changed_cline_bef_curs();
17458 invalidate_botline();
17459 redraw_later(VALID);
17460
17461 if (curwin->w_topline == 0)
17462 curwin->w_topline = 1;
17463 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17464 curwin->w_topline = curbuf->b_ml.ml_line_count;
17465#ifdef FEAT_DIFF
17466 check_topfill(curwin, TRUE);
17467#endif
17468 }
17469}
17470
17471/*
17472 * "winsaveview()" function
17473 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017474 static void
17475f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017476 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017477 typval_T *rettv;
17478{
17479 dict_T *dict;
17480
17481 dict = dict_alloc();
17482 if (dict == NULL)
17483 return;
17484 rettv->v_type = VAR_DICT;
17485 rettv->vval.v_dict = dict;
17486 ++dict->dv_refcount;
17487
17488 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17489 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17490#ifdef FEAT_VIRTUALEDIT
17491 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17492#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017493 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017494 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17495
17496 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17497#ifdef FEAT_DIFF
17498 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17499#endif
17500 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17501 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17502}
17503
17504/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505 * "winwidth(nr)" function
17506 */
17507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017508f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017509 typval_T *argvars;
17510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511{
17512 win_T *wp;
17513
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017514 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017516 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517 else
17518#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017519 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017521 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522#endif
17523}
17524
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017526 * "writefile()" function
17527 */
17528 static void
17529f_writefile(argvars, rettv)
17530 typval_T *argvars;
17531 typval_T *rettv;
17532{
17533 int binary = FALSE;
17534 char_u *fname;
17535 FILE *fd;
17536 listitem_T *li;
17537 char_u *s;
17538 int ret = 0;
17539 int c;
17540
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017541 if (check_restricted() || check_secure())
17542 return;
17543
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017544 if (argvars[0].v_type != VAR_LIST)
17545 {
17546 EMSG2(_(e_listarg), "writefile()");
17547 return;
17548 }
17549 if (argvars[0].vval.v_list == NULL)
17550 return;
17551
17552 if (argvars[2].v_type != VAR_UNKNOWN
17553 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17554 binary = TRUE;
17555
17556 /* Always open the file in binary mode, library functions have a mind of
17557 * their own about CR-LF conversion. */
17558 fname = get_tv_string(&argvars[1]);
17559 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17560 {
17561 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17562 ret = -1;
17563 }
17564 else
17565 {
17566 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17567 li = li->li_next)
17568 {
17569 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17570 {
17571 if (*s == '\n')
17572 c = putc(NUL, fd);
17573 else
17574 c = putc(*s, fd);
17575 if (c == EOF)
17576 {
17577 ret = -1;
17578 break;
17579 }
17580 }
17581 if (!binary || li->li_next != NULL)
17582 if (putc('\n', fd) == EOF)
17583 {
17584 ret = -1;
17585 break;
17586 }
17587 if (ret < 0)
17588 {
17589 EMSG(_(e_write));
17590 break;
17591 }
17592 }
17593 fclose(fd);
17594 }
17595
17596 rettv->vval.v_number = ret;
17597}
17598
17599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017601 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602 */
17603 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017604var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017605 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017606 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017607 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017609 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017611 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612
Bram Moolenaara5525202006-03-02 22:52:09 +000017613 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017614 if (varp->v_type == VAR_LIST)
17615 {
17616 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017617 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000017618 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017619 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017620
17621 l = varp->vval.v_list;
17622 if (l == NULL)
17623 return NULL;
17624
17625 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017626 pos.lnum = list_find_nr(l, 0L, &error);
17627 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017628 return NULL; /* invalid line number */
17629
17630 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017631 pos.col = list_find_nr(l, 1L, &error);
17632 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017633 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017634 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000017635
17636 /* We accept "$" for the column number: last column. */
17637 li = list_find(l, 1L);
17638 if (li != NULL && li->li_tv.v_type == VAR_STRING
17639 && li->li_tv.vval.v_string != NULL
17640 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
17641 pos.col = len + 1;
17642
Bram Moolenaara5525202006-03-02 22:52:09 +000017643 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000017644 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017645 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017646 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017647
Bram Moolenaara5525202006-03-02 22:52:09 +000017648#ifdef FEAT_VIRTUALEDIT
17649 /* Get the virtual offset. Defaults to zero. */
17650 pos.coladd = list_find_nr(l, 2L, &error);
17651 if (error)
17652 pos.coladd = 0;
17653#endif
17654
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017655 return &pos;
17656 }
17657
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017658 name = get_tv_string_chk(varp);
17659 if (name == NULL)
17660 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017661 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017663#ifdef FEAT_VISUAL
17664 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
17665 {
17666 if (VIsual_active)
17667 return &VIsual;
17668 return &curwin->w_cursor;
17669 }
17670#endif
17671 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017673 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
17675 return NULL;
17676 return pp;
17677 }
Bram Moolenaara5525202006-03-02 22:52:09 +000017678
17679#ifdef FEAT_VIRTUALEDIT
17680 pos.coladd = 0;
17681#endif
17682
Bram Moolenaar477933c2007-07-17 14:32:23 +000017683 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017684 {
17685 pos.col = 0;
17686 if (name[1] == '0') /* "w0": first visible line */
17687 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017688 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017689 pos.lnum = curwin->w_topline;
17690 return &pos;
17691 }
17692 else if (name[1] == '$') /* "w$": last visible line */
17693 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017694 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017695 pos.lnum = curwin->w_botline - 1;
17696 return &pos;
17697 }
17698 }
17699 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000017701 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702 {
17703 pos.lnum = curbuf->b_ml.ml_line_count;
17704 pos.col = 0;
17705 }
17706 else
17707 {
17708 pos.lnum = curwin->w_cursor.lnum;
17709 pos.col = (colnr_T)STRLEN(ml_get_curline());
17710 }
17711 return &pos;
17712 }
17713 return NULL;
17714}
17715
17716/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017717 * Convert list in "arg" into a position and optional file number.
17718 * When "fnump" is NULL there is no file number, only 3 items.
17719 * Note that the column is passed on as-is, the caller may want to decrement
17720 * it to use 1 for the first column.
17721 * Return FAIL when conversion is not possible, doesn't check the position for
17722 * validity.
17723 */
17724 static int
17725list2fpos(arg, posp, fnump)
17726 typval_T *arg;
17727 pos_T *posp;
17728 int *fnump;
17729{
17730 list_T *l = arg->vval.v_list;
17731 long i = 0;
17732 long n;
17733
Bram Moolenaarbde35262006-07-23 20:12:24 +000017734 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17735 * when "fnump" isn't NULL and "coladd" is optional. */
17736 if (arg->v_type != VAR_LIST
17737 || l == NULL
17738 || l->lv_len < (fnump == NULL ? 2 : 3)
17739 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017740 return FAIL;
17741
17742 if (fnump != NULL)
17743 {
17744 n = list_find_nr(l, i++, NULL); /* fnum */
17745 if (n < 0)
17746 return FAIL;
17747 if (n == 0)
17748 n = curbuf->b_fnum; /* current buffer */
17749 *fnump = n;
17750 }
17751
17752 n = list_find_nr(l, i++, NULL); /* lnum */
17753 if (n < 0)
17754 return FAIL;
17755 posp->lnum = n;
17756
17757 n = list_find_nr(l, i++, NULL); /* col */
17758 if (n < 0)
17759 return FAIL;
17760 posp->col = n;
17761
17762#ifdef FEAT_VIRTUALEDIT
17763 n = list_find_nr(l, i, NULL);
17764 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000017765 posp->coladd = 0;
17766 else
17767 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017768#endif
17769
17770 return OK;
17771}
17772
17773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774 * Get the length of an environment variable name.
17775 * Advance "arg" to the first character after the name.
17776 * Return 0 for error.
17777 */
17778 static int
17779get_env_len(arg)
17780 char_u **arg;
17781{
17782 char_u *p;
17783 int len;
17784
17785 for (p = *arg; vim_isIDc(*p); ++p)
17786 ;
17787 if (p == *arg) /* no name found */
17788 return 0;
17789
17790 len = (int)(p - *arg);
17791 *arg = p;
17792 return len;
17793}
17794
17795/*
17796 * Get the length of the name of a function or internal variable.
17797 * "arg" is advanced to the first non-white character after the name.
17798 * Return 0 if something is wrong.
17799 */
17800 static int
17801get_id_len(arg)
17802 char_u **arg;
17803{
17804 char_u *p;
17805 int len;
17806
17807 /* Find the end of the name. */
17808 for (p = *arg; eval_isnamec(*p); ++p)
17809 ;
17810 if (p == *arg) /* no name found */
17811 return 0;
17812
17813 len = (int)(p - *arg);
17814 *arg = skipwhite(p);
17815
17816 return len;
17817}
17818
17819/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017820 * Get the length of the name of a variable or function.
17821 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017822 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017823 * Return -1 if curly braces expansion failed.
17824 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017825 * If the name contains 'magic' {}'s, expand them and return the
17826 * expanded name in an allocated string via 'alias' - caller must free.
17827 */
17828 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017829get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830 char_u **arg;
17831 char_u **alias;
17832 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017833 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834{
17835 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017836 char_u *p;
17837 char_u *expr_start;
17838 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839
17840 *alias = NULL; /* default to no alias */
17841
17842 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17843 && (*arg)[2] == (int)KE_SNR)
17844 {
17845 /* hard coded <SNR>, already translated */
17846 *arg += 3;
17847 return get_id_len(arg) + 3;
17848 }
17849 len = eval_fname_script(*arg);
17850 if (len > 0)
17851 {
17852 /* literal "<SID>", "s:" or "<SNR>" */
17853 *arg += len;
17854 }
17855
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017857 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017859 p = find_name_end(*arg, &expr_start, &expr_end,
17860 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861 if (expr_start != NULL)
17862 {
17863 char_u *temp_string;
17864
17865 if (!evaluate)
17866 {
17867 len += (int)(p - *arg);
17868 *arg = skipwhite(p);
17869 return len;
17870 }
17871
17872 /*
17873 * Include any <SID> etc in the expanded string:
17874 * Thus the -len here.
17875 */
17876 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17877 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017878 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879 *alias = temp_string;
17880 *arg = skipwhite(p);
17881 return (int)STRLEN(temp_string);
17882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883
17884 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017885 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886 EMSG2(_(e_invexpr2), *arg);
17887
17888 return len;
17889}
17890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017891/*
17892 * Find the end of a variable or function name, taking care of magic braces.
17893 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17894 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017895 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017896 * Return a pointer to just after the name. Equal to "arg" if there is no
17897 * valid name.
17898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017900find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901 char_u *arg;
17902 char_u **expr_start;
17903 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017904 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017906 int mb_nest = 0;
17907 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908 char_u *p;
17909
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017910 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017912 *expr_start = NULL;
17913 *expr_end = NULL;
17914 }
17915
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017916 /* Quick check for valid starting character. */
17917 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17918 return arg;
17919
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017920 for (p = arg; *p != NUL
17921 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017922 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017923 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017924 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017925 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017926 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017927 if (*p == '\'')
17928 {
17929 /* skip over 'string' to avoid counting [ and ] inside it. */
17930 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17931 ;
17932 if (*p == NUL)
17933 break;
17934 }
17935 else if (*p == '"')
17936 {
17937 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17938 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17939 if (*p == '\\' && p[1] != NUL)
17940 ++p;
17941 if (*p == NUL)
17942 break;
17943 }
17944
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017945 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017946 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017947 if (*p == '[')
17948 ++br_nest;
17949 else if (*p == ']')
17950 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017953 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017955 if (*p == '{')
17956 {
17957 mb_nest++;
17958 if (expr_start != NULL && *expr_start == NULL)
17959 *expr_start = p;
17960 }
17961 else if (*p == '}')
17962 {
17963 mb_nest--;
17964 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17965 *expr_end = p;
17966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968 }
17969
17970 return p;
17971}
17972
17973/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017974 * Expands out the 'magic' {}'s in a variable/function name.
17975 * Note that this can call itself recursively, to deal with
17976 * constructs like foo{bar}{baz}{bam}
17977 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17978 * "in_start" ^
17979 * "expr_start" ^
17980 * "expr_end" ^
17981 * "in_end" ^
17982 *
17983 * Returns a new allocated string, which the caller must free.
17984 * Returns NULL for failure.
17985 */
17986 static char_u *
17987make_expanded_name(in_start, expr_start, expr_end, in_end)
17988 char_u *in_start;
17989 char_u *expr_start;
17990 char_u *expr_end;
17991 char_u *in_end;
17992{
17993 char_u c1;
17994 char_u *retval = NULL;
17995 char_u *temp_result;
17996 char_u *nextcmd = NULL;
17997
17998 if (expr_end == NULL || in_end == NULL)
17999 return NULL;
18000 *expr_start = NUL;
18001 *expr_end = NUL;
18002 c1 = *in_end;
18003 *in_end = NUL;
18004
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018005 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018006 if (temp_result != NULL && nextcmd == NULL)
18007 {
18008 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18009 + (in_end - expr_end) + 1));
18010 if (retval != NULL)
18011 {
18012 STRCPY(retval, in_start);
18013 STRCAT(retval, temp_result);
18014 STRCAT(retval, expr_end + 1);
18015 }
18016 }
18017 vim_free(temp_result);
18018
18019 *in_end = c1; /* put char back for error messages */
18020 *expr_start = '{';
18021 *expr_end = '}';
18022
18023 if (retval != NULL)
18024 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018025 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018026 if (expr_start != NULL)
18027 {
18028 /* Further expansion! */
18029 temp_result = make_expanded_name(retval, expr_start,
18030 expr_end, temp_result);
18031 vim_free(retval);
18032 retval = temp_result;
18033 }
18034 }
18035
18036 return retval;
18037}
18038
18039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018041 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042 */
18043 static int
18044eval_isnamec(c)
18045 int c;
18046{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018047 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18048}
18049
18050/*
18051 * Return TRUE if character "c" can be used as the first character in a
18052 * variable or function name (excluding '{' and '}').
18053 */
18054 static int
18055eval_isnamec1(c)
18056 int c;
18057{
18058 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059}
18060
18061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 * Set number v: variable to "val".
18063 */
18064 void
18065set_vim_var_nr(idx, val)
18066 int idx;
18067 long val;
18068{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018069 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070}
18071
18072/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018073 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 */
18075 long
18076get_vim_var_nr(idx)
18077 int idx;
18078{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018079 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080}
18081
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018082/*
18083 * Get string v: variable value. Uses a static buffer, can only be used once.
18084 */
18085 char_u *
18086get_vim_var_str(idx)
18087 int idx;
18088{
18089 return get_tv_string(&vimvars[idx].vv_tv);
18090}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018091
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018093 * Get List v: variable value. Caller must take care of reference count when
18094 * needed.
18095 */
18096 list_T *
18097get_vim_var_list(idx)
18098 int idx;
18099{
18100 return vimvars[idx].vv_list;
18101}
18102
18103/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018104 * Set v:char to character "c".
18105 */
18106 void
18107set_vim_var_char(c)
18108 int c;
18109{
18110#ifdef FEAT_MBYTE
18111 char_u buf[MB_MAXBYTES];
18112#else
18113 char_u buf[2];
18114#endif
18115
18116#ifdef FEAT_MBYTE
18117 if (has_mbyte)
18118 buf[(*mb_char2bytes)(c, buf)] = NUL;
18119 else
18120#endif
18121 {
18122 buf[0] = c;
18123 buf[1] = NUL;
18124 }
18125 set_vim_var_string(VV_CHAR, buf, -1);
18126}
18127
18128/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018129 * Set v:count to "count" and v:count1 to "count1".
18130 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 */
18132 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018133set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134 long count;
18135 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018136 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018138 if (set_prevcount)
18139 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018140 vimvars[VV_COUNT].vv_nr = count;
18141 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142}
18143
18144/*
18145 * Set string v: variable to a copy of "val".
18146 */
18147 void
18148set_vim_var_string(idx, val, len)
18149 int idx;
18150 char_u *val;
18151 int len; /* length of "val" to use or -1 (whole string) */
18152{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018153 /* Need to do this (at least) once, since we can't initialize a union.
18154 * Will always be invoked when "v:progname" is set. */
18155 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18156
Bram Moolenaare9a41262005-01-15 22:18:47 +000018157 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018159 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018161 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018163 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164}
18165
18166/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018167 * Set List v: variable to "val".
18168 */
18169 void
18170set_vim_var_list(idx, val)
18171 int idx;
18172 list_T *val;
18173{
18174 list_unref(vimvars[idx].vv_list);
18175 vimvars[idx].vv_list = val;
18176 if (val != NULL)
18177 ++val->lv_refcount;
18178}
18179
18180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 * Set v:register if needed.
18182 */
18183 void
18184set_reg_var(c)
18185 int c;
18186{
18187 char_u regname;
18188
18189 if (c == 0 || c == ' ')
18190 regname = '"';
18191 else
18192 regname = c;
18193 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018194 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195 set_vim_var_string(VV_REG, &regname, 1);
18196}
18197
18198/*
18199 * Get or set v:exception. If "oldval" == NULL, return the current value.
18200 * Otherwise, restore the value to "oldval" and return NULL.
18201 * Must always be called in pairs to save and restore v:exception! Does not
18202 * take care of memory allocations.
18203 */
18204 char_u *
18205v_exception(oldval)
18206 char_u *oldval;
18207{
18208 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018209 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018210
Bram Moolenaare9a41262005-01-15 22:18:47 +000018211 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 return NULL;
18213}
18214
18215/*
18216 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18217 * Otherwise, restore the value to "oldval" and return NULL.
18218 * Must always be called in pairs to save and restore v:throwpoint! Does not
18219 * take care of memory allocations.
18220 */
18221 char_u *
18222v_throwpoint(oldval)
18223 char_u *oldval;
18224{
18225 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018226 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227
Bram Moolenaare9a41262005-01-15 22:18:47 +000018228 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 return NULL;
18230}
18231
18232#if defined(FEAT_AUTOCMD) || defined(PROTO)
18233/*
18234 * Set v:cmdarg.
18235 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18236 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18237 * Must always be called in pairs!
18238 */
18239 char_u *
18240set_cmdarg(eap, oldarg)
18241 exarg_T *eap;
18242 char_u *oldarg;
18243{
18244 char_u *oldval;
18245 char_u *newval;
18246 unsigned len;
18247
Bram Moolenaare9a41262005-01-15 22:18:47 +000018248 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018249 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018251 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018252 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018253 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254 }
18255
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018256 if (eap->force_bin == FORCE_BIN)
18257 len = 6;
18258 else if (eap->force_bin == FORCE_NOBIN)
18259 len = 8;
18260 else
18261 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018262
18263 if (eap->read_edit)
18264 len += 7;
18265
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018266 if (eap->force_ff != 0)
18267 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18268# ifdef FEAT_MBYTE
18269 if (eap->force_enc != 0)
18270 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018271 if (eap->bad_char != 0)
18272 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018273# endif
18274
18275 newval = alloc(len + 1);
18276 if (newval == NULL)
18277 return NULL;
18278
18279 if (eap->force_bin == FORCE_BIN)
18280 sprintf((char *)newval, " ++bin");
18281 else if (eap->force_bin == FORCE_NOBIN)
18282 sprintf((char *)newval, " ++nobin");
18283 else
18284 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018285
18286 if (eap->read_edit)
18287 STRCAT(newval, " ++edit");
18288
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018289 if (eap->force_ff != 0)
18290 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18291 eap->cmd + eap->force_ff);
18292# ifdef FEAT_MBYTE
18293 if (eap->force_enc != 0)
18294 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18295 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018296 if (eap->bad_char != 0)
18297 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
18298 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018299# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018300 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018301 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018302}
18303#endif
18304
18305/*
18306 * Get the value of internal variable "name".
18307 * Return OK or FAIL.
18308 */
18309 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018310get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311 char_u *name;
18312 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018313 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018314 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315{
18316 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018317 typval_T *tv = NULL;
18318 typval_T atv;
18319 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321
18322 /* truncate the name, so that we can use strcmp() */
18323 cc = name[len];
18324 name[len] = NUL;
18325
18326 /*
18327 * Check for "b:changedtick".
18328 */
18329 if (STRCMP(name, "b:changedtick") == 0)
18330 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018331 atv.v_type = VAR_NUMBER;
18332 atv.vval.v_number = curbuf->b_changedtick;
18333 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334 }
18335
18336 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337 * Check for user-defined variables.
18338 */
18339 else
18340 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018341 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018343 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018344 }
18345
Bram Moolenaare9a41262005-01-15 22:18:47 +000018346 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018348 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018349 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350 ret = FAIL;
18351 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018352 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018353 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354
18355 name[len] = cc;
18356
18357 return ret;
18358}
18359
18360/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018361 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18362 * Also handle function call with Funcref variable: func(expr)
18363 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18364 */
18365 static int
18366handle_subscript(arg, rettv, evaluate, verbose)
18367 char_u **arg;
18368 typval_T *rettv;
18369 int evaluate; /* do more than finding the end */
18370 int verbose; /* give error messages */
18371{
18372 int ret = OK;
18373 dict_T *selfdict = NULL;
18374 char_u *s;
18375 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018376 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018377
18378 while (ret == OK
18379 && (**arg == '['
18380 || (**arg == '.' && rettv->v_type == VAR_DICT)
18381 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18382 && !vim_iswhite(*(*arg - 1)))
18383 {
18384 if (**arg == '(')
18385 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018386 /* need to copy the funcref so that we can clear rettv */
18387 functv = *rettv;
18388 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018389
18390 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018391 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018392 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018393 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18394 &len, evaluate, selfdict);
18395
18396 /* Clear the funcref afterwards, so that deleting it while
18397 * evaluating the arguments is possible (see test55). */
18398 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018399
18400 /* Stop the expression evaluation when immediately aborting on
18401 * error, or when an interrupt occurred or an exception was thrown
18402 * but not caught. */
18403 if (aborting())
18404 {
18405 if (ret == OK)
18406 clear_tv(rettv);
18407 ret = FAIL;
18408 }
18409 dict_unref(selfdict);
18410 selfdict = NULL;
18411 }
18412 else /* **arg == '[' || **arg == '.' */
18413 {
18414 dict_unref(selfdict);
18415 if (rettv->v_type == VAR_DICT)
18416 {
18417 selfdict = rettv->vval.v_dict;
18418 if (selfdict != NULL)
18419 ++selfdict->dv_refcount;
18420 }
18421 else
18422 selfdict = NULL;
18423 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18424 {
18425 clear_tv(rettv);
18426 ret = FAIL;
18427 }
18428 }
18429 }
18430 dict_unref(selfdict);
18431 return ret;
18432}
18433
18434/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018435 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018436 * value).
18437 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018438 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018439alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018440{
Bram Moolenaar33570922005-01-25 22:26:29 +000018441 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018442}
18443
18444/*
18445 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446 * The string "s" must have been allocated, it is consumed.
18447 * Return NULL for out of memory, the variable otherwise.
18448 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018449 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018450alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451 char_u *s;
18452{
Bram Moolenaar33570922005-01-25 22:26:29 +000018453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018455 rettv = alloc_tv();
18456 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018458 rettv->v_type = VAR_STRING;
18459 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460 }
18461 else
18462 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018463 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464}
18465
18466/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018467 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018469 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018470free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018471 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472{
18473 if (varp != NULL)
18474 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018475 switch (varp->v_type)
18476 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018477 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018478 func_unref(varp->vval.v_string);
18479 /*FALLTHROUGH*/
18480 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018481 vim_free(varp->vval.v_string);
18482 break;
18483 case VAR_LIST:
18484 list_unref(varp->vval.v_list);
18485 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018486 case VAR_DICT:
18487 dict_unref(varp->vval.v_dict);
18488 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018489 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018490#ifdef FEAT_FLOAT
18491 case VAR_FLOAT:
18492#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018493 case VAR_UNKNOWN:
18494 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018495 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018496 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018497 break;
18498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499 vim_free(varp);
18500 }
18501}
18502
18503/*
18504 * Free the memory for a variable value and set the value to NULL or 0.
18505 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018506 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018507clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018508 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018509{
18510 if (varp != NULL)
18511 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018512 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018513 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018514 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018515 func_unref(varp->vval.v_string);
18516 /*FALLTHROUGH*/
18517 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018518 vim_free(varp->vval.v_string);
18519 varp->vval.v_string = NULL;
18520 break;
18521 case VAR_LIST:
18522 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018523 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018524 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018525 case VAR_DICT:
18526 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018527 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018528 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018529 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018530 varp->vval.v_number = 0;
18531 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018532#ifdef FEAT_FLOAT
18533 case VAR_FLOAT:
18534 varp->vval.v_float = 0.0;
18535 break;
18536#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018537 case VAR_UNKNOWN:
18538 break;
18539 default:
18540 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018542 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018543 }
18544}
18545
18546/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018547 * Set the value of a variable to NULL without freeing items.
18548 */
18549 static void
18550init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018551 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018552{
18553 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018554 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018555}
18556
18557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558 * Get the number value of a variable.
18559 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018560 * For incompatible types, return 0.
18561 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18562 * caller of incompatible types: it sets *denote to TRUE if "denote"
18563 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564 */
18565 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018566get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018567 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018569 int error = FALSE;
18570
18571 return get_tv_number_chk(varp, &error); /* return 0L on error */
18572}
18573
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018574 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018575get_tv_number_chk(varp, denote)
18576 typval_T *varp;
18577 int *denote;
18578{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018579 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018581 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018583 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018584 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018585#ifdef FEAT_FLOAT
18586 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018587 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018588 break;
18589#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018590 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018591 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018592 break;
18593 case VAR_STRING:
18594 if (varp->vval.v_string != NULL)
18595 vim_str2nr(varp->vval.v_string, NULL, NULL,
18596 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018597 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018598 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018599 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018600 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018601 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018602 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018603 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018604 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018605 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018606 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018608 if (denote == NULL) /* useful for values that must be unsigned */
18609 n = -1;
18610 else
18611 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018612 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018613}
18614
18615/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018616 * Get the lnum from the first argument.
18617 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018618 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619 */
18620 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018621get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000018622 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623{
Bram Moolenaar33570922005-01-25 22:26:29 +000018624 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 linenr_T lnum;
18626
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018627 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628 if (lnum == 0) /* no valid number, try using line() */
18629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018630 rettv.v_type = VAR_NUMBER;
18631 f_line(argvars, &rettv);
18632 lnum = rettv.vval.v_number;
18633 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634 }
18635 return lnum;
18636}
18637
18638/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018639 * Get the lnum from the first argument.
18640 * Also accepts "$", then "buf" is used.
18641 * Returns 0 on error.
18642 */
18643 static linenr_T
18644get_tv_lnum_buf(argvars, buf)
18645 typval_T *argvars;
18646 buf_T *buf;
18647{
18648 if (argvars[0].v_type == VAR_STRING
18649 && argvars[0].vval.v_string != NULL
18650 && argvars[0].vval.v_string[0] == '$'
18651 && buf != NULL)
18652 return buf->b_ml.ml_line_count;
18653 return get_tv_number_chk(&argvars[0], NULL);
18654}
18655
18656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 * Get the string value of a variable.
18658 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000018659 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18660 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661 * If the String variable has never been set, return an empty string.
18662 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018663 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18664 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 */
18666 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018667get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018668 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669{
18670 static char_u mybuf[NUMBUFLEN];
18671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018672 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673}
18674
18675 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018676get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000018677 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018678 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018680 char_u *res = get_tv_string_buf_chk(varp, buf);
18681
18682 return res != NULL ? res : (char_u *)"";
18683}
18684
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018685 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018686get_tv_string_chk(varp)
18687 typval_T *varp;
18688{
18689 static char_u mybuf[NUMBUFLEN];
18690
18691 return get_tv_string_buf_chk(varp, mybuf);
18692}
18693
18694 static char_u *
18695get_tv_string_buf_chk(varp, buf)
18696 typval_T *varp;
18697 char_u *buf;
18698{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018699 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018701 case VAR_NUMBER:
18702 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
18703 return buf;
18704 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018705 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018706 break;
18707 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018708 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000018709 break;
18710 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018711 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018712 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018713#ifdef FEAT_FLOAT
18714 case VAR_FLOAT:
18715 EMSG(_("E806: using Float as a String"));
18716 break;
18717#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018718 case VAR_STRING:
18719 if (varp->vval.v_string != NULL)
18720 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018721 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018722 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018723 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018724 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018726 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727}
18728
18729/*
18730 * Find variable "name" in the list of variables.
18731 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018732 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018733 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000018734 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018736 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018737find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018739 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018742 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743
Bram Moolenaara7043832005-01-21 11:56:39 +000018744 ht = find_var_ht(name, &varname);
18745 if (htp != NULL)
18746 *htp = ht;
18747 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018749 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750}
18751
18752/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018753 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000018754 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018756 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018757find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000018758 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000018759 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018760 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000018761{
Bram Moolenaar33570922005-01-25 22:26:29 +000018762 hashitem_T *hi;
18763
18764 if (*varname == NUL)
18765 {
18766 /* Must be something like "s:", otherwise "ht" would be NULL. */
18767 switch (varname[-2])
18768 {
18769 case 's': return &SCRIPT_SV(current_SID).sv_var;
18770 case 'g': return &globvars_var;
18771 case 'v': return &vimvars_var;
18772 case 'b': return &curbuf->b_bufvar;
18773 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018774#ifdef FEAT_WINDOWS
18775 case 't': return &curtab->tp_winvar;
18776#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018777 case 'l': return current_funccal == NULL
18778 ? NULL : &current_funccal->l_vars_var;
18779 case 'a': return current_funccal == NULL
18780 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000018781 }
18782 return NULL;
18783 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018784
18785 hi = hash_find(ht, varname);
18786 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018787 {
18788 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018789 * worked find the variable again. Don't auto-load a script if it was
18790 * loaded already, otherwise it would be loaded every time when
18791 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018792 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018793 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018794 hi = hash_find(ht, varname);
18795 if (HASHITEM_EMPTY(hi))
18796 return NULL;
18797 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018798 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018799}
18800
18801/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018802 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018803 * Set "varname" to the start of name without ':'.
18804 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018805 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018806find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807 char_u *name;
18808 char_u **varname;
18809{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018810 hashitem_T *hi;
18811
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 if (name[1] != ':')
18813 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018814 /* The name must not start with a colon or #. */
18815 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816 return NULL;
18817 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018818
18819 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018820 hi = hash_find(&compat_hashtab, name);
18821 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000018822 return &compat_hashtab;
18823
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018825 return &globvarht; /* global variable */
18826 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827 }
18828 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018829 if (*name == 'g') /* global variable */
18830 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018831 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18832 */
18833 if (vim_strchr(name + 2, ':') != NULL
18834 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018835 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018837 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018839 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018840#ifdef FEAT_WINDOWS
18841 if (*name == 't') /* tab page variable */
18842 return &curtab->tp_vars.dv_hashtab;
18843#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000018844 if (*name == 'v') /* v: variable */
18845 return &vimvarht;
18846 if (*name == 'a' && current_funccal != NULL) /* function argument */
18847 return &current_funccal->l_avars.dv_hashtab;
18848 if (*name == 'l' && current_funccal != NULL) /* local function variable */
18849 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850 if (*name == 's' /* script variable */
18851 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
18852 return &SCRIPT_VARS(current_SID);
18853 return NULL;
18854}
18855
18856/*
18857 * Get the string value of a (global/local) variable.
18858 * Returns NULL when it doesn't exist.
18859 */
18860 char_u *
18861get_var_value(name)
18862 char_u *name;
18863{
Bram Moolenaar33570922005-01-25 22:26:29 +000018864 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865
Bram Moolenaara7043832005-01-21 11:56:39 +000018866 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867 if (v == NULL)
18868 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018869 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870}
18871
18872/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018873 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874 * sourcing this script and when executing functions defined in the script.
18875 */
18876 void
18877new_script_vars(id)
18878 scid_T id;
18879{
Bram Moolenaara7043832005-01-21 11:56:39 +000018880 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018881 hashtab_T *ht;
18882 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018883
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18885 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018886 /* Re-allocating ga_data means that an ht_array pointing to
18887 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018888 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018889 for (i = 1; i <= ga_scripts.ga_len; ++i)
18890 {
18891 ht = &SCRIPT_VARS(i);
18892 if (ht->ht_mask == HT_INIT_SIZE - 1)
18893 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018894 sv = &SCRIPT_SV(i);
18895 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018896 }
18897
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 while (ga_scripts.ga_len < id)
18899 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018900 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18901 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903 }
18904 }
18905}
18906
18907/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018908 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18909 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910 */
18911 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018912init_var_dict(dict, dict_var)
18913 dict_T *dict;
18914 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915{
Bram Moolenaar33570922005-01-25 22:26:29 +000018916 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000018917 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000018918 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018919 dict_var->di_tv.vval.v_dict = dict;
18920 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018921 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018922 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18923 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924}
18925
18926/*
18927 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018928 * Frees all allocated variables and the value they contain.
18929 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018930 */
18931 void
Bram Moolenaara7043832005-01-21 11:56:39 +000018932vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000018933 hashtab_T *ht;
18934{
18935 vars_clear_ext(ht, TRUE);
18936}
18937
18938/*
18939 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18940 */
18941 static void
18942vars_clear_ext(ht, free_val)
18943 hashtab_T *ht;
18944 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945{
Bram Moolenaara7043832005-01-21 11:56:39 +000018946 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018947 hashitem_T *hi;
18948 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949
Bram Moolenaar33570922005-01-25 22:26:29 +000018950 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018951 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000018952 for (hi = ht->ht_array; todo > 0; ++hi)
18953 {
18954 if (!HASHITEM_EMPTY(hi))
18955 {
18956 --todo;
18957
Bram Moolenaar33570922005-01-25 22:26:29 +000018958 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018959 * ht_array might change then. hash_clear() takes care of it
18960 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018961 v = HI2DI(hi);
18962 if (free_val)
18963 clear_tv(&v->di_tv);
18964 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18965 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018966 }
18967 }
18968 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018969 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970}
18971
Bram Moolenaara7043832005-01-21 11:56:39 +000018972/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018973 * Delete a variable from hashtab "ht" at item "hi".
18974 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018977delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018978 hashtab_T *ht;
18979 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980{
Bram Moolenaar33570922005-01-25 22:26:29 +000018981 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018982
18983 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018984 clear_tv(&di->di_tv);
18985 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986}
18987
18988/*
18989 * List the value of one internal variable.
18990 */
18991 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018992list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018993 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018995 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018997 char_u *tofree;
18998 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018999 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019000
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019001 current_copyID += COPYID_INC;
19002 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019003 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019004 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019005 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006}
19007
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019009list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019010 char_u *prefix;
19011 char_u *name;
19012 int type;
19013 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019014 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015{
Bram Moolenaar31859182007-08-14 20:41:13 +000019016 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19017 msg_start();
19018 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019 if (name != NULL) /* "a:" vars don't have a name stored */
19020 msg_puts(name);
19021 msg_putchar(' ');
19022 msg_advance(22);
19023 if (type == VAR_NUMBER)
19024 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019025 else if (type == VAR_FUNC)
19026 msg_putchar('*');
19027 else if (type == VAR_LIST)
19028 {
19029 msg_putchar('[');
19030 if (*string == '[')
19031 ++string;
19032 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019033 else if (type == VAR_DICT)
19034 {
19035 msg_putchar('{');
19036 if (*string == '{')
19037 ++string;
19038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039 else
19040 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019041
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019043
19044 if (type == VAR_FUNC)
19045 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019046 if (*first)
19047 {
19048 msg_clr_eos();
19049 *first = FALSE;
19050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051}
19052
19053/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019054 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 * If the variable already exists, the value is updated.
19056 * Otherwise the variable is created.
19057 */
19058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019059set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019061 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019062 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063{
Bram Moolenaar33570922005-01-25 22:26:29 +000019064 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019066 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019067 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019068
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019069 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019070 {
19071 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19072 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19073 ? name[2] : name[0]))
19074 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019075 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019076 return;
19077 }
19078 if (function_exists(name))
19079 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019080 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019081 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019082 return;
19083 }
19084 }
19085
Bram Moolenaara7043832005-01-21 11:56:39 +000019086 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019087 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000019088 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000019089 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000019090 return;
19091 }
19092
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019093 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000019094 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019096 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019097 if (var_check_ro(v->di_flags, name)
19098 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019099 return;
19100 if (v->di_tv.v_type != tv->v_type
19101 && !((v->di_tv.v_type == VAR_STRING
19102 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019103 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019104 || tv->v_type == VAR_NUMBER))
19105#ifdef FEAT_FLOAT
19106 && !((v->di_tv.v_type == VAR_NUMBER
19107 || v->di_tv.v_type == VAR_FLOAT)
19108 && (tv->v_type == VAR_NUMBER
19109 || tv->v_type == VAR_FLOAT))
19110#endif
19111 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019112 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019113 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019114 return;
19115 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019116
19117 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019118 * Handle setting internal v: variables separately: we don't change
19119 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019120 */
19121 if (ht == &vimvarht)
19122 {
19123 if (v->di_tv.v_type == VAR_STRING)
19124 {
19125 vim_free(v->di_tv.vval.v_string);
19126 if (copy || tv->v_type != VAR_STRING)
19127 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19128 else
19129 {
19130 /* Take over the string to avoid an extra alloc/free. */
19131 v->di_tv.vval.v_string = tv->vval.v_string;
19132 tv->vval.v_string = NULL;
19133 }
19134 }
19135 else if (v->di_tv.v_type != VAR_NUMBER)
19136 EMSG2(_(e_intern2), "set_var()");
19137 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019138 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019139 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019140 if (STRCMP(varname, "searchforward") == 0)
19141 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19142 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019143 return;
19144 }
19145
19146 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147 }
19148 else /* add a new variable */
19149 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019150 /* Can't add "v:" variable. */
19151 if (ht == &vimvarht)
19152 {
19153 EMSG2(_(e_illvar), name);
19154 return;
19155 }
19156
Bram Moolenaar92124a32005-06-17 22:03:40 +000019157 /* Make sure the variable name is valid. */
19158 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019159 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19160 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019161 {
19162 EMSG2(_(e_illvar), varname);
19163 return;
19164 }
19165
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019166 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19167 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019168 if (v == NULL)
19169 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019170 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019171 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019173 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174 return;
19175 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019176 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019178
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019179 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019180 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019181 else
19182 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019183 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019184 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019185 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187}
19188
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019189/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019190 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019191 * Also give an error message.
19192 */
19193 static int
19194var_check_ro(flags, name)
19195 int flags;
19196 char_u *name;
19197{
19198 if (flags & DI_FLAGS_RO)
19199 {
19200 EMSG2(_(e_readonlyvar), name);
19201 return TRUE;
19202 }
19203 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19204 {
19205 EMSG2(_(e_readonlysbx), name);
19206 return TRUE;
19207 }
19208 return FALSE;
19209}
19210
19211/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019212 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19213 * Also give an error message.
19214 */
19215 static int
19216var_check_fixed(flags, name)
19217 int flags;
19218 char_u *name;
19219{
19220 if (flags & DI_FLAGS_FIX)
19221 {
19222 EMSG2(_("E795: Cannot delete variable %s"), name);
19223 return TRUE;
19224 }
19225 return FALSE;
19226}
19227
19228/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019229 * Return TRUE if typeval "tv" is set to be locked (immutable).
19230 * Also give an error message, using "name".
19231 */
19232 static int
19233tv_check_lock(lock, name)
19234 int lock;
19235 char_u *name;
19236{
19237 if (lock & VAR_LOCKED)
19238 {
19239 EMSG2(_("E741: Value is locked: %s"),
19240 name == NULL ? (char_u *)_("Unknown") : name);
19241 return TRUE;
19242 }
19243 if (lock & VAR_FIXED)
19244 {
19245 EMSG2(_("E742: Cannot change value of %s"),
19246 name == NULL ? (char_u *)_("Unknown") : name);
19247 return TRUE;
19248 }
19249 return FALSE;
19250}
19251
19252/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019253 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019254 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019255 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019256 * It is OK for "from" and "to" to point to the same item. This is used to
19257 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019260copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019261 typval_T *from;
19262 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019263{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019264 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019265 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019266 switch (from->v_type)
19267 {
19268 case VAR_NUMBER:
19269 to->vval.v_number = from->vval.v_number;
19270 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019271#ifdef FEAT_FLOAT
19272 case VAR_FLOAT:
19273 to->vval.v_float = from->vval.v_float;
19274 break;
19275#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019276 case VAR_STRING:
19277 case VAR_FUNC:
19278 if (from->vval.v_string == NULL)
19279 to->vval.v_string = NULL;
19280 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019281 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019282 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019283 if (from->v_type == VAR_FUNC)
19284 func_ref(to->vval.v_string);
19285 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019286 break;
19287 case VAR_LIST:
19288 if (from->vval.v_list == NULL)
19289 to->vval.v_list = NULL;
19290 else
19291 {
19292 to->vval.v_list = from->vval.v_list;
19293 ++to->vval.v_list->lv_refcount;
19294 }
19295 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019296 case VAR_DICT:
19297 if (from->vval.v_dict == NULL)
19298 to->vval.v_dict = NULL;
19299 else
19300 {
19301 to->vval.v_dict = from->vval.v_dict;
19302 ++to->vval.v_dict->dv_refcount;
19303 }
19304 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019305 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019306 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019307 break;
19308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309}
19310
19311/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019312 * Make a copy of an item.
19313 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019314 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19315 * reference to an already copied list/dict can be used.
19316 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019317 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019318 static int
19319item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019320 typval_T *from;
19321 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019322 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019323 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019324{
19325 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019326 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019327
Bram Moolenaar33570922005-01-25 22:26:29 +000019328 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019329 {
19330 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019331 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019332 }
19333 ++recurse;
19334
19335 switch (from->v_type)
19336 {
19337 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019338#ifdef FEAT_FLOAT
19339 case VAR_FLOAT:
19340#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019341 case VAR_STRING:
19342 case VAR_FUNC:
19343 copy_tv(from, to);
19344 break;
19345 case VAR_LIST:
19346 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019347 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019348 if (from->vval.v_list == NULL)
19349 to->vval.v_list = NULL;
19350 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19351 {
19352 /* use the copy made earlier */
19353 to->vval.v_list = from->vval.v_list->lv_copylist;
19354 ++to->vval.v_list->lv_refcount;
19355 }
19356 else
19357 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19358 if (to->vval.v_list == NULL)
19359 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019360 break;
19361 case VAR_DICT:
19362 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019363 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019364 if (from->vval.v_dict == NULL)
19365 to->vval.v_dict = NULL;
19366 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19367 {
19368 /* use the copy made earlier */
19369 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19370 ++to->vval.v_dict->dv_refcount;
19371 }
19372 else
19373 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19374 if (to->vval.v_dict == NULL)
19375 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019376 break;
19377 default:
19378 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019379 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019380 }
19381 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019382 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019383}
19384
19385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386 * ":echo expr1 ..." print each argument separated with a space, add a
19387 * newline at the end.
19388 * ":echon expr1 ..." print each argument plain.
19389 */
19390 void
19391ex_echo(eap)
19392 exarg_T *eap;
19393{
19394 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019395 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019396 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397 char_u *p;
19398 int needclr = TRUE;
19399 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019400 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401
19402 if (eap->skip)
19403 ++emsg_skip;
19404 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19405 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019406 /* If eval1() causes an error message the text from the command may
19407 * still need to be cleared. E.g., "echo 22,44". */
19408 need_clr_eos = needclr;
19409
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019411 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412 {
19413 /*
19414 * Report the invalid expression unless the expression evaluation
19415 * has been cancelled due to an aborting error, an interrupt, or an
19416 * exception.
19417 */
19418 if (!aborting())
19419 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019420 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019421 break;
19422 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019423 need_clr_eos = FALSE;
19424
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425 if (!eap->skip)
19426 {
19427 if (atstart)
19428 {
19429 atstart = FALSE;
19430 /* Call msg_start() after eval1(), evaluating the expression
19431 * may cause a message to appear. */
19432 if (eap->cmdidx == CMD_echo)
19433 msg_start();
19434 }
19435 else if (eap->cmdidx == CMD_echo)
19436 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019437 current_copyID += COPYID_INC;
19438 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019439 if (p != NULL)
19440 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019442 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019444 if (*p != TAB && needclr)
19445 {
19446 /* remove any text still there from the command */
19447 msg_clr_eos();
19448 needclr = FALSE;
19449 }
19450 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451 }
19452 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019453 {
19454#ifdef FEAT_MBYTE
19455 if (has_mbyte)
19456 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019457 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019458
19459 (void)msg_outtrans_len_attr(p, i, echo_attr);
19460 p += i - 1;
19461 }
19462 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019464 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019467 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019469 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470 arg = skipwhite(arg);
19471 }
19472 eap->nextcmd = check_nextcmd(arg);
19473
19474 if (eap->skip)
19475 --emsg_skip;
19476 else
19477 {
19478 /* remove text that may still be there from the command */
19479 if (needclr)
19480 msg_clr_eos();
19481 if (eap->cmdidx == CMD_echo)
19482 msg_end();
19483 }
19484}
19485
19486/*
19487 * ":echohl {name}".
19488 */
19489 void
19490ex_echohl(eap)
19491 exarg_T *eap;
19492{
19493 int id;
19494
19495 id = syn_name2id(eap->arg);
19496 if (id == 0)
19497 echo_attr = 0;
19498 else
19499 echo_attr = syn_id2attr(id);
19500}
19501
19502/*
19503 * ":execute expr1 ..." execute the result of an expression.
19504 * ":echomsg expr1 ..." Print a message
19505 * ":echoerr expr1 ..." Print an error
19506 * Each gets spaces around each argument and a newline at the end for
19507 * echo commands
19508 */
19509 void
19510ex_execute(eap)
19511 exarg_T *eap;
19512{
19513 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019514 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515 int ret = OK;
19516 char_u *p;
19517 garray_T ga;
19518 int len;
19519 int save_did_emsg;
19520
19521 ga_init2(&ga, 1, 80);
19522
19523 if (eap->skip)
19524 ++emsg_skip;
19525 while (*arg != NUL && *arg != '|' && *arg != '\n')
19526 {
19527 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019528 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 {
19530 /*
19531 * Report the invalid expression unless the expression evaluation
19532 * has been cancelled due to an aborting error, an interrupt, or an
19533 * exception.
19534 */
19535 if (!aborting())
19536 EMSG2(_(e_invexpr2), p);
19537 ret = FAIL;
19538 break;
19539 }
19540
19541 if (!eap->skip)
19542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019543 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544 len = (int)STRLEN(p);
19545 if (ga_grow(&ga, len + 2) == FAIL)
19546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019547 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548 ret = FAIL;
19549 break;
19550 }
19551 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554 ga.ga_len += len;
19555 }
19556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019557 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 arg = skipwhite(arg);
19559 }
19560
19561 if (ret != FAIL && ga.ga_data != NULL)
19562 {
19563 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019564 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019565 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019566 out_flush();
19567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568 else if (eap->cmdidx == CMD_echoerr)
19569 {
19570 /* We don't want to abort following commands, restore did_emsg. */
19571 save_did_emsg = did_emsg;
19572 EMSG((char_u *)ga.ga_data);
19573 if (!force_abort)
19574 did_emsg = save_did_emsg;
19575 }
19576 else if (eap->cmdidx == CMD_execute)
19577 do_cmdline((char_u *)ga.ga_data,
19578 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19579 }
19580
19581 ga_clear(&ga);
19582
19583 if (eap->skip)
19584 --emsg_skip;
19585
19586 eap->nextcmd = check_nextcmd(arg);
19587}
19588
19589/*
19590 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19591 * "arg" points to the "&" or '+' when called, to "option" when returning.
19592 * Returns NULL when no option name found. Otherwise pointer to the char
19593 * after the option name.
19594 */
19595 static char_u *
19596find_option_end(arg, opt_flags)
19597 char_u **arg;
19598 int *opt_flags;
19599{
19600 char_u *p = *arg;
19601
19602 ++p;
19603 if (*p == 'g' && p[1] == ':')
19604 {
19605 *opt_flags = OPT_GLOBAL;
19606 p += 2;
19607 }
19608 else if (*p == 'l' && p[1] == ':')
19609 {
19610 *opt_flags = OPT_LOCAL;
19611 p += 2;
19612 }
19613 else
19614 *opt_flags = 0;
19615
19616 if (!ASCII_ISALPHA(*p))
19617 return NULL;
19618 *arg = p;
19619
19620 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
19621 p += 4; /* termcap option */
19622 else
19623 while (ASCII_ISALPHA(*p))
19624 ++p;
19625 return p;
19626}
19627
19628/*
19629 * ":function"
19630 */
19631 void
19632ex_function(eap)
19633 exarg_T *eap;
19634{
19635 char_u *theline;
19636 int j;
19637 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 char_u *name = NULL;
19640 char_u *p;
19641 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019642 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643 garray_T newargs;
19644 garray_T newlines;
19645 int varargs = FALSE;
19646 int mustend = FALSE;
19647 int flags = 0;
19648 ufunc_T *fp;
19649 int indent;
19650 int nesting;
19651 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019652 dictitem_T *v;
19653 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019654 static int func_nr = 0; /* number for nameless function */
19655 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019656 hashtab_T *ht;
19657 int todo;
19658 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019659 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660
19661 /*
19662 * ":function" without argument: list functions.
19663 */
19664 if (ends_excmd(*eap->arg))
19665 {
19666 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019667 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019668 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000019669 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019670 {
19671 if (!HASHITEM_EMPTY(hi))
19672 {
19673 --todo;
19674 fp = HI2UF(hi);
19675 if (!isdigit(*fp->uf_name))
19676 list_func_head(fp, FALSE);
19677 }
19678 }
19679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680 eap->nextcmd = check_nextcmd(eap->arg);
19681 return;
19682 }
19683
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019684 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019685 * ":function /pat": list functions matching pattern.
19686 */
19687 if (*eap->arg == '/')
19688 {
19689 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
19690 if (!eap->skip)
19691 {
19692 regmatch_T regmatch;
19693
19694 c = *p;
19695 *p = NUL;
19696 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
19697 *p = c;
19698 if (regmatch.regprog != NULL)
19699 {
19700 regmatch.rm_ic = p_ic;
19701
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019702 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019703 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
19704 {
19705 if (!HASHITEM_EMPTY(hi))
19706 {
19707 --todo;
19708 fp = HI2UF(hi);
19709 if (!isdigit(*fp->uf_name)
19710 && vim_regexec(&regmatch, fp->uf_name, 0))
19711 list_func_head(fp, FALSE);
19712 }
19713 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000019714 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019715 }
19716 }
19717 if (*p == '/')
19718 ++p;
19719 eap->nextcmd = check_nextcmd(p);
19720 return;
19721 }
19722
19723 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019724 * Get the function name. There are these situations:
19725 * func normal function name
19726 * "name" == func, "fudi.fd_dict" == NULL
19727 * dict.func new dictionary entry
19728 * "name" == NULL, "fudi.fd_dict" set,
19729 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19730 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019731 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019732 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19733 * dict.func existing dict entry that's not a Funcref
19734 * "name" == NULL, "fudi.fd_dict" set,
19735 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019738 name = trans_function_name(&p, eap->skip, 0, &fudi);
19739 paren = (vim_strchr(p, '(') != NULL);
19740 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 {
19742 /*
19743 * Return on an invalid expression in braces, unless the expression
19744 * evaluation has been cancelled due to an aborting error, an
19745 * interrupt, or an exception.
19746 */
19747 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019748 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019749 if (!eap->skip && fudi.fd_newkey != NULL)
19750 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019751 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754 else
19755 eap->skip = TRUE;
19756 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000019757
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758 /* An error in a function call during evaluation of an expression in magic
19759 * braces should not cause the function not to be defined. */
19760 saved_did_emsg = did_emsg;
19761 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762
19763 /*
19764 * ":function func" with only function name: list function.
19765 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019766 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 {
19768 if (!ends_excmd(*skipwhite(p)))
19769 {
19770 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019771 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 }
19773 eap->nextcmd = check_nextcmd(p);
19774 if (eap->nextcmd != NULL)
19775 *p = NUL;
19776 if (!eap->skip && !got_int)
19777 {
19778 fp = find_func(name);
19779 if (fp != NULL)
19780 {
19781 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019782 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019784 if (FUNCLINE(fp, j) == NULL)
19785 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786 msg_putchar('\n');
19787 msg_outnum((long)(j + 1));
19788 if (j < 9)
19789 msg_putchar(' ');
19790 if (j < 99)
19791 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019792 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793 out_flush(); /* show a line at a time */
19794 ui_breakcheck();
19795 }
19796 if (!got_int)
19797 {
19798 msg_putchar('\n');
19799 msg_puts((char_u *)" endfunction");
19800 }
19801 }
19802 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000019803 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019805 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806 }
19807
19808 /*
19809 * ":function name(arg1, arg2)" Define function.
19810 */
19811 p = skipwhite(p);
19812 if (*p != '(')
19813 {
19814 if (!eap->skip)
19815 {
19816 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019817 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818 }
19819 /* attempt to continue by skipping some text */
19820 if (vim_strchr(p, '(') != NULL)
19821 p = vim_strchr(p, '(');
19822 }
19823 p = skipwhite(p + 1);
19824
19825 ga_init2(&newargs, (int)sizeof(char_u *), 3);
19826 ga_init2(&newlines, (int)sizeof(char_u *), 3);
19827
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019828 if (!eap->skip)
19829 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019830 /* Check the name of the function. Unless it's a dictionary function
19831 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019832 if (name != NULL)
19833 arg = name;
19834 else
19835 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019836 if (arg != NULL && (fudi.fd_di == NULL
19837 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019838 {
19839 if (*arg == K_SPECIAL)
19840 j = 3;
19841 else
19842 j = 0;
19843 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
19844 : eval_isnamec(arg[j])))
19845 ++j;
19846 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000019847 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019848 }
19849 }
19850
Bram Moolenaar071d4272004-06-13 20:20:40 +000019851 /*
19852 * Isolate the arguments: "arg1, arg2, ...)"
19853 */
19854 while (*p != ')')
19855 {
19856 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
19857 {
19858 varargs = TRUE;
19859 p += 3;
19860 mustend = TRUE;
19861 }
19862 else
19863 {
19864 arg = p;
19865 while (ASCII_ISALNUM(*p) || *p == '_')
19866 ++p;
19867 if (arg == p || isdigit(*arg)
19868 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
19869 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
19870 {
19871 if (!eap->skip)
19872 EMSG2(_("E125: Illegal argument: %s"), arg);
19873 break;
19874 }
19875 if (ga_grow(&newargs, 1) == FAIL)
19876 goto erret;
19877 c = *p;
19878 *p = NUL;
19879 arg = vim_strsave(arg);
19880 if (arg == NULL)
19881 goto erret;
19882 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
19883 *p = c;
19884 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885 if (*p == ',')
19886 ++p;
19887 else
19888 mustend = TRUE;
19889 }
19890 p = skipwhite(p);
19891 if (mustend && *p != ')')
19892 {
19893 if (!eap->skip)
19894 EMSG2(_(e_invarg2), eap->arg);
19895 break;
19896 }
19897 }
19898 ++p; /* skip the ')' */
19899
Bram Moolenaare9a41262005-01-15 22:18:47 +000019900 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901 for (;;)
19902 {
19903 p = skipwhite(p);
19904 if (STRNCMP(p, "range", 5) == 0)
19905 {
19906 flags |= FC_RANGE;
19907 p += 5;
19908 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019909 else if (STRNCMP(p, "dict", 4) == 0)
19910 {
19911 flags |= FC_DICT;
19912 p += 4;
19913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914 else if (STRNCMP(p, "abort", 5) == 0)
19915 {
19916 flags |= FC_ABORT;
19917 p += 5;
19918 }
19919 else
19920 break;
19921 }
19922
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019923 /* When there is a line break use what follows for the function body.
19924 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19925 if (*p == '\n')
19926 line_arg = p + 1;
19927 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928 EMSG(_(e_trailing));
19929
19930 /*
19931 * Read the body of the function, until ":endfunction" is found.
19932 */
19933 if (KeyTyped)
19934 {
19935 /* Check if the function already exists, don't let the user type the
19936 * whole function before telling him it doesn't work! For a script we
19937 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019938 if (!eap->skip && !eap->forceit)
19939 {
19940 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
19941 EMSG(_(e_funcdict));
19942 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019943 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019946 if (!eap->skip && did_emsg)
19947 goto erret;
19948
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949 msg_putchar('\n'); /* don't overwrite the function name */
19950 cmdline_row = msg_row;
19951 }
19952
19953 indent = 2;
19954 nesting = 0;
19955 for (;;)
19956 {
19957 msg_scroll = TRUE;
19958 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019959 sourcing_lnum_off = sourcing_lnum;
19960
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019961 if (line_arg != NULL)
19962 {
19963 /* Use eap->arg, split up in parts by line breaks. */
19964 theline = line_arg;
19965 p = vim_strchr(theline, '\n');
19966 if (p == NULL)
19967 line_arg += STRLEN(line_arg);
19968 else
19969 {
19970 *p = NUL;
19971 line_arg = p + 1;
19972 }
19973 }
19974 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975 theline = getcmdline(':', 0L, indent);
19976 else
19977 theline = eap->getline(':', eap->cookie, indent);
19978 if (KeyTyped)
19979 lines_left = Rows - 1;
19980 if (theline == NULL)
19981 {
19982 EMSG(_("E126: Missing :endfunction"));
19983 goto erret;
19984 }
19985
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019986 /* Detect line continuation: sourcing_lnum increased more than one. */
19987 if (sourcing_lnum > sourcing_lnum_off + 1)
19988 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19989 else
19990 sourcing_lnum_off = 0;
19991
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992 if (skip_until != NULL)
19993 {
19994 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19995 * don't check for ":endfunc". */
19996 if (STRCMP(theline, skip_until) == 0)
19997 {
19998 vim_free(skip_until);
19999 skip_until = NULL;
20000 }
20001 }
20002 else
20003 {
20004 /* skip ':' and blanks*/
20005 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20006 ;
20007
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020008 /* Check for "endfunction". */
20009 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020011 if (line_arg == NULL)
20012 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013 break;
20014 }
20015
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020016 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 * at "end". */
20018 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20019 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020020 else if (STRNCMP(p, "if", 2) == 0
20021 || STRNCMP(p, "wh", 2) == 0
20022 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023 || STRNCMP(p, "try", 3) == 0)
20024 indent += 2;
20025
20026 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020027 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020029 if (*p == '!')
20030 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031 p += eval_fname_script(p);
20032 if (ASCII_ISALPHA(*p))
20033 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020034 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035 if (*skipwhite(p) == '(')
20036 {
20037 ++nesting;
20038 indent += 2;
20039 }
20040 }
20041 }
20042
20043 /* Check for ":append" or ":insert". */
20044 p = skip_range(p, NULL);
20045 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20046 || (p[0] == 'i'
20047 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20048 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20049 skip_until = vim_strsave((char_u *)".");
20050
20051 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20052 arg = skipwhite(skiptowhite(p));
20053 if (arg[0] == '<' && arg[1] =='<'
20054 && ((p[0] == 'p' && p[1] == 'y'
20055 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20056 || (p[0] == 'p' && p[1] == 'e'
20057 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20058 || (p[0] == 't' && p[1] == 'c'
20059 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20060 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20061 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020062 || (p[0] == 'm' && p[1] == 'z'
20063 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 ))
20065 {
20066 /* ":python <<" continues until a dot, like ":append" */
20067 p = skipwhite(arg + 2);
20068 if (*p == NUL)
20069 skip_until = vim_strsave((char_u *)".");
20070 else
20071 skip_until = vim_strsave(p);
20072 }
20073 }
20074
20075 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020076 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020077 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020078 if (line_arg == NULL)
20079 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020080 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020081 }
20082
20083 /* Copy the line to newly allocated memory. get_one_sourceline()
20084 * allocates 250 bytes per line, this saves 80% on average. The cost
20085 * is an extra alloc/free. */
20086 p = vim_strsave(theline);
20087 if (p != NULL)
20088 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020089 if (line_arg == NULL)
20090 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020091 theline = p;
20092 }
20093
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020094 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20095
20096 /* Add NULL lines for continuation lines, so that the line count is
20097 * equal to the index in the growarray. */
20098 while (sourcing_lnum_off-- > 0)
20099 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020100
20101 /* Check for end of eap->arg. */
20102 if (line_arg != NULL && *line_arg == NUL)
20103 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104 }
20105
20106 /* Don't define the function when skipping commands or when an error was
20107 * detected. */
20108 if (eap->skip || did_emsg)
20109 goto erret;
20110
20111 /*
20112 * If there are no errors, add the function
20113 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020114 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020115 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020116 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020117 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020118 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020119 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020120 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020121 goto erret;
20122 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020123
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020124 fp = find_func(name);
20125 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020127 if (!eap->forceit)
20128 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020129 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020130 goto erret;
20131 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020132 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020133 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020134 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020135 name);
20136 goto erret;
20137 }
20138 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020139 ga_clear_strings(&(fp->uf_args));
20140 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020141 vim_free(name);
20142 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144 }
20145 else
20146 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020147 char numbuf[20];
20148
20149 fp = NULL;
20150 if (fudi.fd_newkey == NULL && !eap->forceit)
20151 {
20152 EMSG(_(e_funcdict));
20153 goto erret;
20154 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020155 if (fudi.fd_di == NULL)
20156 {
20157 /* Can't add a function to a locked dictionary */
20158 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20159 goto erret;
20160 }
20161 /* Can't change an existing function if it is locked */
20162 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20163 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020164
20165 /* Give the function a sequential number. Can only be used with a
20166 * Funcref! */
20167 vim_free(name);
20168 sprintf(numbuf, "%d", ++func_nr);
20169 name = vim_strsave((char_u *)numbuf);
20170 if (name == NULL)
20171 goto erret;
20172 }
20173
20174 if (fp == NULL)
20175 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020176 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020177 {
20178 int slen, plen;
20179 char_u *scriptname;
20180
20181 /* Check that the autoload name matches the script name. */
20182 j = FAIL;
20183 if (sourcing_name != NULL)
20184 {
20185 scriptname = autoload_name(name);
20186 if (scriptname != NULL)
20187 {
20188 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020189 plen = (int)STRLEN(p);
20190 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020191 if (slen > plen && fnamecmp(p,
20192 sourcing_name + slen - plen) == 0)
20193 j = OK;
20194 vim_free(scriptname);
20195 }
20196 }
20197 if (j == FAIL)
20198 {
20199 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20200 goto erret;
20201 }
20202 }
20203
20204 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205 if (fp == NULL)
20206 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020207
20208 if (fudi.fd_dict != NULL)
20209 {
20210 if (fudi.fd_di == NULL)
20211 {
20212 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020213 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020214 if (fudi.fd_di == NULL)
20215 {
20216 vim_free(fp);
20217 goto erret;
20218 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020219 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20220 {
20221 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020222 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020223 goto erret;
20224 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020225 }
20226 else
20227 /* overwrite existing dict entry */
20228 clear_tv(&fudi.fd_di->di_tv);
20229 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020230 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020231 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020232 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020233
20234 /* behave like "dict" was used */
20235 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020236 }
20237
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020239 STRCPY(fp->uf_name, name);
20240 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020242 fp->uf_args = newargs;
20243 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020244#ifdef FEAT_PROFILE
20245 fp->uf_tml_count = NULL;
20246 fp->uf_tml_total = NULL;
20247 fp->uf_tml_self = NULL;
20248 fp->uf_profiling = FALSE;
20249 if (prof_def_func())
20250 func_do_profile(fp);
20251#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020252 fp->uf_varargs = varargs;
20253 fp->uf_flags = flags;
20254 fp->uf_calls = 0;
20255 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020256 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257
20258erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259 ga_clear_strings(&newargs);
20260 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020261ret_free:
20262 vim_free(skip_until);
20263 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266}
20267
20268/*
20269 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020270 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020272 * flags:
20273 * TFN_INT: internal function name OK
20274 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 * Advances "pp" to just after the function name (if no error).
20276 */
20277 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020278trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279 char_u **pp;
20280 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020281 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020282 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020284 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 char_u *start;
20286 char_u *end;
20287 int lead;
20288 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020290 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020291
20292 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020293 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020294 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020295
20296 /* Check for hard coded <SNR>: already translated function ID (from a user
20297 * command). */
20298 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20299 && (*pp)[2] == (int)KE_SNR)
20300 {
20301 *pp += 3;
20302 len = get_id_len(pp) + 3;
20303 return vim_strnsave(start, len);
20304 }
20305
20306 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20307 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020309 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020311
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020312 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20313 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020314 if (end == start)
20315 {
20316 if (!skip)
20317 EMSG(_("E129: Function name required"));
20318 goto theend;
20319 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020320 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020321 {
20322 /*
20323 * Report an invalid expression in braces, unless the expression
20324 * evaluation has been cancelled due to an aborting error, an
20325 * interrupt, or an exception.
20326 */
20327 if (!aborting())
20328 {
20329 if (end != NULL)
20330 EMSG2(_(e_invarg2), start);
20331 }
20332 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020333 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020334 goto theend;
20335 }
20336
20337 if (lv.ll_tv != NULL)
20338 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020339 if (fdp != NULL)
20340 {
20341 fdp->fd_dict = lv.ll_dict;
20342 fdp->fd_newkey = lv.ll_newkey;
20343 lv.ll_newkey = NULL;
20344 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020345 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020346 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20347 {
20348 name = vim_strsave(lv.ll_tv->vval.v_string);
20349 *pp = end;
20350 }
20351 else
20352 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020353 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20354 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020355 EMSG(_(e_funcref));
20356 else
20357 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020358 name = NULL;
20359 }
20360 goto theend;
20361 }
20362
20363 if (lv.ll_name == NULL)
20364 {
20365 /* Error found, but continue after the function name. */
20366 *pp = end;
20367 goto theend;
20368 }
20369
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020370 /* Check if the name is a Funcref. If so, use the value. */
20371 if (lv.ll_exp_name != NULL)
20372 {
20373 len = (int)STRLEN(lv.ll_exp_name);
20374 name = deref_func_name(lv.ll_exp_name, &len);
20375 if (name == lv.ll_exp_name)
20376 name = NULL;
20377 }
20378 else
20379 {
20380 len = (int)(end - *pp);
20381 name = deref_func_name(*pp, &len);
20382 if (name == *pp)
20383 name = NULL;
20384 }
20385 if (name != NULL)
20386 {
20387 name = vim_strsave(name);
20388 *pp = end;
20389 goto theend;
20390 }
20391
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020392 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020393 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020394 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020395 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20396 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20397 {
20398 /* When there was "s:" already or the name expanded to get a
20399 * leading "s:" then remove it. */
20400 lv.ll_name += 2;
20401 len -= 2;
20402 lead = 2;
20403 }
20404 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020405 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020406 {
20407 if (lead == 2) /* skip over "s:" */
20408 lv.ll_name += 2;
20409 len = (int)(end - lv.ll_name);
20410 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020411
20412 /*
20413 * Copy the function name to allocated memory.
20414 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20415 * Accept <SNR>123_name() outside a script.
20416 */
20417 if (skip)
20418 lead = 0; /* do nothing */
20419 else if (lead > 0)
20420 {
20421 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020422 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20423 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020424 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020425 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020426 if (current_SID <= 0)
20427 {
20428 EMSG(_(e_usingsid));
20429 goto theend;
20430 }
20431 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20432 lead += (int)STRLEN(sid_buf);
20433 }
20434 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020435 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020436 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020437 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020438 goto theend;
20439 }
20440 name = alloc((unsigned)(len + lead + 1));
20441 if (name != NULL)
20442 {
20443 if (lead > 0)
20444 {
20445 name[0] = K_SPECIAL;
20446 name[1] = KS_EXTRA;
20447 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020448 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020449 STRCPY(name + 3, sid_buf);
20450 }
20451 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20452 name[len + lead] = NUL;
20453 }
20454 *pp = end;
20455
20456theend:
20457 clear_lval(&lv);
20458 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459}
20460
20461/*
20462 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20463 * Return 2 if "p" starts with "s:".
20464 * Return 0 otherwise.
20465 */
20466 static int
20467eval_fname_script(p)
20468 char_u *p;
20469{
20470 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20471 || STRNICMP(p + 1, "SNR>", 4) == 0))
20472 return 5;
20473 if (p[0] == 's' && p[1] == ':')
20474 return 2;
20475 return 0;
20476}
20477
20478/*
20479 * Return TRUE if "p" starts with "<SID>" or "s:".
20480 * Only works if eval_fname_script() returned non-zero for "p"!
20481 */
20482 static int
20483eval_fname_sid(p)
20484 char_u *p;
20485{
20486 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20487}
20488
20489/*
20490 * List the head of the function: "name(arg1, arg2)".
20491 */
20492 static void
20493list_func_head(fp, indent)
20494 ufunc_T *fp;
20495 int indent;
20496{
20497 int j;
20498
20499 msg_start();
20500 if (indent)
20501 MSG_PUTS(" ");
20502 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020503 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020504 {
20505 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020506 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020507 }
20508 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020509 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020511 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512 {
20513 if (j)
20514 MSG_PUTS(", ");
20515 msg_puts(FUNCARG(fp, j));
20516 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020517 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 {
20519 if (j)
20520 MSG_PUTS(", ");
20521 MSG_PUTS("...");
20522 }
20523 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020524 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020525 if (p_verbose > 0)
20526 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527}
20528
20529/*
20530 * Find a function by name, return pointer to it in ufuncs.
20531 * Return NULL for unknown function.
20532 */
20533 static ufunc_T *
20534find_func(name)
20535 char_u *name;
20536{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020537 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020539 hi = hash_find(&func_hashtab, name);
20540 if (!HASHITEM_EMPTY(hi))
20541 return HI2UF(hi);
20542 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020543}
20544
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020545#if defined(EXITFREE) || defined(PROTO)
20546 void
20547free_all_functions()
20548{
20549 hashitem_T *hi;
20550
20551 /* Need to start all over every time, because func_free() may change the
20552 * hash table. */
20553 while (func_hashtab.ht_used > 0)
20554 for (hi = func_hashtab.ht_array; ; ++hi)
20555 if (!HASHITEM_EMPTY(hi))
20556 {
20557 func_free(HI2UF(hi));
20558 break;
20559 }
20560}
20561#endif
20562
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020563/*
20564 * Return TRUE if a function "name" exists.
20565 */
20566 static int
20567function_exists(name)
20568 char_u *name;
20569{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020570 char_u *nm = name;
20571 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020572 int n = FALSE;
20573
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020574 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020575 nm = skipwhite(nm);
20576
20577 /* Only accept "funcname", "funcname ", "funcname (..." and
20578 * "funcname(...", not "funcname!...". */
20579 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020580 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020581 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020582 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020583 else
20584 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020585 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020586 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020587 return n;
20588}
20589
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020590/*
20591 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020592 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020593 */
20594 static int
20595builtin_function(name)
20596 char_u *name;
20597{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020598 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20599 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020600}
20601
Bram Moolenaar05159a02005-02-26 23:04:13 +000020602#if defined(FEAT_PROFILE) || defined(PROTO)
20603/*
20604 * Start profiling function "fp".
20605 */
20606 static void
20607func_do_profile(fp)
20608 ufunc_T *fp;
20609{
20610 fp->uf_tm_count = 0;
20611 profile_zero(&fp->uf_tm_self);
20612 profile_zero(&fp->uf_tm_total);
20613 if (fp->uf_tml_count == NULL)
20614 fp->uf_tml_count = (int *)alloc_clear((unsigned)
20615 (sizeof(int) * fp->uf_lines.ga_len));
20616 if (fp->uf_tml_total == NULL)
20617 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
20618 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20619 if (fp->uf_tml_self == NULL)
20620 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
20621 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20622 fp->uf_tml_idx = -1;
20623 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
20624 || fp->uf_tml_self == NULL)
20625 return; /* out of memory */
20626
20627 fp->uf_profiling = TRUE;
20628}
20629
20630/*
20631 * Dump the profiling results for all functions in file "fd".
20632 */
20633 void
20634func_dump_profile(fd)
20635 FILE *fd;
20636{
20637 hashitem_T *hi;
20638 int todo;
20639 ufunc_T *fp;
20640 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000020641 ufunc_T **sorttab;
20642 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020643
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020644 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020645 if (todo == 0)
20646 return; /* nothing to dump */
20647
Bram Moolenaar73830342005-02-28 22:48:19 +000020648 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
20649
Bram Moolenaar05159a02005-02-26 23:04:13 +000020650 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
20651 {
20652 if (!HASHITEM_EMPTY(hi))
20653 {
20654 --todo;
20655 fp = HI2UF(hi);
20656 if (fp->uf_profiling)
20657 {
Bram Moolenaar73830342005-02-28 22:48:19 +000020658 if (sorttab != NULL)
20659 sorttab[st_len++] = fp;
20660
Bram Moolenaar05159a02005-02-26 23:04:13 +000020661 if (fp->uf_name[0] == K_SPECIAL)
20662 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
20663 else
20664 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
20665 if (fp->uf_tm_count == 1)
20666 fprintf(fd, "Called 1 time\n");
20667 else
20668 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
20669 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
20670 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
20671 fprintf(fd, "\n");
20672 fprintf(fd, "count total (s) self (s)\n");
20673
20674 for (i = 0; i < fp->uf_lines.ga_len; ++i)
20675 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020676 if (FUNCLINE(fp, i) == NULL)
20677 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000020678 prof_func_line(fd, fp->uf_tml_count[i],
20679 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020680 fprintf(fd, "%s\n", FUNCLINE(fp, i));
20681 }
20682 fprintf(fd, "\n");
20683 }
20684 }
20685 }
Bram Moolenaar73830342005-02-28 22:48:19 +000020686
20687 if (sorttab != NULL && st_len > 0)
20688 {
20689 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20690 prof_total_cmp);
20691 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
20692 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20693 prof_self_cmp);
20694 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
20695 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020696
20697 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020698}
Bram Moolenaar73830342005-02-28 22:48:19 +000020699
20700 static void
20701prof_sort_list(fd, sorttab, st_len, title, prefer_self)
20702 FILE *fd;
20703 ufunc_T **sorttab;
20704 int st_len;
20705 char *title;
20706 int prefer_self; /* when equal print only self time */
20707{
20708 int i;
20709 ufunc_T *fp;
20710
20711 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
20712 fprintf(fd, "count total (s) self (s) function\n");
20713 for (i = 0; i < 20 && i < st_len; ++i)
20714 {
20715 fp = sorttab[i];
20716 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
20717 prefer_self);
20718 if (fp->uf_name[0] == K_SPECIAL)
20719 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
20720 else
20721 fprintf(fd, " %s()\n", fp->uf_name);
20722 }
20723 fprintf(fd, "\n");
20724}
20725
20726/*
20727 * Print the count and times for one function or function line.
20728 */
20729 static void
20730prof_func_line(fd, count, total, self, prefer_self)
20731 FILE *fd;
20732 int count;
20733 proftime_T *total;
20734 proftime_T *self;
20735 int prefer_self; /* when equal print only self time */
20736{
20737 if (count > 0)
20738 {
20739 fprintf(fd, "%5d ", count);
20740 if (prefer_self && profile_equal(total, self))
20741 fprintf(fd, " ");
20742 else
20743 fprintf(fd, "%s ", profile_msg(total));
20744 if (!prefer_self && profile_equal(total, self))
20745 fprintf(fd, " ");
20746 else
20747 fprintf(fd, "%s ", profile_msg(self));
20748 }
20749 else
20750 fprintf(fd, " ");
20751}
20752
20753/*
20754 * Compare function for total time sorting.
20755 */
20756 static int
20757#ifdef __BORLANDC__
20758_RTLENTRYF
20759#endif
20760prof_total_cmp(s1, s2)
20761 const void *s1;
20762 const void *s2;
20763{
20764 ufunc_T *p1, *p2;
20765
20766 p1 = *(ufunc_T **)s1;
20767 p2 = *(ufunc_T **)s2;
20768 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
20769}
20770
20771/*
20772 * Compare function for self time sorting.
20773 */
20774 static int
20775#ifdef __BORLANDC__
20776_RTLENTRYF
20777#endif
20778prof_self_cmp(s1, s2)
20779 const void *s1;
20780 const void *s2;
20781{
20782 ufunc_T *p1, *p2;
20783
20784 p1 = *(ufunc_T **)s1;
20785 p2 = *(ufunc_T **)s2;
20786 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
20787}
20788
Bram Moolenaar05159a02005-02-26 23:04:13 +000020789#endif
20790
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020791/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020792 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020793 * Return TRUE if a package was loaded.
20794 */
20795 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020796script_autoload(name, reload)
20797 char_u *name;
20798 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020799{
20800 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020801 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020802 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020803 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020804
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020805 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020806 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020807 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020808 return FALSE;
20809
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020810 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020811
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020812 /* Find the name in the list of previously loaded package names. Skip
20813 * "autoload/", it's always the same. */
20814 for (i = 0; i < ga_loaded.ga_len; ++i)
20815 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
20816 break;
20817 if (!reload && i < ga_loaded.ga_len)
20818 ret = FALSE; /* was loaded already */
20819 else
20820 {
20821 /* Remember the name if it wasn't loaded already. */
20822 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
20823 {
20824 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
20825 tofree = NULL;
20826 }
20827
20828 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000020829 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020830 ret = TRUE;
20831 }
20832
20833 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020834 return ret;
20835}
20836
20837/*
20838 * Return the autoload script name for a function or variable name.
20839 * Returns NULL when out of memory.
20840 */
20841 static char_u *
20842autoload_name(name)
20843 char_u *name;
20844{
20845 char_u *p;
20846 char_u *scriptname;
20847
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020848 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020849 scriptname = alloc((unsigned)(STRLEN(name) + 14));
20850 if (scriptname == NULL)
20851 return FALSE;
20852 STRCPY(scriptname, "autoload/");
20853 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020854 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020855 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020856 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020857 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020858 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020859}
20860
Bram Moolenaar071d4272004-06-13 20:20:40 +000020861#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20862
20863/*
20864 * Function given to ExpandGeneric() to obtain the list of user defined
20865 * function names.
20866 */
20867 char_u *
20868get_user_func_name(xp, idx)
20869 expand_T *xp;
20870 int idx;
20871{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020872 static long_u done;
20873 static hashitem_T *hi;
20874 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875
20876 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020877 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020878 done = 0;
20879 hi = func_hashtab.ht_array;
20880 }
20881 if (done < func_hashtab.ht_used)
20882 {
20883 if (done++ > 0)
20884 ++hi;
20885 while (HASHITEM_EMPTY(hi))
20886 ++hi;
20887 fp = HI2UF(hi);
20888
20889 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
20890 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891
20892 cat_func_name(IObuff, fp);
20893 if (xp->xp_context != EXPAND_USER_FUNC)
20894 {
20895 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020896 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020897 STRCAT(IObuff, ")");
20898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899 return IObuff;
20900 }
20901 return NULL;
20902}
20903
20904#endif /* FEAT_CMDL_COMPL */
20905
20906/*
20907 * Copy the function name of "fp" to buffer "buf".
20908 * "buf" must be able to hold the function name plus three bytes.
20909 * Takes care of script-local function names.
20910 */
20911 static void
20912cat_func_name(buf, fp)
20913 char_u *buf;
20914 ufunc_T *fp;
20915{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020916 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917 {
20918 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020919 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920 }
20921 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020922 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923}
20924
20925/*
20926 * ":delfunction {name}"
20927 */
20928 void
20929ex_delfunction(eap)
20930 exarg_T *eap;
20931{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020932 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933 char_u *p;
20934 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020935 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020936
20937 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020938 name = trans_function_name(&p, eap->skip, 0, &fudi);
20939 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020940 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020941 {
20942 if (fudi.fd_dict != NULL && !eap->skip)
20943 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946 if (!ends_excmd(*skipwhite(p)))
20947 {
20948 vim_free(name);
20949 EMSG(_(e_trailing));
20950 return;
20951 }
20952 eap->nextcmd = check_nextcmd(p);
20953 if (eap->nextcmd != NULL)
20954 *p = NUL;
20955
20956 if (!eap->skip)
20957 fp = find_func(name);
20958 vim_free(name);
20959
20960 if (!eap->skip)
20961 {
20962 if (fp == NULL)
20963 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020964 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965 return;
20966 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020967 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968 {
20969 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
20970 return;
20971 }
20972
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020973 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020975 /* Delete the dict item that refers to the function, it will
20976 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020977 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020979 else
20980 func_free(fp);
20981 }
20982}
20983
20984/*
20985 * Free a function and remove it from the list of functions.
20986 */
20987 static void
20988func_free(fp)
20989 ufunc_T *fp;
20990{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020991 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020992
20993 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020994 ga_clear_strings(&(fp->uf_args));
20995 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020996#ifdef FEAT_PROFILE
20997 vim_free(fp->uf_tml_count);
20998 vim_free(fp->uf_tml_total);
20999 vim_free(fp->uf_tml_self);
21000#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021001
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021002 /* remove the function from the function hashtable */
21003 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21004 if (HASHITEM_EMPTY(hi))
21005 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021006 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021007 hash_remove(&func_hashtab, hi);
21008
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021009 vim_free(fp);
21010}
21011
21012/*
21013 * Unreference a Function: decrement the reference count and free it when it
21014 * becomes zero. Only for numbered functions.
21015 */
21016 static void
21017func_unref(name)
21018 char_u *name;
21019{
21020 ufunc_T *fp;
21021
21022 if (name != NULL && isdigit(*name))
21023 {
21024 fp = find_func(name);
21025 if (fp == NULL)
21026 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021027 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021028 {
21029 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021030 * when "uf_calls" becomes zero. */
21031 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021032 func_free(fp);
21033 }
21034 }
21035}
21036
21037/*
21038 * Count a reference to a Function.
21039 */
21040 static void
21041func_ref(name)
21042 char_u *name;
21043{
21044 ufunc_T *fp;
21045
21046 if (name != NULL && isdigit(*name))
21047 {
21048 fp = find_func(name);
21049 if (fp == NULL)
21050 EMSG2(_(e_intern2), "func_ref()");
21051 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021052 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053 }
21054}
21055
21056/*
21057 * Call a user function.
21058 */
21059 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021060call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061 ufunc_T *fp; /* pointer to function */
21062 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021063 typval_T *argvars; /* arguments */
21064 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065 linenr_T firstline; /* first line of range */
21066 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021067 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021068{
Bram Moolenaar33570922005-01-25 22:26:29 +000021069 char_u *save_sourcing_name;
21070 linenr_T save_sourcing_lnum;
21071 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021072 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021073 int save_did_emsg;
21074 static int depth = 0;
21075 dictitem_T *v;
21076 int fixvar_idx = 0; /* index in fixvar[] */
21077 int i;
21078 int ai;
21079 char_u numbuf[NUMBUFLEN];
21080 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021081#ifdef FEAT_PROFILE
21082 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021083 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085
21086 /* If depth of calling is getting too high, don't execute the function */
21087 if (depth >= p_mfd)
21088 {
21089 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021090 rettv->v_type = VAR_NUMBER;
21091 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092 return;
21093 }
21094 ++depth;
21095
21096 line_breakcheck(); /* check for CTRL-C hit */
21097
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021098 fc = (funccall_T *)alloc(sizeof(funccall_T));
21099 fc->caller = current_funccal;
21100 current_funccal = fc;
21101 fc->func = fp;
21102 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021103 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021104 fc->linenr = 0;
21105 fc->returned = FALSE;
21106 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021108 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21109 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110
Bram Moolenaar33570922005-01-25 22:26:29 +000021111 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021112 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021113 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21114 * each argument variable and saves a lot of time.
21115 */
21116 /*
21117 * Init l: variables.
21118 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021119 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021120 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021121 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021122 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21123 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021124 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021125 name = v->di_key;
21126 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021127 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021128 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021129 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021130 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021131 v->di_tv.vval.v_dict = selfdict;
21132 ++selfdict->dv_refcount;
21133 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021134
Bram Moolenaar33570922005-01-25 22:26:29 +000021135 /*
21136 * Init a: variables.
21137 * Set a:0 to "argcount".
21138 * Set a:000 to a list with room for the "..." arguments.
21139 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021140 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21141 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021142 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021143 /* Use "name" to avoid a warning from some compiler that checks the
21144 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021145 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021146 name = v->di_key;
21147 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021148 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021149 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021150 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021151 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021152 v->di_tv.vval.v_list = &fc->l_varlist;
21153 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21154 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21155 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021156
21157 /*
21158 * Set a:firstline to "firstline" and a:lastline to "lastline".
21159 * Set a:name to named arguments.
21160 * Set a:N to the "..." arguments.
21161 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021162 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021163 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021164 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021165 (varnumber_T)lastline);
21166 for (i = 0; i < argcount; ++i)
21167 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021168 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021169 if (ai < 0)
21170 /* named argument a:name */
21171 name = FUNCARG(fp, i);
21172 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021173 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021174 /* "..." argument a:1, a:2, etc. */
21175 sprintf((char *)numbuf, "%d", ai + 1);
21176 name = numbuf;
21177 }
21178 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21179 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021180 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021181 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21182 }
21183 else
21184 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021185 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21186 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021187 if (v == NULL)
21188 break;
21189 v->di_flags = DI_FLAGS_RO;
21190 }
21191 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021192 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021193
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021194 /* Note: the values are copied directly to avoid alloc/free.
21195 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021196 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021197 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021198
21199 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21200 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021201 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21202 fc->l_listitems[ai].li_tv = argvars[i];
21203 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021204 }
21205 }
21206
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207 /* Don't redraw while executing the function. */
21208 ++RedrawingDisabled;
21209 save_sourcing_name = sourcing_name;
21210 save_sourcing_lnum = sourcing_lnum;
21211 sourcing_lnum = 1;
21212 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021213 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021214 if (sourcing_name != NULL)
21215 {
21216 if (save_sourcing_name != NULL
21217 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21218 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21219 else
21220 STRCPY(sourcing_name, "function ");
21221 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21222
21223 if (p_verbose >= 12)
21224 {
21225 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021226 verbose_enter_scroll();
21227
Bram Moolenaar555b2802005-05-19 21:08:39 +000021228 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229 if (p_verbose >= 14)
21230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021231 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021232 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021233 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021234 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021235
21236 msg_puts((char_u *)"(");
21237 for (i = 0; i < argcount; ++i)
21238 {
21239 if (i > 0)
21240 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021241 if (argvars[i].v_type == VAR_NUMBER)
21242 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243 else
21244 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021245 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21246 if (s != NULL)
21247 {
21248 trunc_string(s, buf, MSG_BUF_CLEN);
21249 msg_puts(buf);
21250 vim_free(tofree);
21251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252 }
21253 }
21254 msg_puts((char_u *)")");
21255 }
21256 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021257
21258 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021259 --no_wait_return;
21260 }
21261 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021262#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021263 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021264 {
21265 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21266 func_do_profile(fp);
21267 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021268 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021269 {
21270 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021271 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021272 profile_zero(&fp->uf_tm_children);
21273 }
21274 script_prof_save(&wait_start);
21275 }
21276#endif
21277
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021279 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280 save_did_emsg = did_emsg;
21281 did_emsg = FALSE;
21282
21283 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021284 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021285 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21286
21287 --RedrawingDisabled;
21288
21289 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021290 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021292 clear_tv(rettv);
21293 rettv->v_type = VAR_NUMBER;
21294 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 }
21296
Bram Moolenaar05159a02005-02-26 23:04:13 +000021297#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021298 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021299 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021300 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021301 profile_end(&call_start);
21302 profile_sub_wait(&wait_start, &call_start);
21303 profile_add(&fp->uf_tm_total, &call_start);
21304 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021305 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021306 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021307 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21308 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021309 }
21310 }
21311#endif
21312
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313 /* when being verbose, mention the return value */
21314 if (p_verbose >= 12)
21315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021317 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021320 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021321 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021322 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021323 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021324 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021325 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021326 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021327 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021328 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021329 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021330
Bram Moolenaar555b2802005-05-19 21:08:39 +000021331 /* The value may be very long. Skip the middle part, so that we
21332 * have some idea how it starts and ends. smsg() would always
21333 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021334 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021335 if (s != NULL)
21336 {
21337 trunc_string(s, buf, MSG_BUF_CLEN);
21338 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21339 vim_free(tofree);
21340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341 }
21342 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021343
21344 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345 --no_wait_return;
21346 }
21347
21348 vim_free(sourcing_name);
21349 sourcing_name = save_sourcing_name;
21350 sourcing_lnum = save_sourcing_lnum;
21351 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021352#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021353 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021354 script_prof_restore(&wait_start);
21355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356
21357 if (p_verbose >= 12 && sourcing_name != NULL)
21358 {
21359 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021360 verbose_enter_scroll();
21361
Bram Moolenaar555b2802005-05-19 21:08:39 +000021362 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021364
21365 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021366 --no_wait_return;
21367 }
21368
21369 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021370 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021372
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021373 /* If the a:000 list and the l: and a: dicts are not referenced we can
21374 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021375 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21376 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21377 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21378 {
21379 free_funccal(fc, FALSE);
21380 }
21381 else
21382 {
21383 hashitem_T *hi;
21384 listitem_T *li;
21385 int todo;
21386
21387 /* "fc" is still in use. This can happen when returning "a:000" or
21388 * assigning "l:" to a global variable.
21389 * Link "fc" in the list for garbage collection later. */
21390 fc->caller = previous_funccal;
21391 previous_funccal = fc;
21392
21393 /* Make a copy of the a: variables, since we didn't do that above. */
21394 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21395 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21396 {
21397 if (!HASHITEM_EMPTY(hi))
21398 {
21399 --todo;
21400 v = HI2DI(hi);
21401 copy_tv(&v->di_tv, &v->di_tv);
21402 }
21403 }
21404
21405 /* Make a copy of the a:000 items, since we didn't do that above. */
21406 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21407 copy_tv(&li->li_tv, &li->li_tv);
21408 }
21409}
21410
21411/*
21412 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021413 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021414 */
21415 static int
21416can_free_funccal(fc, copyID)
21417 funccall_T *fc;
21418 int copyID;
21419{
21420 return (fc->l_varlist.lv_copyID != copyID
21421 && fc->l_vars.dv_copyID != copyID
21422 && fc->l_avars.dv_copyID != copyID);
21423}
21424
21425/*
21426 * Free "fc" and what it contains.
21427 */
21428 static void
21429free_funccal(fc, free_val)
21430 funccall_T *fc;
21431 int free_val; /* a: vars were allocated */
21432{
21433 listitem_T *li;
21434
21435 /* The a: variables typevals may not have been allocated, only free the
21436 * allocated variables. */
21437 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21438
21439 /* free all l: variables */
21440 vars_clear(&fc->l_vars.dv_hashtab);
21441
21442 /* Free the a:000 variables if they were allocated. */
21443 if (free_val)
21444 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21445 clear_tv(&li->li_tv);
21446
21447 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448}
21449
21450/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021451 * Add a number variable "name" to dict "dp" with value "nr".
21452 */
21453 static void
21454add_nr_var(dp, v, name, nr)
21455 dict_T *dp;
21456 dictitem_T *v;
21457 char *name;
21458 varnumber_T nr;
21459{
21460 STRCPY(v->di_key, name);
21461 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21462 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21463 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021464 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021465 v->di_tv.vval.v_number = nr;
21466}
21467
21468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 * ":return [expr]"
21470 */
21471 void
21472ex_return(eap)
21473 exarg_T *eap;
21474{
21475 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021476 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 int returning = FALSE;
21478
21479 if (current_funccal == NULL)
21480 {
21481 EMSG(_("E133: :return not inside a function"));
21482 return;
21483 }
21484
21485 if (eap->skip)
21486 ++emsg_skip;
21487
21488 eap->nextcmd = NULL;
21489 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021490 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021491 {
21492 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021493 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021495 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496 }
21497 /* It's safer to return also on error. */
21498 else if (!eap->skip)
21499 {
21500 /*
21501 * Return unless the expression evaluation has been cancelled due to an
21502 * aborting error, an interrupt, or an exception.
21503 */
21504 if (!aborting())
21505 returning = do_return(eap, FALSE, TRUE, NULL);
21506 }
21507
21508 /* When skipping or the return gets pending, advance to the next command
21509 * in this line (!returning). Otherwise, ignore the rest of the line.
21510 * Following lines will be ignored by get_func_line(). */
21511 if (returning)
21512 eap->nextcmd = NULL;
21513 else if (eap->nextcmd == NULL) /* no argument */
21514 eap->nextcmd = check_nextcmd(arg);
21515
21516 if (eap->skip)
21517 --emsg_skip;
21518}
21519
21520/*
21521 * Return from a function. Possibly makes the return pending. Also called
21522 * for a pending return at the ":endtry" or after returning from an extra
21523 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021524 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021525 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526 * FALSE when the return gets pending.
21527 */
21528 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021529do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530 exarg_T *eap;
21531 int reanimate;
21532 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021533 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534{
21535 int idx;
21536 struct condstack *cstack = eap->cstack;
21537
21538 if (reanimate)
21539 /* Undo the return. */
21540 current_funccal->returned = FALSE;
21541
21542 /*
21543 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21544 * not in its finally clause (which then is to be executed next) is found.
21545 * In this case, make the ":return" pending for execution at the ":endtry".
21546 * Otherwise, return normally.
21547 */
21548 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21549 if (idx >= 0)
21550 {
21551 cstack->cs_pending[idx] = CSTP_RETURN;
21552
21553 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021554 /* A pending return again gets pending. "rettv" points to an
21555 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021557 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021558 else
21559 {
21560 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021561 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021563 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021565 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566 {
21567 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021568 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021569 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 else
21571 EMSG(_(e_outofmem));
21572 }
21573 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021574 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575
21576 if (reanimate)
21577 {
21578 /* The pending return value could be overwritten by a ":return"
21579 * without argument in a finally clause; reset the default
21580 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021581 current_funccal->rettv->v_type = VAR_NUMBER;
21582 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583 }
21584 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021585 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586 }
21587 else
21588 {
21589 current_funccal->returned = TRUE;
21590
21591 /* If the return is carried out now, store the return value. For
21592 * a return immediately after reanimation, the value is already
21593 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021594 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021596 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021597 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021599 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600 }
21601 }
21602
21603 return idx < 0;
21604}
21605
21606/*
21607 * Free the variable with a pending return value.
21608 */
21609 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021610discard_pending_return(rettv)
21611 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612{
Bram Moolenaar33570922005-01-25 22:26:29 +000021613 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614}
21615
21616/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021617 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 * is an allocated string. Used by report_pending() for verbose messages.
21619 */
21620 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021621get_return_cmd(rettv)
21622 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021624 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021625 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021626 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021628 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021629 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021630 if (s == NULL)
21631 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021632
21633 STRCPY(IObuff, ":return ");
21634 STRNCPY(IObuff + 8, s, IOSIZE - 8);
21635 if (STRLEN(s) + 8 >= IOSIZE)
21636 STRCPY(IObuff + IOSIZE - 4, "...");
21637 vim_free(tofree);
21638 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639}
21640
21641/*
21642 * Get next function line.
21643 * Called by do_cmdline() to get the next line.
21644 * Returns allocated string, or NULL for end of function.
21645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646 char_u *
21647get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000021648 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000021650 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651{
Bram Moolenaar33570922005-01-25 22:26:29 +000021652 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021653 ufunc_T *fp = fcp->func;
21654 char_u *retval;
21655 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656
21657 /* If breakpoints have been added/deleted need to check for it. */
21658 if (fcp->dbg_tick != debug_tick)
21659 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021660 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 sourcing_lnum);
21662 fcp->dbg_tick = debug_tick;
21663 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021664#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021665 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021666 func_line_end(cookie);
21667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668
Bram Moolenaar05159a02005-02-26 23:04:13 +000021669 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021670 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
21671 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672 retval = NULL;
21673 else
21674 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021675 /* Skip NULL lines (continuation lines). */
21676 while (fcp->linenr < gap->ga_len
21677 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
21678 ++fcp->linenr;
21679 if (fcp->linenr >= gap->ga_len)
21680 retval = NULL;
21681 else
21682 {
21683 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
21684 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021685#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021686 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021687 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021688#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690 }
21691
21692 /* Did we encounter a breakpoint? */
21693 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
21694 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021695 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021696 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021697 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021698 sourcing_lnum);
21699 fcp->dbg_tick = debug_tick;
21700 }
21701
21702 return retval;
21703}
21704
Bram Moolenaar05159a02005-02-26 23:04:13 +000021705#if defined(FEAT_PROFILE) || defined(PROTO)
21706/*
21707 * Called when starting to read a function line.
21708 * "sourcing_lnum" must be correct!
21709 * When skipping lines it may not actually be executed, but we won't find out
21710 * until later and we need to store the time now.
21711 */
21712 void
21713func_line_start(cookie)
21714 void *cookie;
21715{
21716 funccall_T *fcp = (funccall_T *)cookie;
21717 ufunc_T *fp = fcp->func;
21718
21719 if (fp->uf_profiling && sourcing_lnum >= 1
21720 && sourcing_lnum <= fp->uf_lines.ga_len)
21721 {
21722 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021723 /* Skip continuation lines. */
21724 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
21725 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021726 fp->uf_tml_execed = FALSE;
21727 profile_start(&fp->uf_tml_start);
21728 profile_zero(&fp->uf_tml_children);
21729 profile_get_wait(&fp->uf_tml_wait);
21730 }
21731}
21732
21733/*
21734 * Called when actually executing a function line.
21735 */
21736 void
21737func_line_exec(cookie)
21738 void *cookie;
21739{
21740 funccall_T *fcp = (funccall_T *)cookie;
21741 ufunc_T *fp = fcp->func;
21742
21743 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21744 fp->uf_tml_execed = TRUE;
21745}
21746
21747/*
21748 * Called when done with a function line.
21749 */
21750 void
21751func_line_end(cookie)
21752 void *cookie;
21753{
21754 funccall_T *fcp = (funccall_T *)cookie;
21755 ufunc_T *fp = fcp->func;
21756
21757 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21758 {
21759 if (fp->uf_tml_execed)
21760 {
21761 ++fp->uf_tml_count[fp->uf_tml_idx];
21762 profile_end(&fp->uf_tml_start);
21763 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021764 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000021765 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
21766 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021767 }
21768 fp->uf_tml_idx = -1;
21769 }
21770}
21771#endif
21772
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773/*
21774 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021775 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776 */
21777 int
21778func_has_ended(cookie)
21779 void *cookie;
21780{
Bram Moolenaar33570922005-01-25 22:26:29 +000021781 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782
21783 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21784 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021785 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786 || fcp->returned);
21787}
21788
21789/*
21790 * return TRUE if cookie indicates a function which "abort"s on errors.
21791 */
21792 int
21793func_has_abort(cookie)
21794 void *cookie;
21795{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021796 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021797}
21798
21799#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21800typedef enum
21801{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021802 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
21803 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
21804 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021805} var_flavour_T;
21806
21807static var_flavour_T var_flavour __ARGS((char_u *varname));
21808
21809 static var_flavour_T
21810var_flavour(varname)
21811 char_u *varname;
21812{
21813 char_u *p = varname;
21814
21815 if (ASCII_ISUPPER(*p))
21816 {
21817 while (*(++p))
21818 if (ASCII_ISLOWER(*p))
21819 return VAR_FLAVOUR_SESSION;
21820 return VAR_FLAVOUR_VIMINFO;
21821 }
21822 else
21823 return VAR_FLAVOUR_DEFAULT;
21824}
21825#endif
21826
21827#if defined(FEAT_VIMINFO) || defined(PROTO)
21828/*
21829 * Restore global vars that start with a capital from the viminfo file
21830 */
21831 int
21832read_viminfo_varlist(virp, writing)
21833 vir_T *virp;
21834 int writing;
21835{
21836 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021837 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000021838 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839
21840 if (!writing && (find_viminfo_parameter('!') != NULL))
21841 {
21842 tab = vim_strchr(virp->vir_line + 1, '\t');
21843 if (tab != NULL)
21844 {
21845 *tab++ = '\0'; /* isolate the variable name */
21846 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021847 type = VAR_STRING;
21848#ifdef FEAT_FLOAT
21849 else if (*tab == 'F')
21850 type = VAR_FLOAT;
21851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852
21853 tab = vim_strchr(tab, '\t');
21854 if (tab != NULL)
21855 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021856 tv.v_type = type;
21857 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021858 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021860#ifdef FEAT_FLOAT
21861 else if (type == VAR_FLOAT)
21862 (void)string2float(tab + 1, &tv.vval.v_float);
21863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021865 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021866 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021867 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021868 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869 }
21870 }
21871 }
21872
21873 return viminfo_readline(virp);
21874}
21875
21876/*
21877 * Write global vars that start with a capital to the viminfo file
21878 */
21879 void
21880write_viminfo_varlist(fp)
21881 FILE *fp;
21882{
Bram Moolenaar33570922005-01-25 22:26:29 +000021883 hashitem_T *hi;
21884 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021885 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021886 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021887 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021888 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021889 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021890
21891 if (find_viminfo_parameter('!') == NULL)
21892 return;
21893
21894 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000021895
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021896 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021897 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021899 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021901 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021902 this_var = HI2DI(hi);
21903 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021904 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021905 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000021906 {
21907 case VAR_STRING: s = "STR"; break;
21908 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021909#ifdef FEAT_FLOAT
21910 case VAR_FLOAT: s = "FLO"; break;
21911#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000021912 default: continue;
21913 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021914 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021915 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021916 if (p != NULL)
21917 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000021918 vim_free(tofree);
21919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 }
21921 }
21922}
21923#endif
21924
21925#if defined(FEAT_SESSION) || defined(PROTO)
21926 int
21927store_session_globals(fd)
21928 FILE *fd;
21929{
Bram Moolenaar33570922005-01-25 22:26:29 +000021930 hashitem_T *hi;
21931 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021932 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933 char_u *p, *t;
21934
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021935 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021936 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021938 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021940 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021941 this_var = HI2DI(hi);
21942 if ((this_var->di_tv.v_type == VAR_NUMBER
21943 || this_var->di_tv.v_type == VAR_STRING)
21944 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021945 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021946 /* Escape special characters with a backslash. Turn a LF and
21947 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021948 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000021949 (char_u *)"\\\"\n\r");
21950 if (p == NULL) /* out of memory */
21951 break;
21952 for (t = p; *t != NUL; ++t)
21953 if (*t == '\n')
21954 *t = 'n';
21955 else if (*t == '\r')
21956 *t = 'r';
21957 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000021958 this_var->di_key,
21959 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21960 : ' ',
21961 p,
21962 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21963 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000021964 || put_eol(fd) == FAIL)
21965 {
21966 vim_free(p);
21967 return FAIL;
21968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021969 vim_free(p);
21970 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021971#ifdef FEAT_FLOAT
21972 else if (this_var->di_tv.v_type == VAR_FLOAT
21973 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
21974 {
21975 float_T f = this_var->di_tv.vval.v_float;
21976 int sign = ' ';
21977
21978 if (f < 0)
21979 {
21980 f = -f;
21981 sign = '-';
21982 }
21983 if ((fprintf(fd, "let %s = %c&%f",
21984 this_var->di_key, sign, f) < 0)
21985 || put_eol(fd) == FAIL)
21986 return FAIL;
21987 }
21988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 }
21990 }
21991 return OK;
21992}
21993#endif
21994
Bram Moolenaar661b1822005-07-28 22:36:45 +000021995/*
21996 * Display script name where an item was last set.
21997 * Should only be invoked when 'verbose' is non-zero.
21998 */
21999 void
22000last_set_msg(scriptID)
22001 scid_T scriptID;
22002{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022003 char_u *p;
22004
Bram Moolenaar661b1822005-07-28 22:36:45 +000022005 if (scriptID != 0)
22006 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022007 p = home_replace_save(NULL, get_scriptname(scriptID));
22008 if (p != NULL)
22009 {
22010 verbose_enter();
22011 MSG_PUTS(_("\n\tLast set from "));
22012 MSG_PUTS(p);
22013 vim_free(p);
22014 verbose_leave();
22015 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022016 }
22017}
22018
Bram Moolenaard812df62008-11-09 12:46:09 +000022019/*
22020 * List v:oldfiles in a nice way.
22021 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022022 void
22023ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022024 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022025{
22026 list_T *l = vimvars[VV_OLDFILES].vv_list;
22027 listitem_T *li;
22028 int nr = 0;
22029
22030 if (l == NULL)
22031 msg((char_u *)_("No old files"));
22032 else
22033 {
22034 msg_start();
22035 msg_scroll = TRUE;
22036 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22037 {
22038 msg_outnum((long)++nr);
22039 MSG_PUTS(": ");
22040 msg_outtrans(get_tv_string(&li->li_tv));
22041 msg_putchar('\n');
22042 out_flush(); /* output one line at a time */
22043 ui_breakcheck();
22044 }
22045 /* Assume "got_int" was set to truncate the listing. */
22046 got_int = FALSE;
22047
22048#ifdef FEAT_BROWSE_CMD
22049 if (cmdmod.browse)
22050 {
22051 quit_more = FALSE;
22052 nr = prompt_for_number(FALSE);
22053 msg_starthere();
22054 if (nr > 0)
22055 {
22056 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22057 (long)nr);
22058
22059 if (p != NULL)
22060 {
22061 p = expand_env_save(p);
22062 eap->arg = p;
22063 eap->cmdidx = CMD_edit;
22064 cmdmod.browse = FALSE;
22065 do_exedit(eap, NULL);
22066 vim_free(p);
22067 }
22068 }
22069 }
22070#endif
22071 }
22072}
22073
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074#endif /* FEAT_EVAL */
22075
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022077#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078
22079#ifdef WIN3264
22080/*
22081 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22082 */
22083static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22084static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22085static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22086
22087/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022088 * Get the short path (8.3) for the filename in "fnamep".
22089 * Only works for a valid file name.
22090 * When the path gets longer "fnamep" is changed and the allocated buffer
22091 * is put in "bufp".
22092 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22093 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094 */
22095 static int
22096get_short_pathname(fnamep, bufp, fnamelen)
22097 char_u **fnamep;
22098 char_u **bufp;
22099 int *fnamelen;
22100{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022101 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102 char_u *newbuf;
22103
22104 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105 l = GetShortPathName(*fnamep, *fnamep, len);
22106 if (l > len - 1)
22107 {
22108 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022109 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110 newbuf = vim_strnsave(*fnamep, l);
22111 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022112 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022113
22114 vim_free(*bufp);
22115 *fnamep = *bufp = newbuf;
22116
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022117 /* Really should always succeed, as the buffer is big enough. */
22118 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 }
22120
22121 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022122 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123}
22124
22125/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022126 * Get the short path (8.3) for the filename in "fname". The converted
22127 * path is returned in "bufp".
22128 *
22129 * Some of the directories specified in "fname" may not exist. This function
22130 * will shorten the existing directories at the beginning of the path and then
22131 * append the remaining non-existing path.
22132 *
22133 * fname - Pointer to the filename to shorten. On return, contains the
22134 * pointer to the shortened pathname
22135 * bufp - Pointer to an allocated buffer for the filename.
22136 * fnamelen - Length of the filename pointed to by fname
22137 *
22138 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139 */
22140 static int
22141shortpath_for_invalid_fname(fname, bufp, fnamelen)
22142 char_u **fname;
22143 char_u **bufp;
22144 int *fnamelen;
22145{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022146 char_u *short_fname, *save_fname, *pbuf_unused;
22147 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022148 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022149 int old_len, len;
22150 int new_len, sfx_len;
22151 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152
22153 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022154 old_len = *fnamelen;
22155 save_fname = vim_strnsave(*fname, old_len);
22156 pbuf_unused = NULL;
22157 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022159 endp = save_fname + old_len - 1; /* Find the end of the copy */
22160 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022162 /*
22163 * Try shortening the supplied path till it succeeds by removing one
22164 * directory at a time from the tail of the path.
22165 */
22166 len = 0;
22167 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022169 /* go back one path-separator */
22170 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22171 --endp;
22172 if (endp <= save_fname)
22173 break; /* processed the complete path */
22174
22175 /*
22176 * Replace the path separator with a NUL and try to shorten the
22177 * resulting path.
22178 */
22179 ch = *endp;
22180 *endp = 0;
22181 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022182 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022183 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22184 {
22185 retval = FAIL;
22186 goto theend;
22187 }
22188 *endp = ch; /* preserve the string */
22189
22190 if (len > 0)
22191 break; /* successfully shortened the path */
22192
22193 /* failed to shorten the path. Skip the path separator */
22194 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022195 }
22196
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022197 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022199 /*
22200 * Succeeded in shortening the path. Now concatenate the shortened
22201 * path with the remaining path at the tail.
22202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022203
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022204 /* Compute the length of the new path. */
22205 sfx_len = (int)(save_endp - endp) + 1;
22206 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022208 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022210 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022211 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022212 /* There is not enough space in the currently allocated string,
22213 * copy it to a buffer big enough. */
22214 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022215 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022216 {
22217 retval = FAIL;
22218 goto theend;
22219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220 }
22221 else
22222 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022223 /* Transfer short_fname to the main buffer (it's big enough),
22224 * unless get_short_pathname() did its work in-place. */
22225 *fname = *bufp = save_fname;
22226 if (short_fname != save_fname)
22227 vim_strncpy(save_fname, short_fname, len);
22228 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022229 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022230
22231 /* concat the not-shortened part of the path */
22232 vim_strncpy(*fname + len, endp, sfx_len);
22233 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022235
22236theend:
22237 vim_free(pbuf_unused);
22238 vim_free(save_fname);
22239
22240 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241}
22242
22243/*
22244 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022245 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246 */
22247 static int
22248shortpath_for_partial(fnamep, bufp, fnamelen)
22249 char_u **fnamep;
22250 char_u **bufp;
22251 int *fnamelen;
22252{
22253 int sepcount, len, tflen;
22254 char_u *p;
22255 char_u *pbuf, *tfname;
22256 int hasTilde;
22257
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022258 /* Count up the path separators from the RHS.. so we know which part
22259 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022261 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 if (vim_ispathsep(*p))
22263 ++sepcount;
22264
22265 /* Need full path first (use expand_env() to remove a "~/") */
22266 hasTilde = (**fnamep == '~');
22267 if (hasTilde)
22268 pbuf = tfname = expand_env_save(*fnamep);
22269 else
22270 pbuf = tfname = FullName_save(*fnamep, FALSE);
22271
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022272 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022273
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022274 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22275 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276
22277 if (len == 0)
22278 {
22279 /* Don't have a valid filename, so shorten the rest of the
22280 * path if we can. This CAN give us invalid 8.3 filenames, but
22281 * there's not a lot of point in guessing what it might be.
22282 */
22283 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022284 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22285 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286 }
22287
22288 /* Count the paths backward to find the beginning of the desired string. */
22289 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022290 {
22291#ifdef FEAT_MBYTE
22292 if (has_mbyte)
22293 p -= mb_head_off(tfname, p);
22294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295 if (vim_ispathsep(*p))
22296 {
22297 if (sepcount == 0 || (hasTilde && sepcount == 1))
22298 break;
22299 else
22300 sepcount --;
22301 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022303 if (hasTilde)
22304 {
22305 --p;
22306 if (p >= tfname)
22307 *p = '~';
22308 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022309 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310 }
22311 else
22312 ++p;
22313
22314 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22315 vim_free(*bufp);
22316 *fnamelen = (int)STRLEN(p);
22317 *bufp = pbuf;
22318 *fnamep = p;
22319
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022320 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022321}
22322#endif /* WIN3264 */
22323
22324/*
22325 * Adjust a filename, according to a string of modifiers.
22326 * *fnamep must be NUL terminated when called. When returning, the length is
22327 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022328 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022329 * When there is an error, *fnamep is set to NULL.
22330 */
22331 int
22332modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22333 char_u *src; /* string with modifiers */
22334 int *usedlen; /* characters after src that are used */
22335 char_u **fnamep; /* file name so far */
22336 char_u **bufp; /* buffer for allocated file name or NULL */
22337 int *fnamelen; /* length of fnamep */
22338{
22339 int valid = 0;
22340 char_u *tail;
22341 char_u *s, *p, *pbuf;
22342 char_u dirname[MAXPATHL];
22343 int c;
22344 int has_fullname = 0;
22345#ifdef WIN3264
22346 int has_shortname = 0;
22347#endif
22348
22349repeat:
22350 /* ":p" - full path/file_name */
22351 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22352 {
22353 has_fullname = 1;
22354
22355 valid |= VALID_PATH;
22356 *usedlen += 2;
22357
22358 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22359 if ((*fnamep)[0] == '~'
22360#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22361 && ((*fnamep)[1] == '/'
22362# ifdef BACKSLASH_IN_FILENAME
22363 || (*fnamep)[1] == '\\'
22364# endif
22365 || (*fnamep)[1] == NUL)
22366
22367#endif
22368 )
22369 {
22370 *fnamep = expand_env_save(*fnamep);
22371 vim_free(*bufp); /* free any allocated file name */
22372 *bufp = *fnamep;
22373 if (*fnamep == NULL)
22374 return -1;
22375 }
22376
22377 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022378 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379 {
22380 if (vim_ispathsep(*p)
22381 && p[1] == '.'
22382 && (p[2] == NUL
22383 || vim_ispathsep(p[2])
22384 || (p[2] == '.'
22385 && (p[3] == NUL || vim_ispathsep(p[3])))))
22386 break;
22387 }
22388
22389 /* FullName_save() is slow, don't use it when not needed. */
22390 if (*p != NUL || !vim_isAbsName(*fnamep))
22391 {
22392 *fnamep = FullName_save(*fnamep, *p != NUL);
22393 vim_free(*bufp); /* free any allocated file name */
22394 *bufp = *fnamep;
22395 if (*fnamep == NULL)
22396 return -1;
22397 }
22398
22399 /* Append a path separator to a directory. */
22400 if (mch_isdir(*fnamep))
22401 {
22402 /* Make room for one or two extra characters. */
22403 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22404 vim_free(*bufp); /* free any allocated file name */
22405 *bufp = *fnamep;
22406 if (*fnamep == NULL)
22407 return -1;
22408 add_pathsep(*fnamep);
22409 }
22410 }
22411
22412 /* ":." - path relative to the current directory */
22413 /* ":~" - path relative to the home directory */
22414 /* ":8" - shortname path - postponed till after */
22415 while (src[*usedlen] == ':'
22416 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22417 {
22418 *usedlen += 2;
22419 if (c == '8')
22420 {
22421#ifdef WIN3264
22422 has_shortname = 1; /* Postpone this. */
22423#endif
22424 continue;
22425 }
22426 pbuf = NULL;
22427 /* Need full path first (use expand_env() to remove a "~/") */
22428 if (!has_fullname)
22429 {
22430 if (c == '.' && **fnamep == '~')
22431 p = pbuf = expand_env_save(*fnamep);
22432 else
22433 p = pbuf = FullName_save(*fnamep, FALSE);
22434 }
22435 else
22436 p = *fnamep;
22437
22438 has_fullname = 0;
22439
22440 if (p != NULL)
22441 {
22442 if (c == '.')
22443 {
22444 mch_dirname(dirname, MAXPATHL);
22445 s = shorten_fname(p, dirname);
22446 if (s != NULL)
22447 {
22448 *fnamep = s;
22449 if (pbuf != NULL)
22450 {
22451 vim_free(*bufp); /* free any allocated file name */
22452 *bufp = pbuf;
22453 pbuf = NULL;
22454 }
22455 }
22456 }
22457 else
22458 {
22459 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22460 /* Only replace it when it starts with '~' */
22461 if (*dirname == '~')
22462 {
22463 s = vim_strsave(dirname);
22464 if (s != NULL)
22465 {
22466 *fnamep = s;
22467 vim_free(*bufp);
22468 *bufp = s;
22469 }
22470 }
22471 }
22472 vim_free(pbuf);
22473 }
22474 }
22475
22476 tail = gettail(*fnamep);
22477 *fnamelen = (int)STRLEN(*fnamep);
22478
22479 /* ":h" - head, remove "/file_name", can be repeated */
22480 /* Don't remove the first "/" or "c:\" */
22481 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22482 {
22483 valid |= VALID_HEAD;
22484 *usedlen += 2;
22485 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022486 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022487 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488 *fnamelen = (int)(tail - *fnamep);
22489#ifdef VMS
22490 if (*fnamelen > 0)
22491 *fnamelen += 1; /* the path separator is part of the path */
22492#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022493 if (*fnamelen == 0)
22494 {
22495 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22496 p = vim_strsave((char_u *)".");
22497 if (p == NULL)
22498 return -1;
22499 vim_free(*bufp);
22500 *bufp = *fnamep = tail = p;
22501 *fnamelen = 1;
22502 }
22503 else
22504 {
22505 while (tail > s && !after_pathsep(s, tail))
22506 mb_ptr_back(*fnamep, tail);
22507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022508 }
22509
22510 /* ":8" - shortname */
22511 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22512 {
22513 *usedlen += 2;
22514#ifdef WIN3264
22515 has_shortname = 1;
22516#endif
22517 }
22518
22519#ifdef WIN3264
22520 /* Check shortname after we have done 'heads' and before we do 'tails'
22521 */
22522 if (has_shortname)
22523 {
22524 pbuf = NULL;
22525 /* Copy the string if it is shortened by :h */
22526 if (*fnamelen < (int)STRLEN(*fnamep))
22527 {
22528 p = vim_strnsave(*fnamep, *fnamelen);
22529 if (p == 0)
22530 return -1;
22531 vim_free(*bufp);
22532 *bufp = *fnamep = p;
22533 }
22534
22535 /* Split into two implementations - makes it easier. First is where
22536 * there isn't a full name already, second is where there is.
22537 */
22538 if (!has_fullname && !vim_isAbsName(*fnamep))
22539 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022540 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022541 return -1;
22542 }
22543 else
22544 {
22545 int l;
22546
22547 /* Simple case, already have the full-name
22548 * Nearly always shorter, so try first time. */
22549 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022550 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551 return -1;
22552
22553 if (l == 0)
22554 {
22555 /* Couldn't find the filename.. search the paths.
22556 */
22557 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022558 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022559 return -1;
22560 }
22561 *fnamelen = l;
22562 }
22563 }
22564#endif /* WIN3264 */
22565
22566 /* ":t" - tail, just the basename */
22567 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22568 {
22569 *usedlen += 2;
22570 *fnamelen -= (int)(tail - *fnamep);
22571 *fnamep = tail;
22572 }
22573
22574 /* ":e" - extension, can be repeated */
22575 /* ":r" - root, without extension, can be repeated */
22576 while (src[*usedlen] == ':'
22577 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22578 {
22579 /* find a '.' in the tail:
22580 * - for second :e: before the current fname
22581 * - otherwise: The last '.'
22582 */
22583 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22584 s = *fnamep - 2;
22585 else
22586 s = *fnamep + *fnamelen - 1;
22587 for ( ; s > tail; --s)
22588 if (s[0] == '.')
22589 break;
22590 if (src[*usedlen + 1] == 'e') /* :e */
22591 {
22592 if (s > tail)
22593 {
22594 *fnamelen += (int)(*fnamep - (s + 1));
22595 *fnamep = s + 1;
22596#ifdef VMS
22597 /* cut version from the extension */
22598 s = *fnamep + *fnamelen - 1;
22599 for ( ; s > *fnamep; --s)
22600 if (s[0] == ';')
22601 break;
22602 if (s > *fnamep)
22603 *fnamelen = s - *fnamep;
22604#endif
22605 }
22606 else if (*fnamep <= tail)
22607 *fnamelen = 0;
22608 }
22609 else /* :r */
22610 {
22611 if (s > tail) /* remove one extension */
22612 *fnamelen = (int)(s - *fnamep);
22613 }
22614 *usedlen += 2;
22615 }
22616
22617 /* ":s?pat?foo?" - substitute */
22618 /* ":gs?pat?foo?" - global substitute */
22619 if (src[*usedlen] == ':'
22620 && (src[*usedlen + 1] == 's'
22621 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
22622 {
22623 char_u *str;
22624 char_u *pat;
22625 char_u *sub;
22626 int sep;
22627 char_u *flags;
22628 int didit = FALSE;
22629
22630 flags = (char_u *)"";
22631 s = src + *usedlen + 2;
22632 if (src[*usedlen + 1] == 'g')
22633 {
22634 flags = (char_u *)"g";
22635 ++s;
22636 }
22637
22638 sep = *s++;
22639 if (sep)
22640 {
22641 /* find end of pattern */
22642 p = vim_strchr(s, sep);
22643 if (p != NULL)
22644 {
22645 pat = vim_strnsave(s, (int)(p - s));
22646 if (pat != NULL)
22647 {
22648 s = p + 1;
22649 /* find end of substitution */
22650 p = vim_strchr(s, sep);
22651 if (p != NULL)
22652 {
22653 sub = vim_strnsave(s, (int)(p - s));
22654 str = vim_strnsave(*fnamep, *fnamelen);
22655 if (sub != NULL && str != NULL)
22656 {
22657 *usedlen = (int)(p + 1 - src);
22658 s = do_string_sub(str, pat, sub, flags);
22659 if (s != NULL)
22660 {
22661 *fnamep = s;
22662 *fnamelen = (int)STRLEN(s);
22663 vim_free(*bufp);
22664 *bufp = s;
22665 didit = TRUE;
22666 }
22667 }
22668 vim_free(sub);
22669 vim_free(str);
22670 }
22671 vim_free(pat);
22672 }
22673 }
22674 /* after using ":s", repeat all the modifiers */
22675 if (didit)
22676 goto repeat;
22677 }
22678 }
22679
22680 return valid;
22681}
22682
22683/*
22684 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22685 * "flags" can be "g" to do a global substitute.
22686 * Returns an allocated string, NULL for error.
22687 */
22688 char_u *
22689do_string_sub(str, pat, sub, flags)
22690 char_u *str;
22691 char_u *pat;
22692 char_u *sub;
22693 char_u *flags;
22694{
22695 int sublen;
22696 regmatch_T regmatch;
22697 int i;
22698 int do_all;
22699 char_u *tail;
22700 garray_T ga;
22701 char_u *ret;
22702 char_u *save_cpo;
22703
22704 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22705 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022706 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022707
22708 ga_init2(&ga, 1, 200);
22709
22710 do_all = (flags[0] == 'g');
22711
22712 regmatch.rm_ic = p_ic;
22713 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
22714 if (regmatch.regprog != NULL)
22715 {
22716 tail = str;
22717 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
22718 {
22719 /*
22720 * Get some space for a temporary buffer to do the substitution
22721 * into. It will contain:
22722 * - The text up to where the match is.
22723 * - The substituted text.
22724 * - The text after the match.
22725 */
22726 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
22727 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
22728 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
22729 {
22730 ga_clear(&ga);
22731 break;
22732 }
22733
22734 /* copy the text up to where the match is */
22735 i = (int)(regmatch.startp[0] - tail);
22736 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
22737 /* add the substituted text */
22738 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
22739 + ga.ga_len + i, TRUE, TRUE, FALSE);
22740 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741 /* avoid getting stuck on a match with an empty string */
22742 if (tail == regmatch.endp[0])
22743 {
22744 if (*tail == NUL)
22745 break;
22746 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
22747 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022748 }
22749 else
22750 {
22751 tail = regmatch.endp[0];
22752 if (*tail == NUL)
22753 break;
22754 }
22755 if (!do_all)
22756 break;
22757 }
22758
22759 if (ga.ga_data != NULL)
22760 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
22761
22762 vim_free(regmatch.regprog);
22763 }
22764
22765 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
22766 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022767 if (p_cpo == empty_option)
22768 p_cpo = save_cpo;
22769 else
22770 /* Darn, evaluating {sub} expression changed the value. */
22771 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772
22773 return ret;
22774}
22775
22776#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */