blob: 2e5b8766c62dc160900cf6e94fe316c8dec34a0d [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
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200148static 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));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000436static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
438static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
439static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000440static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static char_u *list2string __ARGS((typval_T *tv, int copyID));
443static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000444static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000445static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
446static void set_ref_in_list __ARGS((list_T *l, int copyID));
447static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000449static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000450static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
451static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000452static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static long dict_len __ARGS((dict_T *d));
454static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000455static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000457static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
458static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
461static int string2float __ARGS((char_u *text, float_T *value));
462#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
464static int find_internal_func __ARGS((char_u *name));
465static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
466static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +0200467static int call_func __ARGS((char_u *func_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 +0000468static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000469static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#ifdef FEAT_FLOAT
472static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200473static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000475static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000480#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200481static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200483static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#ifdef FEAT_FLOAT
497static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
498#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000499static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000500static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000502static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000504#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000505static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000506static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
508#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#ifdef FEAT_FLOAT
512static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200513static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#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));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200529#ifdef FEAT_FLOAT
530static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
531#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000532static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000534static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000540#ifdef FEAT_FLOAT
541static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200543static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000544#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000545static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000546static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000554static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000556static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000562static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000570static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000571static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000572static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000573static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200576static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000577static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000585static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000599static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000605static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000617#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200618static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000619static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000626static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000627static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000629static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000633#ifdef vim_mkdir
634static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
635#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100637#ifdef FEAT_MZSCHEME
638static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000642static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000643#ifdef FEAT_FLOAT
644static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
645#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000646static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000647static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000648static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000649static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000650static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000651static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000663#ifdef FEAT_FLOAT
664static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
665#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000667static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000669static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000676static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000677static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000678static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000679static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200681static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000682static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000684static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000686#ifdef FEAT_FLOAT
687static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200688static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000691static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000692static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
693static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000695#ifdef FEAT_FLOAT
696static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
697static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
698#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000699static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700#ifdef HAVE_STRFTIME
701static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
702#endif
703static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000714static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000716static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000717static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000718static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000719static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000720static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000722static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200723#ifdef FEAT_FLOAT
724static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
726#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000730#ifdef FEAT_FLOAT
731static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
732#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200734static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000744static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000747static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000748
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000749static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000750static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static int get_env_len __ARGS((char_u **arg));
752static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000753static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000754static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
755#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
756#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
757 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000758static 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 +0000759static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000760static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000761static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
762static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static typval_T *alloc_tv __ARGS((void));
764static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static void init_tv __ARGS((typval_T *varp));
766static long get_tv_number __ARGS((typval_T *varp));
767static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000768static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static char_u *get_tv_string __ARGS((typval_T *varp));
770static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000771static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000772static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000773static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
775static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
776static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000777static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
778static 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 +0000779static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
780static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000781static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000782static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000783static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
785static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
786static int eval_fname_script __ARGS((char_u *p));
787static int eval_fname_sid __ARGS((char_u *p));
788static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000789static ufunc_T *find_func __ARGS((char_u *name));
790static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000791static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000792#ifdef FEAT_PROFILE
793static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000794static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
795static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
796static int
797# ifdef __BORLANDC__
798 _RTLENTRYF
799# endif
800 prof_total_cmp __ARGS((const void *s1, const void *s2));
801static int
802# ifdef __BORLANDC__
803 _RTLENTRYF
804# endif
805 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000807static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000808static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000809static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000810static void func_free __ARGS((ufunc_T *fp));
811static void func_unref __ARGS((char_u *name));
812static void func_ref __ARGS((char_u *name));
813static 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 +0000814static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
815static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000816static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000817static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
818static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000819static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000820static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000821static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200823
824#ifdef EBCDIC
825static int compare_func_name __ARGS((const void *s1, const void *s2));
826static void sortFunctions __ARGS(());
827#endif
828
829
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000830/* Character used as separated in autoload function/variable names. */
831#define AUTOLOAD_CHAR '#'
832
Bram Moolenaar33570922005-01-25 22:26:29 +0000833/*
834 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000835 */
836 void
837eval_init()
838{
Bram Moolenaar33570922005-01-25 22:26:29 +0000839 int i;
840 struct vimvar *p;
841
842 init_var_dict(&globvardict, &globvars_var);
843 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000844 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000845 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000846
847 for (i = 0; i < VV_LEN; ++i)
848 {
849 p = &vimvars[i];
850 STRCPY(p->vv_di.di_key, p->vv_name);
851 if (p->vv_flags & VV_RO)
852 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
853 else if (p->vv_flags & VV_RO_SBX)
854 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
855 else
856 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000857
858 /* add to v: scope dict, unless the value is not always available */
859 if (p->vv_type != VAR_UNKNOWN)
860 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000861 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000862 /* add to compat scope dict */
863 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000864 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000865 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200866
867#ifdef EBCDIC
868 /*
869 * Sort the function table, to enable binary sort.
870 */
871 sortFunctions();
872#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000873}
874
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000875#if defined(EXITFREE) || defined(PROTO)
876 void
877eval_clear()
878{
879 int i;
880 struct vimvar *p;
881
882 for (i = 0; i < VV_LEN; ++i)
883 {
884 p = &vimvars[i];
885 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000886 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000887 vim_free(p->vv_str);
888 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000889 }
890 else if (p->vv_di.di_tv.v_type == VAR_LIST)
891 {
892 list_unref(p->vv_list);
893 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000894 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000895 }
896 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000897 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000898 hash_clear(&compat_hashtab);
899
Bram Moolenaard9fba312005-06-26 22:34:35 +0000900 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000901
902 /* global variables */
903 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000904
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000905 /* autoloaded script names */
906 ga_clear_strings(&ga_loaded);
907
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200908 /* script-local variables */
909 for (i = 1; i <= ga_scripts.ga_len; ++i)
910 {
911 vars_clear(&SCRIPT_VARS(i));
912 vim_free(SCRIPT_SV(i));
913 }
914 ga_clear(&ga_scripts);
915
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000916 /* unreferenced lists and dicts */
917 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000918
919 /* functions */
920 free_all_functions();
921 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000922}
923#endif
924
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 * Return the name of the executed function.
927 */
928 char_u *
929func_name(cookie)
930 void *cookie;
931{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000932 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933}
934
935/*
936 * Return the address holding the next breakpoint line for a funccall cookie.
937 */
938 linenr_T *
939func_breakpoint(cookie)
940 void *cookie;
941{
Bram Moolenaar33570922005-01-25 22:26:29 +0000942 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943}
944
945/*
946 * Return the address holding the debug tick for a funccall cookie.
947 */
948 int *
949func_dbg_tick(cookie)
950 void *cookie;
951{
Bram Moolenaar33570922005-01-25 22:26:29 +0000952 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953}
954
955/*
956 * Return the nesting level for a funccall cookie.
957 */
958 int
959func_level(cookie)
960 void *cookie;
961{
Bram Moolenaar33570922005-01-25 22:26:29 +0000962 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000966funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000968/* pointer to list of previously used funccal, still around because some
969 * item in it is still being used. */
970funccall_T *previous_funccal = NULL;
971
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972/*
973 * Return TRUE when a function was ended by a ":return" command.
974 */
975 int
976current_func_returned()
977{
978 return current_funccal->returned;
979}
980
981
982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 * Set an internal variable to a string value. Creates the variable if it does
984 * not already exist.
985 */
986 void
987set_internal_string_var(name, value)
988 char_u *name;
989 char_u *value;
990{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000991 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000992 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994 val = vim_strsave(value);
995 if (val != NULL)
996 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000997 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000998 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001000 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001001 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 }
1003 }
1004}
1005
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001006static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001007static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001008static char_u *redir_endp = NULL;
1009static char_u *redir_varname = NULL;
1010
1011/*
1012 * Start recording command output to a variable
1013 * Returns OK if successfully completed the setup. FAIL otherwise.
1014 */
1015 int
1016var_redir_start(name, append)
1017 char_u *name;
1018 int append; /* append to an existing variable */
1019{
1020 int save_emsg;
1021 int err;
1022 typval_T tv;
1023
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001024 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001025 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001026 {
1027 EMSG(_(e_invarg));
1028 return FAIL;
1029 }
1030
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001031 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001032 redir_varname = vim_strsave(name);
1033 if (redir_varname == NULL)
1034 return FAIL;
1035
1036 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1037 if (redir_lval == NULL)
1038 {
1039 var_redir_stop();
1040 return FAIL;
1041 }
1042
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001043 /* The output is stored in growarray "redir_ga" until redirection ends. */
1044 ga_init2(&redir_ga, (int)sizeof(char), 500);
1045
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001047 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1048 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1050 {
1051 if (redir_endp != NULL && *redir_endp != NUL)
1052 /* Trailing characters are present after the variable name */
1053 EMSG(_(e_trailing));
1054 else
1055 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001056 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 var_redir_stop();
1058 return FAIL;
1059 }
1060
1061 /* check if we can write to the variable: set it to or append an empty
1062 * string */
1063 save_emsg = did_emsg;
1064 did_emsg = FALSE;
1065 tv.v_type = VAR_STRING;
1066 tv.vval.v_string = (char_u *)"";
1067 if (append)
1068 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1069 else
1070 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1071 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001072 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001073 if (err)
1074 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001075 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 var_redir_stop();
1077 return FAIL;
1078 }
1079 if (redir_lval->ll_newkey != NULL)
1080 {
1081 /* Dictionary item was created, don't do it again. */
1082 vim_free(redir_lval->ll_newkey);
1083 redir_lval->ll_newkey = NULL;
1084 }
1085
1086 return OK;
1087}
1088
1089/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001090 * Append "value[value_len]" to the variable set by var_redir_start().
1091 * The actual appending is postponed until redirection ends, because the value
1092 * appended may in fact be the string we write to, changing it may cause freed
1093 * memory to be used:
1094 * :redir => foo
1095 * :let foo
1096 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097 */
1098 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001099var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001101 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001103 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104
1105 if (redir_lval == NULL)
1106 return;
1107
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001108 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001109 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001111 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001113 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 {
1115 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001116 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001117 }
1118 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120}
1121
1122/*
1123 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001124 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 */
1126 void
1127var_redir_stop()
1128{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001129 typval_T tv;
1130
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 if (redir_lval != NULL)
1132 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001133 /* If there was no error: assign the text to the variable. */
1134 if (redir_endp != NULL)
1135 {
1136 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1137 tv.v_type = VAR_STRING;
1138 tv.vval.v_string = redir_ga.ga_data;
1139 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1140 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001141
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001142 /* free the collected output */
1143 vim_free(redir_ga.ga_data);
1144 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001145
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146 clear_lval(redir_lval);
1147 vim_free(redir_lval);
1148 redir_lval = NULL;
1149 }
1150 vim_free(redir_varname);
1151 redir_varname = NULL;
1152}
1153
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154# if defined(FEAT_MBYTE) || defined(PROTO)
1155 int
1156eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1157 char_u *enc_from;
1158 char_u *enc_to;
1159 char_u *fname_from;
1160 char_u *fname_to;
1161{
1162 int err = FALSE;
1163
1164 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1165 set_vim_var_string(VV_CC_TO, enc_to, -1);
1166 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1167 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1168 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1169 err = TRUE;
1170 set_vim_var_string(VV_CC_FROM, NULL, -1);
1171 set_vim_var_string(VV_CC_TO, NULL, -1);
1172 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1173 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1174
1175 if (err)
1176 return FAIL;
1177 return OK;
1178}
1179# endif
1180
1181# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1182 int
1183eval_printexpr(fname, args)
1184 char_u *fname;
1185 char_u *args;
1186{
1187 int err = FALSE;
1188
1189 set_vim_var_string(VV_FNAME_IN, fname, -1);
1190 set_vim_var_string(VV_CMDARG, args, -1);
1191 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1192 err = TRUE;
1193 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1194 set_vim_var_string(VV_CMDARG, NULL, -1);
1195
1196 if (err)
1197 {
1198 mch_remove(fname);
1199 return FAIL;
1200 }
1201 return OK;
1202}
1203# endif
1204
1205# if defined(FEAT_DIFF) || defined(PROTO)
1206 void
1207eval_diff(origfile, newfile, outfile)
1208 char_u *origfile;
1209 char_u *newfile;
1210 char_u *outfile;
1211{
1212 int err = FALSE;
1213
1214 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1215 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1216 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1217 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1218 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1219 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1220 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1221}
1222
1223 void
1224eval_patch(origfile, difffile, outfile)
1225 char_u *origfile;
1226 char_u *difffile;
1227 char_u *outfile;
1228{
1229 int err;
1230
1231 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1232 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1233 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1234 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1235 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1236 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1237 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1238}
1239# endif
1240
1241/*
1242 * Top level evaluation function, returning a boolean.
1243 * Sets "error" to TRUE if there was an error.
1244 * Return TRUE or FALSE.
1245 */
1246 int
1247eval_to_bool(arg, error, nextcmd, skip)
1248 char_u *arg;
1249 int *error;
1250 char_u **nextcmd;
1251 int skip; /* only parse, don't execute */
1252{
Bram Moolenaar33570922005-01-25 22:26:29 +00001253 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 int retval = FALSE;
1255
1256 if (skip)
1257 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001258 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 else
1261 {
1262 *error = FALSE;
1263 if (!skip)
1264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001265 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001266 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 }
1268 }
1269 if (skip)
1270 --emsg_skip;
1271
1272 return retval;
1273}
1274
1275/*
1276 * Top level evaluation function, returning a string. If "skip" is TRUE,
1277 * only parsing to "nextcmd" is done, without reporting errors. Return
1278 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1279 */
1280 char_u *
1281eval_to_string_skip(arg, nextcmd, skip)
1282 char_u *arg;
1283 char_u **nextcmd;
1284 int skip; /* only parse, don't execute */
1285{
Bram Moolenaar33570922005-01-25 22:26:29 +00001286 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 char_u *retval;
1288
1289 if (skip)
1290 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001291 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 retval = NULL;
1293 else
1294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001295 retval = vim_strsave(get_tv_string(&tv));
1296 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 }
1298 if (skip)
1299 --emsg_skip;
1300
1301 return retval;
1302}
1303
1304/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001305 * Skip over an expression at "*pp".
1306 * Return FAIL for an error, OK otherwise.
1307 */
1308 int
1309skip_expr(pp)
1310 char_u **pp;
1311{
Bram Moolenaar33570922005-01-25 22:26:29 +00001312 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001313
1314 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001315 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001316}
1317
1318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001320 * When "convert" is TRUE convert a List into a sequence of lines and convert
1321 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 * Return pointer to allocated memory, or NULL for failure.
1323 */
1324 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001325eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 char_u *arg;
1327 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001328 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329{
Bram Moolenaar33570922005-01-25 22:26:29 +00001330 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001332 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001333#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001337 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 retval = NULL;
1339 else
1340 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001341 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001342 {
1343 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001344 if (tv.vval.v_list != NULL)
1345 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001346 ga_append(&ga, NUL);
1347 retval = (char_u *)ga.ga_data;
1348 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349#ifdef FEAT_FLOAT
1350 else if (convert && tv.v_type == VAR_FLOAT)
1351 {
1352 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1353 retval = vim_strsave(numbuf);
1354 }
1355#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 else
1357 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001358 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 }
1360
1361 return retval;
1362}
1363
1364/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001365 * Call eval_to_string() without using current local variables and using
1366 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 */
1368 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001369eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 char_u *arg;
1371 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001372 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373{
1374 char_u *retval;
1375 void *save_funccalp;
1376
1377 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001378 if (use_sandbox)
1379 ++sandbox;
1380 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001382 if (use_sandbox)
1383 --sandbox;
1384 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 restore_funccal(save_funccalp);
1386 return retval;
1387}
1388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389/*
1390 * Top level evaluation function, returning a number.
1391 * Evaluates "expr" silently.
1392 * Returns -1 for an error.
1393 */
1394 int
1395eval_to_number(expr)
1396 char_u *expr;
1397{
Bram Moolenaar33570922005-01-25 22:26:29 +00001398 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001400 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401
1402 ++emsg_off;
1403
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001404 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 retval = -1;
1406 else
1407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001408 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001409 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 }
1411 --emsg_off;
1412
1413 return retval;
1414}
1415
Bram Moolenaara40058a2005-07-11 22:42:07 +00001416/*
1417 * Prepare v: variable "idx" to be used.
1418 * Save the current typeval in "save_tv".
1419 * When not used yet add the variable to the v: hashtable.
1420 */
1421 static void
1422prepare_vimvar(idx, save_tv)
1423 int idx;
1424 typval_T *save_tv;
1425{
1426 *save_tv = vimvars[idx].vv_tv;
1427 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1428 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1429}
1430
1431/*
1432 * Restore v: variable "idx" to typeval "save_tv".
1433 * When no longer defined, remove the variable from the v: hashtable.
1434 */
1435 static void
1436restore_vimvar(idx, save_tv)
1437 int idx;
1438 typval_T *save_tv;
1439{
1440 hashitem_T *hi;
1441
Bram Moolenaara40058a2005-07-11 22:42:07 +00001442 vimvars[idx].vv_tv = *save_tv;
1443 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1444 {
1445 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1446 if (HASHITEM_EMPTY(hi))
1447 EMSG2(_(e_intern2), "restore_vimvar()");
1448 else
1449 hash_remove(&vimvarht, hi);
1450 }
1451}
1452
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001453#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001454/*
1455 * Evaluate an expression to a list with suggestions.
1456 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001457 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001458 */
1459 list_T *
1460eval_spell_expr(badword, expr)
1461 char_u *badword;
1462 char_u *expr;
1463{
1464 typval_T save_val;
1465 typval_T rettv;
1466 list_T *list = NULL;
1467 char_u *p = skipwhite(expr);
1468
1469 /* Set "v:val" to the bad word. */
1470 prepare_vimvar(VV_VAL, &save_val);
1471 vimvars[VV_VAL].vv_type = VAR_STRING;
1472 vimvars[VV_VAL].vv_str = badword;
1473 if (p_verbose == 0)
1474 ++emsg_off;
1475
1476 if (eval1(&p, &rettv, TRUE) == OK)
1477 {
1478 if (rettv.v_type != VAR_LIST)
1479 clear_tv(&rettv);
1480 else
1481 list = rettv.vval.v_list;
1482 }
1483
1484 if (p_verbose == 0)
1485 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001486 restore_vimvar(VV_VAL, &save_val);
1487
1488 return list;
1489}
1490
1491/*
1492 * "list" is supposed to contain two items: a word and a number. Return the
1493 * word in "pp" and the number as the return value.
1494 * Return -1 if anything isn't right.
1495 * Used to get the good word and score from the eval_spell_expr() result.
1496 */
1497 int
1498get_spellword(list, pp)
1499 list_T *list;
1500 char_u **pp;
1501{
1502 listitem_T *li;
1503
1504 li = list->lv_first;
1505 if (li == NULL)
1506 return -1;
1507 *pp = get_tv_string(&li->li_tv);
1508
1509 li = li->li_next;
1510 if (li == NULL)
1511 return -1;
1512 return get_tv_number(&li->li_tv);
1513}
1514#endif
1515
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001516/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001517 * Top level evaluation function.
1518 * Returns an allocated typval_T with the result.
1519 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001520 */
1521 typval_T *
1522eval_expr(arg, nextcmd)
1523 char_u *arg;
1524 char_u **nextcmd;
1525{
1526 typval_T *tv;
1527
1528 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001529 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001530 {
1531 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001532 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001533 }
1534
1535 return tv;
1536}
1537
1538
Bram Moolenaar4f688582007-07-24 12:34:30 +00001539#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1540 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001542 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001543 * Uses argv[argc] for the function arguments. Only Number and String
1544 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001545 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001547 static int
1548call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 char_u *func;
1550 int argc;
1551 char_u **argv;
1552 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
Bram Moolenaar33570922005-01-25 22:26:29 +00001555 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 long n;
1557 int len;
1558 int i;
1559 int doesrange;
1560 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001563 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
1567 for (i = 0; i < argc; i++)
1568 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001569 /* Pass a NULL or empty argument as an empty string */
1570 if (argv[i] == NULL || *argv[i] == NUL)
1571 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001572 argvars[i].v_type = VAR_STRING;
1573 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001574 continue;
1575 }
1576
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 /* Recognize a number argument, the others must be strings. */
1578 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1579 if (len != 0 && len == (int)STRLEN(argv[i]))
1580 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001581 argvars[i].v_type = VAR_NUMBER;
1582 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 }
1584 else
1585 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001586 argvars[i].v_type = VAR_STRING;
1587 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 }
1589 }
1590
1591 if (safe)
1592 {
1593 save_funccalp = save_funccal();
1594 ++sandbox;
1595 }
1596
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001597 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1598 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001600 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 if (safe)
1602 {
1603 --sandbox;
1604 restore_funccal(save_funccalp);
1605 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001606 vim_free(argvars);
1607
1608 if (ret == FAIL)
1609 clear_tv(rettv);
1610
1611 return ret;
1612}
1613
Bram Moolenaar4f688582007-07-24 12:34:30 +00001614# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001616 * Call vimL function "func" and return the result as a string.
1617 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001618 * Uses argv[argc] for the function arguments.
1619 */
1620 void *
1621call_func_retstr(func, argc, argv, safe)
1622 char_u *func;
1623 int argc;
1624 char_u **argv;
1625 int safe; /* use the sandbox */
1626{
1627 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001628 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001629
1630 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1631 return NULL;
1632
1633 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 return retval;
1636}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001637# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001638
Bram Moolenaar4f688582007-07-24 12:34:30 +00001639# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001640/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001641 * Call vimL function "func" and return the result as a number.
1642 * Returns -1 when calling the function fails.
1643 * Uses argv[argc] for the function arguments.
1644 */
1645 long
1646call_func_retnr(func, argc, argv, safe)
1647 char_u *func;
1648 int argc;
1649 char_u **argv;
1650 int safe; /* use the sandbox */
1651{
1652 typval_T rettv;
1653 long retval;
1654
1655 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1656 return -1;
1657
1658 retval = get_tv_number_chk(&rettv, NULL);
1659 clear_tv(&rettv);
1660 return retval;
1661}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001662# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001663
1664/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001665 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001666 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001667 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001668 */
1669 void *
1670call_func_retlist(func, argc, argv, safe)
1671 char_u *func;
1672 int argc;
1673 char_u **argv;
1674 int safe; /* use the sandbox */
1675{
1676 typval_T rettv;
1677
1678 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1679 return NULL;
1680
1681 if (rettv.v_type != VAR_LIST)
1682 {
1683 clear_tv(&rettv);
1684 return NULL;
1685 }
1686
1687 return rettv.vval.v_list;
1688}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689#endif
1690
Bram Moolenaar4f688582007-07-24 12:34:30 +00001691
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692/*
1693 * Save the current function call pointer, and set it to NULL.
1694 * Used when executing autocommands and for ":source".
1695 */
1696 void *
1697save_funccal()
1698{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001699 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 current_funccal = NULL;
1702 return (void *)fc;
1703}
1704
1705 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001706restore_funccal(vfc)
1707 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001709 funccall_T *fc = (funccall_T *)vfc;
1710
1711 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712}
1713
Bram Moolenaar05159a02005-02-26 23:04:13 +00001714#if defined(FEAT_PROFILE) || defined(PROTO)
1715/*
1716 * Prepare profiling for entering a child or something else that is not
1717 * counted for the script/function itself.
1718 * Should always be called in pair with prof_child_exit().
1719 */
1720 void
1721prof_child_enter(tm)
1722 proftime_T *tm; /* place to store waittime */
1723{
1724 funccall_T *fc = current_funccal;
1725
1726 if (fc != NULL && fc->func->uf_profiling)
1727 profile_start(&fc->prof_child);
1728 script_prof_save(tm);
1729}
1730
1731/*
1732 * Take care of time spent in a child.
1733 * Should always be called after prof_child_enter().
1734 */
1735 void
1736prof_child_exit(tm)
1737 proftime_T *tm; /* where waittime was stored */
1738{
1739 funccall_T *fc = current_funccal;
1740
1741 if (fc != NULL && fc->func->uf_profiling)
1742 {
1743 profile_end(&fc->prof_child);
1744 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1745 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1746 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1747 }
1748 script_prof_restore(tm);
1749}
1750#endif
1751
1752
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753#ifdef FEAT_FOLDING
1754/*
1755 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1756 * it in "*cp". Doesn't give error messages.
1757 */
1758 int
1759eval_foldexpr(arg, cp)
1760 char_u *arg;
1761 int *cp;
1762{
Bram Moolenaar33570922005-01-25 22:26:29 +00001763 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 int retval;
1765 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001766 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1767 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
1769 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001770 if (use_sandbox)
1771 ++sandbox;
1772 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001774 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 retval = 0;
1776 else
1777 {
1778 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779 if (tv.v_type == VAR_NUMBER)
1780 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001781 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 retval = 0;
1783 else
1784 {
1785 /* If the result is a string, check if there is a non-digit before
1786 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 if (!VIM_ISDIGIT(*s) && *s != '-')
1789 *cp = *s++;
1790 retval = atol((char *)s);
1791 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001792 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 }
1794 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001795 if (use_sandbox)
1796 --sandbox;
1797 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
1799 return retval;
1800}
1801#endif
1802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001804 * ":let" list all variable values
1805 * ":let var1 var2" list variable values
1806 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001807 * ":let var += expr" assignment command.
1808 * ":let var -= expr" assignment command.
1809 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001810 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 */
1812 void
1813ex_let(eap)
1814 exarg_T *eap;
1815{
1816 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001818 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 int var_count = 0;
1821 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001822 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001823 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001824 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
Bram Moolenaardb552d602006-03-23 22:59:57 +00001826 argend = skip_var_list(arg, &var_count, &semicolon);
1827 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001828 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001829 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1830 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001831 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001832 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001834 /*
1835 * ":let" without "=": list variables
1836 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001837 if (*arg == '[')
1838 EMSG(_(e_invarg));
1839 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001841 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001843 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001845 list_glob_vars(&first);
1846 list_buf_vars(&first);
1847 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001848#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001849 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001850#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001851 list_script_vars(&first);
1852 list_func_vars(&first);
1853 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 eap->nextcmd = check_nextcmd(arg);
1856 }
1857 else
1858 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001859 op[0] = '=';
1860 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001861 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001862 {
1863 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1864 op[0] = expr[-1]; /* +=, -= or .= */
1865 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001866 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 if (eap->skip)
1869 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001870 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if (eap->skip)
1872 {
1873 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 --emsg_skip;
1876 }
1877 else if (i != FAIL)
1878 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001880 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 }
1883 }
1884}
1885
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886/*
1887 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1888 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001889 * When "nextchars" is not NULL it points to a string with characters that
1890 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1891 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 * Returns OK or FAIL;
1893 */
1894 static int
1895ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1896 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001897 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001898 int copy; /* copy values from "tv", don't move */
1899 int semicolon; /* from skip_var_list() */
1900 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902{
1903 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001904 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001906 listitem_T *item;
1907 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001908
1909 if (*arg != '[')
1910 {
1911 /*
1912 * ":let var = expr" or ":for var in list"
1913 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 return FAIL;
1916 return OK;
1917 }
1918
1919 /*
1920 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1921 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001922 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923 {
1924 EMSG(_(e_listreq));
1925 return FAIL;
1926 }
1927
1928 i = list_len(l);
1929 if (semicolon == 0 && var_count < i)
1930 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001931 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 return FAIL;
1933 }
1934 if (var_count - semicolon > i)
1935 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001936 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 return FAIL;
1938 }
1939
1940 item = l->lv_first;
1941 while (*arg != ']')
1942 {
1943 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945 item = item->li_next;
1946 if (arg == NULL)
1947 return FAIL;
1948
1949 arg = skipwhite(arg);
1950 if (*arg == ';')
1951 {
1952 /* Put the rest of the list (may be empty) in the var after ';'.
1953 * Create a new list for this. */
1954 l = list_alloc();
1955 if (l == NULL)
1956 return FAIL;
1957 while (item != NULL)
1958 {
1959 list_append_tv(l, &item->li_tv);
1960 item = item->li_next;
1961 }
1962
1963 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001964 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 ltv.vval.v_list = l;
1966 l->lv_refcount = 1;
1967
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001968 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1969 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970 clear_tv(&ltv);
1971 if (arg == NULL)
1972 return FAIL;
1973 break;
1974 }
1975 else if (*arg != ',' && *arg != ']')
1976 {
1977 EMSG2(_(e_intern2), "ex_let_vars()");
1978 return FAIL;
1979 }
1980 }
1981
1982 return OK;
1983}
1984
1985/*
1986 * Skip over assignable variable "var" or list of variables "[var, var]".
1987 * Used for ":let varvar = expr" and ":for varvar in expr".
1988 * For "[var, var]" increment "*var_count" for each variable.
1989 * for "[var, var; var]" set "semicolon".
1990 * Return NULL for an error.
1991 */
1992 static char_u *
1993skip_var_list(arg, var_count, semicolon)
1994 char_u *arg;
1995 int *var_count;
1996 int *semicolon;
1997{
1998 char_u *p, *s;
1999
2000 if (*arg == '[')
2001 {
2002 /* "[var, var]": find the matching ']'. */
2003 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002004 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002005 {
2006 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2007 s = skip_var_one(p);
2008 if (s == p)
2009 {
2010 EMSG2(_(e_invarg2), p);
2011 return NULL;
2012 }
2013 ++*var_count;
2014
2015 p = skipwhite(s);
2016 if (*p == ']')
2017 break;
2018 else if (*p == ';')
2019 {
2020 if (*semicolon == 1)
2021 {
2022 EMSG(_("Double ; in list of variables"));
2023 return NULL;
2024 }
2025 *semicolon = 1;
2026 }
2027 else if (*p != ',')
2028 {
2029 EMSG2(_(e_invarg2), p);
2030 return NULL;
2031 }
2032 }
2033 return p + 1;
2034 }
2035 else
2036 return skip_var_one(arg);
2037}
2038
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002039/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002040 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002041 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002042 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002043 static char_u *
2044skip_var_one(arg)
2045 char_u *arg;
2046{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002047 if (*arg == '@' && arg[1] != NUL)
2048 return arg + 2;
2049 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2050 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051}
2052
Bram Moolenaara7043832005-01-21 11:56:39 +00002053/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002054 * List variables for hashtab "ht" with prefix "prefix".
2055 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002056 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002057 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002058list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002059 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002060 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002061 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002062 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002063{
Bram Moolenaar33570922005-01-25 22:26:29 +00002064 hashitem_T *hi;
2065 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002066 int todo;
2067
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002068 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002069 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2070 {
2071 if (!HASHITEM_EMPTY(hi))
2072 {
2073 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002074 di = HI2DI(hi);
2075 if (empty || di->di_tv.v_type != VAR_STRING
2076 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002077 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002078 }
2079 }
2080}
2081
2082/*
2083 * List global variables.
2084 */
2085 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002086list_glob_vars(first)
2087 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002088{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002089 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002090}
2091
2092/*
2093 * List buffer variables.
2094 */
2095 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002096list_buf_vars(first)
2097 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002098{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002099 char_u numbuf[NUMBUFLEN];
2100
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2102 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002103
2104 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2106 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002107}
2108
2109/*
2110 * List window variables.
2111 */
2112 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113list_win_vars(first)
2114 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002115{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2117 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002118}
2119
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002120#ifdef FEAT_WINDOWS
2121/*
2122 * List tab page variables.
2123 */
2124 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125list_tab_vars(first)
2126 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002127{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2129 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002130}
2131#endif
2132
Bram Moolenaara7043832005-01-21 11:56:39 +00002133/*
2134 * List Vim variables.
2135 */
2136 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137list_vim_vars(first)
2138 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002139{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002141}
2142
2143/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002144 * List script-local variables, if there is a script.
2145 */
2146 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147list_script_vars(first)
2148 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002149{
2150 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2152 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002153}
2154
2155/*
2156 * List function variables, if there is a function.
2157 */
2158 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159list_func_vars(first)
2160 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002161{
2162 if (current_funccal != NULL)
2163 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002165}
2166
2167/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168 * List variables in "arg".
2169 */
2170 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 exarg_T *eap;
2173 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175{
2176 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002177 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002178 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002179 char_u *name_start;
2180 char_u *arg_subsc;
2181 char_u *tofree;
2182 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183
2184 while (!ends_excmd(*arg) && !got_int)
2185 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002186 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002188 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002189 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2190 {
2191 emsg_severe = TRUE;
2192 EMSG(_(e_trailing));
2193 break;
2194 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002196 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002198 /* get_name_len() takes care of expanding curly braces */
2199 name_start = name = arg;
2200 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2201 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002203 /* This is mainly to keep test 49 working: when expanding
2204 * curly braces fails overrule the exception error message. */
2205 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 emsg_severe = TRUE;
2208 EMSG2(_(e_invarg2), arg);
2209 break;
2210 }
2211 error = TRUE;
2212 }
2213 else
2214 {
2215 if (tofree != NULL)
2216 name = tofree;
2217 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 else
2220 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221 /* handle d.key, l[idx], f(expr) */
2222 arg_subsc = arg;
2223 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002224 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002226 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002227 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002230 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 case 'g': list_glob_vars(first); break;
2232 case 'b': list_buf_vars(first); break;
2233 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002234#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002235 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002236#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002237 case 'v': list_vim_vars(first); break;
2238 case 's': list_script_vars(first); break;
2239 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 default:
2241 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002242 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 }
2244 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 {
2246 char_u numbuf[NUMBUFLEN];
2247 char_u *tf;
2248 int c;
2249 char_u *s;
2250
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002251 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 c = *arg;
2253 *arg = NUL;
2254 list_one_var_a((char_u *)"",
2255 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002256 tv.v_type,
2257 s == NULL ? (char_u *)"" : s,
2258 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 *arg = c;
2260 vim_free(tf);
2261 }
2262 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 }
2265 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266
2267 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269
2270 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 }
2272
2273 return arg;
2274}
2275
2276/*
2277 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2278 * Returns a pointer to the char just after the var name.
2279 * Returns NULL if there is an error.
2280 */
2281 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002282ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002284 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002286 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002287 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288{
2289 int c1;
2290 char_u *name;
2291 char_u *p;
2292 char_u *arg_end = NULL;
2293 int len;
2294 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002295 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296
2297 /*
2298 * ":let $VAR = expr": Set environment variable.
2299 */
2300 if (*arg == '$')
2301 {
2302 /* Find the end of the name. */
2303 ++arg;
2304 name = arg;
2305 len = get_env_len(&arg);
2306 if (len == 0)
2307 EMSG2(_(e_invarg2), name - 1);
2308 else
2309 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002310 if (op != NULL && (*op == '+' || *op == '-'))
2311 EMSG2(_(e_letwrong), op);
2312 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002313 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 EMSG(_(e_letunexp));
2315 else
2316 {
2317 c1 = name[len];
2318 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002319 p = get_tv_string_chk(tv);
2320 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002321 {
2322 int mustfree = FALSE;
2323 char_u *s = vim_getenv(name, &mustfree);
2324
2325 if (s != NULL)
2326 {
2327 p = tofree = concat_str(s, p);
2328 if (mustfree)
2329 vim_free(s);
2330 }
2331 }
2332 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002333 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002334 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002335 if (STRICMP(name, "HOME") == 0)
2336 init_homedir();
2337 else if (didset_vim && STRICMP(name, "VIM") == 0)
2338 didset_vim = FALSE;
2339 else if (didset_vimruntime
2340 && STRICMP(name, "VIMRUNTIME") == 0)
2341 didset_vimruntime = FALSE;
2342 arg_end = arg;
2343 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002345 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 }
2347 }
2348 }
2349
2350 /*
2351 * ":let &option = expr": Set option value.
2352 * ":let &l:option = expr": Set local option value.
2353 * ":let &g:option = expr": Set global option value.
2354 */
2355 else if (*arg == '&')
2356 {
2357 /* Find the end of the name. */
2358 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002359 if (p == NULL || (endchars != NULL
2360 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 EMSG(_(e_letunexp));
2362 else
2363 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 long n;
2365 int opt_type;
2366 long numval;
2367 char_u *stringval = NULL;
2368 char_u *s;
2369
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 c1 = *p;
2371 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372
2373 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002374 s = get_tv_string_chk(tv); /* != NULL if number or string */
2375 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002376 {
2377 opt_type = get_option_value(arg, &numval,
2378 &stringval, opt_flags);
2379 if ((opt_type == 1 && *op == '.')
2380 || (opt_type == 0 && *op != '.'))
2381 EMSG2(_(e_letwrong), op);
2382 else
2383 {
2384 if (opt_type == 1) /* number */
2385 {
2386 if (*op == '+')
2387 n = numval + n;
2388 else
2389 n = numval - n;
2390 }
2391 else if (opt_type == 0 && stringval != NULL) /* string */
2392 {
2393 s = concat_str(stringval, s);
2394 vim_free(stringval);
2395 stringval = s;
2396 }
2397 }
2398 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002399 if (s != NULL)
2400 {
2401 set_option_value(arg, n, s, opt_flags);
2402 arg_end = p;
2403 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406 }
2407 }
2408
2409 /*
2410 * ":let @r = expr": Set register contents.
2411 */
2412 else if (*arg == '@')
2413 {
2414 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 if (op != NULL && (*op == '+' || *op == '-'))
2416 EMSG2(_(e_letwrong), op);
2417 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002418 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 EMSG(_(e_letunexp));
2420 else
2421 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002422 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423 char_u *s;
2424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002425 p = get_tv_string_chk(tv);
2426 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002428 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 if (s != NULL)
2430 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002431 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 vim_free(s);
2433 }
2434 }
2435 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002436 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002438 arg_end = arg + 1;
2439 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002440 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002441 }
2442 }
2443
2444 /*
2445 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002448 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002450 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002452 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002454 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2456 EMSG(_(e_letunexp));
2457 else
2458 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 arg_end = p;
2461 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 }
2465
2466 else
2467 EMSG2(_(e_invarg2), arg);
2468
2469 return arg_end;
2470}
2471
2472/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002473 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2474 */
2475 static int
2476check_changedtick(arg)
2477 char_u *arg;
2478{
2479 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2480 {
2481 EMSG2(_(e_readonlyvar), arg);
2482 return TRUE;
2483 }
2484 return FALSE;
2485}
2486
2487/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 * Get an lval: variable, Dict item or List item that can be assigned a value
2489 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2490 * "name.key", "name.key[expr]" etc.
2491 * Indexing only works if "name" is an existing List or Dictionary.
2492 * "name" points to the start of the name.
2493 * If "rettv" is not NULL it points to the value to be assigned.
2494 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2495 * wrong; must end in space or cmd separator.
2496 *
2497 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002498 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 */
2501 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002502get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002503 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002504 typval_T *rettv;
2505 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 int unlet;
2507 int skip;
2508 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002509 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 char_u *expr_start, *expr_end;
2513 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002514 dictitem_T *v;
2515 typval_T var1;
2516 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002518 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002521 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002524 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525
2526 if (skip)
2527 {
2528 /* When skipping just find the end of the name. */
2529 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002530 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 }
2532
2533 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002534 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 if (expr_start != NULL)
2536 {
2537 /* Don't expand the name when we already know there is an error. */
2538 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2539 && *p != '[' && *p != '.')
2540 {
2541 EMSG(_(e_trailing));
2542 return NULL;
2543 }
2544
2545 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2546 if (lp->ll_exp_name == NULL)
2547 {
2548 /* Report an invalid expression in braces, unless the
2549 * expression evaluation has been cancelled due to an
2550 * aborting error, an interrupt, or an exception. */
2551 if (!aborting() && !quiet)
2552 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002553 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 EMSG2(_(e_invarg2), name);
2555 return NULL;
2556 }
2557 }
2558 lp->ll_name = lp->ll_exp_name;
2559 }
2560 else
2561 lp->ll_name = name;
2562
2563 /* Without [idx] or .key we are done. */
2564 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2565 return p;
2566
2567 cc = *p;
2568 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002569 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 if (v == NULL && !quiet)
2571 EMSG2(_(e_undefvar), lp->ll_name);
2572 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573 if (v == NULL)
2574 return NULL;
2575
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 /*
2577 * Loop until no more [idx] or .key is following.
2578 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002579 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2583 && !(lp->ll_tv->v_type == VAR_DICT
2584 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002585 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 if (!quiet)
2587 EMSG(_("E689: Can only index a List or Dictionary"));
2588 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002589 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002591 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 if (!quiet)
2593 EMSG(_("E708: [:] must come last"));
2594 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002595 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002596
Bram Moolenaar8c711452005-01-14 21:53:12 +00002597 len = -1;
2598 if (*p == '.')
2599 {
2600 key = p + 1;
2601 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2602 ;
2603 if (len == 0)
2604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (!quiet)
2606 EMSG(_(e_emptykey));
2607 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 }
2609 p = key + len;
2610 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002611 else
2612 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002614 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 if (*p == ':')
2616 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002617 else
2618 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 empty1 = FALSE;
2620 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002622 if (get_tv_string_chk(&var1) == NULL)
2623 {
2624 /* not a number or string */
2625 clear_tv(&var1);
2626 return NULL;
2627 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 }
2629
2630 /* Optionally get the second index [ :expr]. */
2631 if (*p == ':')
2632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002636 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637 if (!empty1)
2638 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002640 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 if (rettv != NULL && (rettv->v_type != VAR_LIST
2642 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (!quiet)
2645 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 if (!empty1)
2647 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 }
2650 p = skipwhite(p + 1);
2651 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 else
2654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2657 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 if (!empty1)
2659 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002662 if (get_tv_string_chk(&var2) == NULL)
2663 {
2664 /* not a number or string */
2665 if (!empty1)
2666 clear_tv(&var1);
2667 clear_tv(&var2);
2668 return NULL;
2669 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002672 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002675
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002677 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 if (!quiet)
2679 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 if (!empty1)
2681 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 }
2686
2687 /* Skip to past ']'. */
2688 ++p;
2689 }
2690
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 {
2693 if (len == -1)
2694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002696 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 if (*key == NUL)
2698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (!quiet)
2700 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 }
2704 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002705 lp->ll_list = NULL;
2706 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002707 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002710 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002714 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 if (len == -1)
2716 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (len == -1)
2724 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 p = NULL;
2727 break;
2728 }
2729 if (len == -1)
2730 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
2733 else
2734 {
2735 /*
2736 * Get the number and item for the only or first index of the List.
2737 */
2738 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 else
2741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002742 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 clear_tv(&var1);
2744 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002745 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 lp->ll_list = lp->ll_tv->vval.v_list;
2747 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2748 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002750 if (lp->ll_n1 < 0)
2751 {
2752 lp->ll_n1 = 0;
2753 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2754 }
2755 }
2756 if (lp->ll_li == NULL)
2757 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 }
2762
2763 /*
2764 * May need to find the item or absolute index for the second
2765 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 * When no index given: "lp->ll_empty2" is TRUE.
2767 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002771 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 }
2780
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2782 if (lp->ll_n1 < 0)
2783 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2784 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002786 }
2787
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002790 }
2791
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 return p;
2793}
2794
2795/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002796 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 */
2798 static void
2799clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002800 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801{
2802 vim_free(lp->ll_exp_name);
2803 vim_free(lp->ll_newkey);
2804}
2805
2806/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002807 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002809 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 */
2811 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002812set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002813 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002815 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002817 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818{
2819 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002820 listitem_T *ri;
2821 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822
2823 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002824 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002826 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 cc = *endp;
2828 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002829 if (op != NULL && *op != '=')
2830 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002831 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002832
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002833 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002834 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002835 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002836 {
2837 if (tv_op(&tv, rettv, op) == OK)
2838 set_var(lp->ll_name, &tv, FALSE);
2839 clear_tv(&tv);
2840 }
2841 }
2842 else
2843 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002845 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002847 else if (tv_check_lock(lp->ll_newkey == NULL
2848 ? lp->ll_tv->v_lock
2849 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2850 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 else if (lp->ll_range)
2852 {
2853 /*
2854 * Assign the List values to the list items.
2855 */
2856 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002857 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002858 if (op != NULL && *op != '=')
2859 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2860 else
2861 {
2862 clear_tv(&lp->ll_li->li_tv);
2863 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2864 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 ri = ri->li_next;
2866 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2867 break;
2868 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002871 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002872 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 ri = NULL;
2874 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002875 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002876 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 lp->ll_li = lp->ll_li->li_next;
2878 ++lp->ll_n1;
2879 }
2880 if (ri != NULL)
2881 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002882 else if (lp->ll_empty2
2883 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 : lp->ll_n1 != lp->ll_n2)
2885 EMSG(_("E711: List value has not enough items"));
2886 }
2887 else
2888 {
2889 /*
2890 * Assign to a List or Dictionary item.
2891 */
2892 if (lp->ll_newkey != NULL)
2893 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002894 if (op != NULL && *op != '=')
2895 {
2896 EMSG2(_(e_letwrong), op);
2897 return;
2898 }
2899
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002901 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 if (di == NULL)
2903 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002904 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2905 {
2906 vim_free(di);
2907 return;
2908 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002910 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002911 else if (op != NULL && *op != '=')
2912 {
2913 tv_op(lp->ll_tv, rettv, op);
2914 return;
2915 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002916 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002918
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 /*
2920 * Assign the value to the variable or list item.
2921 */
2922 if (copy)
2923 copy_tv(rettv, lp->ll_tv);
2924 else
2925 {
2926 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002927 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002929 }
2930 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002931}
2932
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002933/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002934 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2935 * Returns OK or FAIL.
2936 */
2937 static int
2938tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 typval_T *tv1;
2940 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002941 char_u *op;
2942{
2943 long n;
2944 char_u numbuf[NUMBUFLEN];
2945 char_u *s;
2946
2947 /* Can't do anything with a Funcref or a Dict on the right. */
2948 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2949 {
2950 switch (tv1->v_type)
2951 {
2952 case VAR_DICT:
2953 case VAR_FUNC:
2954 break;
2955
2956 case VAR_LIST:
2957 if (*op != '+' || tv2->v_type != VAR_LIST)
2958 break;
2959 /* List += List */
2960 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2961 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2962 return OK;
2963
2964 case VAR_NUMBER:
2965 case VAR_STRING:
2966 if (tv2->v_type == VAR_LIST)
2967 break;
2968 if (*op == '+' || *op == '-')
2969 {
2970 /* nr += nr or nr -= nr*/
2971 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002972#ifdef FEAT_FLOAT
2973 if (tv2->v_type == VAR_FLOAT)
2974 {
2975 float_T f = n;
2976
2977 if (*op == '+')
2978 f += tv2->vval.v_float;
2979 else
2980 f -= tv2->vval.v_float;
2981 clear_tv(tv1);
2982 tv1->v_type = VAR_FLOAT;
2983 tv1->vval.v_float = f;
2984 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002985 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002986#endif
2987 {
2988 if (*op == '+')
2989 n += get_tv_number(tv2);
2990 else
2991 n -= get_tv_number(tv2);
2992 clear_tv(tv1);
2993 tv1->v_type = VAR_NUMBER;
2994 tv1->vval.v_number = n;
2995 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 }
2997 else
2998 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002999 if (tv2->v_type == VAR_FLOAT)
3000 break;
3001
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003002 /* str .= str */
3003 s = get_tv_string(tv1);
3004 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3005 clear_tv(tv1);
3006 tv1->v_type = VAR_STRING;
3007 tv1->vval.v_string = s;
3008 }
3009 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003010
3011#ifdef FEAT_FLOAT
3012 case VAR_FLOAT:
3013 {
3014 float_T f;
3015
3016 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3017 && tv2->v_type != VAR_NUMBER
3018 && tv2->v_type != VAR_STRING))
3019 break;
3020 if (tv2->v_type == VAR_FLOAT)
3021 f = tv2->vval.v_float;
3022 else
3023 f = get_tv_number(tv2);
3024 if (*op == '+')
3025 tv1->vval.v_float += f;
3026 else
3027 tv1->vval.v_float -= f;
3028 }
3029 return OK;
3030#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003031 }
3032 }
3033
3034 EMSG2(_(e_letwrong), op);
3035 return FAIL;
3036}
3037
3038/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003039 * Add a watcher to a list.
3040 */
3041 static void
3042list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003043 list_T *l;
3044 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003045{
3046 lw->lw_next = l->lv_watch;
3047 l->lv_watch = lw;
3048}
3049
3050/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003051 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003052 * No warning when it isn't found...
3053 */
3054 static void
3055list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003056 list_T *l;
3057 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003058{
Bram Moolenaar33570922005-01-25 22:26:29 +00003059 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003060
3061 lwp = &l->lv_watch;
3062 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3063 {
3064 if (lw == lwrem)
3065 {
3066 *lwp = lw->lw_next;
3067 break;
3068 }
3069 lwp = &lw->lw_next;
3070 }
3071}
3072
3073/*
3074 * Just before removing an item from a list: advance watchers to the next
3075 * item.
3076 */
3077 static void
3078list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003079 list_T *l;
3080 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003081{
Bram Moolenaar33570922005-01-25 22:26:29 +00003082 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003083
3084 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3085 if (lw->lw_item == item)
3086 lw->lw_item = item->li_next;
3087}
3088
3089/*
3090 * Evaluate the expression used in a ":for var in expr" command.
3091 * "arg" points to "var".
3092 * Set "*errp" to TRUE for an error, FALSE otherwise;
3093 * Return a pointer that holds the info. Null when there is an error.
3094 */
3095 void *
3096eval_for_line(arg, errp, nextcmdp, skip)
3097 char_u *arg;
3098 int *errp;
3099 char_u **nextcmdp;
3100 int skip;
3101{
Bram Moolenaar33570922005-01-25 22:26:29 +00003102 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003103 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003104 typval_T tv;
3105 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003106
3107 *errp = TRUE; /* default: there is an error */
3108
Bram Moolenaar33570922005-01-25 22:26:29 +00003109 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003110 if (fi == NULL)
3111 return NULL;
3112
3113 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3114 if (expr == NULL)
3115 return fi;
3116
3117 expr = skipwhite(expr);
3118 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3119 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003120 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003121 return fi;
3122 }
3123
3124 if (skip)
3125 ++emsg_skip;
3126 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3127 {
3128 *errp = FALSE;
3129 if (!skip)
3130 {
3131 l = tv.vval.v_list;
3132 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003133 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003135 clear_tv(&tv);
3136 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003137 else
3138 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003139 /* No need to increment the refcount, it's already set for the
3140 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003141 fi->fi_list = l;
3142 list_add_watch(l, &fi->fi_lw);
3143 fi->fi_lw.lw_item = l->lv_first;
3144 }
3145 }
3146 }
3147 if (skip)
3148 --emsg_skip;
3149
3150 return fi;
3151}
3152
3153/*
3154 * Use the first item in a ":for" list. Advance to the next.
3155 * Assign the values to the variable (list). "arg" points to the first one.
3156 * Return TRUE when a valid item was found, FALSE when at end of list or
3157 * something wrong.
3158 */
3159 int
3160next_for_item(fi_void, arg)
3161 void *fi_void;
3162 char_u *arg;
3163{
Bram Moolenaar33570922005-01-25 22:26:29 +00003164 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003165 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003166 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167
3168 item = fi->fi_lw.lw_item;
3169 if (item == NULL)
3170 result = FALSE;
3171 else
3172 {
3173 fi->fi_lw.lw_item = item->li_next;
3174 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3175 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3176 }
3177 return result;
3178}
3179
3180/*
3181 * Free the structure used to store info used by ":for".
3182 */
3183 void
3184free_for_info(fi_void)
3185 void *fi_void;
3186{
Bram Moolenaar33570922005-01-25 22:26:29 +00003187 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003189 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003190 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003192 list_unref(fi->fi_list);
3193 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194 vim_free(fi);
3195}
3196
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3198
3199 void
3200set_context_for_expression(xp, arg, cmdidx)
3201 expand_T *xp;
3202 char_u *arg;
3203 cmdidx_T cmdidx;
3204{
3205 int got_eq = FALSE;
3206 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003207 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003209 if (cmdidx == CMD_let)
3210 {
3211 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003212 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213 {
3214 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003215 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216 {
3217 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003218 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219 if (vim_iswhite(*p))
3220 break;
3221 }
3222 return;
3223 }
3224 }
3225 else
3226 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3227 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 while ((xp->xp_pattern = vim_strpbrk(arg,
3229 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3230 {
3231 c = *xp->xp_pattern;
3232 if (c == '&')
3233 {
3234 c = xp->xp_pattern[1];
3235 if (c == '&')
3236 {
3237 ++xp->xp_pattern;
3238 xp->xp_context = cmdidx != CMD_let || got_eq
3239 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3240 }
3241 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003244 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3245 xp->xp_pattern += 2;
3246
3247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 }
3249 else if (c == '$')
3250 {
3251 /* environment variable */
3252 xp->xp_context = EXPAND_ENV_VARS;
3253 }
3254 else if (c == '=')
3255 {
3256 got_eq = TRUE;
3257 xp->xp_context = EXPAND_EXPRESSION;
3258 }
3259 else if (c == '<'
3260 && xp->xp_context == EXPAND_FUNCTIONS
3261 && vim_strchr(xp->xp_pattern, '(') == NULL)
3262 {
3263 /* Function name can start with "<SNR>" */
3264 break;
3265 }
3266 else if (cmdidx != CMD_let || got_eq)
3267 {
3268 if (c == '"') /* string */
3269 {
3270 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3271 if (c == '\\' && xp->xp_pattern[1] != NUL)
3272 ++xp->xp_pattern;
3273 xp->xp_context = EXPAND_NOTHING;
3274 }
3275 else if (c == '\'') /* literal string */
3276 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003277 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3279 /* skip */ ;
3280 xp->xp_context = EXPAND_NOTHING;
3281 }
3282 else if (c == '|')
3283 {
3284 if (xp->xp_pattern[1] == '|')
3285 {
3286 ++xp->xp_pattern;
3287 xp->xp_context = EXPAND_EXPRESSION;
3288 }
3289 else
3290 xp->xp_context = EXPAND_COMMANDS;
3291 }
3292 else
3293 xp->xp_context = EXPAND_EXPRESSION;
3294 }
3295 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296 /* Doesn't look like something valid, expand as an expression
3297 * anyway. */
3298 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 arg = xp->xp_pattern;
3300 if (*arg != NUL)
3301 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3302 /* skip */ ;
3303 }
3304 xp->xp_pattern = arg;
3305}
3306
3307#endif /* FEAT_CMDL_COMPL */
3308
3309/*
3310 * ":1,25call func(arg1, arg2)" function call.
3311 */
3312 void
3313ex_call(eap)
3314 exarg_T *eap;
3315{
3316 char_u *arg = eap->arg;
3317 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003319 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003321 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 linenr_T lnum;
3323 int doesrange;
3324 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003325 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003327 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003328 if (fudi.fd_newkey != NULL)
3329 {
3330 /* Still need to give an error message for missing key. */
3331 EMSG2(_(e_dictkey), fudi.fd_newkey);
3332 vim_free(fudi.fd_newkey);
3333 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003334 if (tofree == NULL)
3335 return;
3336
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003337 /* Increase refcount on dictionary, it could get deleted when evaluating
3338 * the arguments. */
3339 if (fudi.fd_dict != NULL)
3340 ++fudi.fd_dict->dv_refcount;
3341
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003342 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003343 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003344 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345
Bram Moolenaar532c7802005-01-27 14:44:31 +00003346 /* Skip white space to allow ":call func ()". Not good, but required for
3347 * backward compatibility. */
3348 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003349 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351 if (*startarg != '(')
3352 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003353 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 goto end;
3355 }
3356
3357 /*
3358 * When skipping, evaluate the function once, to find the end of the
3359 * arguments.
3360 * When the function takes a range, this is discovered after the first
3361 * call, and the loop is broken.
3362 */
3363 if (eap->skip)
3364 {
3365 ++emsg_skip;
3366 lnum = eap->line2; /* do it once, also with an invalid range */
3367 }
3368 else
3369 lnum = eap->line1;
3370 for ( ; lnum <= eap->line2; ++lnum)
3371 {
3372 if (!eap->skip && eap->addr_count > 0)
3373 {
3374 curwin->w_cursor.lnum = lnum;
3375 curwin->w_cursor.col = 0;
3376 }
3377 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003378 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003379 eap->line1, eap->line2, &doesrange,
3380 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 {
3382 failed = TRUE;
3383 break;
3384 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003385
3386 /* Handle a function returning a Funcref, Dictionary or List. */
3387 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3388 {
3389 failed = TRUE;
3390 break;
3391 }
3392
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003393 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 if (doesrange || eap->skip)
3395 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003396
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003398 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003399 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003400 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 if (aborting())
3402 break;
3403 }
3404 if (eap->skip)
3405 --emsg_skip;
3406
3407 if (!failed)
3408 {
3409 /* Check for trailing illegal characters and a following command. */
3410 if (!ends_excmd(*arg))
3411 {
3412 emsg_severe = TRUE;
3413 EMSG(_(e_trailing));
3414 }
3415 else
3416 eap->nextcmd = check_nextcmd(arg);
3417 }
3418
3419end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003420 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003421 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422}
3423
3424/*
3425 * ":unlet[!] var1 ... " command.
3426 */
3427 void
3428ex_unlet(eap)
3429 exarg_T *eap;
3430{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003431 ex_unletlock(eap, eap->arg, 0);
3432}
3433
3434/*
3435 * ":lockvar" and ":unlockvar" commands
3436 */
3437 void
3438ex_lockvar(eap)
3439 exarg_T *eap;
3440{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003442 int deep = 2;
3443
3444 if (eap->forceit)
3445 deep = -1;
3446 else if (vim_isdigit(*arg))
3447 {
3448 deep = getdigits(&arg);
3449 arg = skipwhite(arg);
3450 }
3451
3452 ex_unletlock(eap, arg, deep);
3453}
3454
3455/*
3456 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3457 */
3458 static void
3459ex_unletlock(eap, argstart, deep)
3460 exarg_T *eap;
3461 char_u *argstart;
3462 int deep;
3463{
3464 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003467 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
3469 do
3470 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003471 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003472 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3473 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003474 if (lv.ll_name == NULL)
3475 error = TRUE; /* error but continue parsing */
3476 if (name_end == NULL || (!vim_iswhite(*name_end)
3477 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003479 if (name_end != NULL)
3480 {
3481 emsg_severe = TRUE;
3482 EMSG(_(e_trailing));
3483 }
3484 if (!(eap->skip || error))
3485 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 break;
3487 }
3488
3489 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003490 {
3491 if (eap->cmdidx == CMD_unlet)
3492 {
3493 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3494 error = TRUE;
3495 }
3496 else
3497 {
3498 if (do_lock_var(&lv, name_end, deep,
3499 eap->cmdidx == CMD_lockvar) == FAIL)
3500 error = TRUE;
3501 }
3502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003504 if (!eap->skip)
3505 clear_lval(&lv);
3506
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 arg = skipwhite(name_end);
3508 } while (!ends_excmd(*arg));
3509
3510 eap->nextcmd = check_nextcmd(arg);
3511}
3512
Bram Moolenaar8c711452005-01-14 21:53:12 +00003513 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003514do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003515 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003516 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003517 int forceit;
3518{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003519 int ret = OK;
3520 int cc;
3521
3522 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003523 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003524 cc = *name_end;
3525 *name_end = NUL;
3526
3527 /* Normal name or expanded name. */
3528 if (check_changedtick(lp->ll_name))
3529 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003530 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003531 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003532 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003533 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003534 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3535 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003536 else if (lp->ll_range)
3537 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003538 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003539
3540 /* Delete a range of List items. */
3541 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3542 {
3543 li = lp->ll_li->li_next;
3544 listitem_remove(lp->ll_list, lp->ll_li);
3545 lp->ll_li = li;
3546 ++lp->ll_n1;
3547 }
3548 }
3549 else
3550 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003551 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003552 /* unlet a List item. */
3553 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003554 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003555 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003556 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003557 }
3558
3559 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003560}
3561
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562/*
3563 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003564 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 */
3566 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003567do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003569 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
Bram Moolenaar33570922005-01-25 22:26:29 +00003571 hashtab_T *ht;
3572 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003573 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003574 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
Bram Moolenaar33570922005-01-25 22:26:29 +00003576 ht = find_var_ht(name, &varname);
3577 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003579 hi = hash_find(ht, varname);
3580 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003581 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003582 di = HI2DI(hi);
3583 if (var_check_fixed(di->di_flags, name)
3584 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003585 return FAIL;
3586 delete_var(ht, hi);
3587 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 if (forceit)
3591 return OK;
3592 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 return FAIL;
3594}
3595
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003596/*
3597 * Lock or unlock variable indicated by "lp".
3598 * "deep" is the levels to go (-1 for unlimited);
3599 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3600 */
3601 static int
3602do_lock_var(lp, name_end, deep, lock)
3603 lval_T *lp;
3604 char_u *name_end;
3605 int deep;
3606 int lock;
3607{
3608 int ret = OK;
3609 int cc;
3610 dictitem_T *di;
3611
3612 if (deep == 0) /* nothing to do */
3613 return OK;
3614
3615 if (lp->ll_tv == NULL)
3616 {
3617 cc = *name_end;
3618 *name_end = NUL;
3619
3620 /* Normal name or expanded name. */
3621 if (check_changedtick(lp->ll_name))
3622 ret = FAIL;
3623 else
3624 {
3625 di = find_var(lp->ll_name, NULL);
3626 if (di == NULL)
3627 ret = FAIL;
3628 else
3629 {
3630 if (lock)
3631 di->di_flags |= DI_FLAGS_LOCK;
3632 else
3633 di->di_flags &= ~DI_FLAGS_LOCK;
3634 item_lock(&di->di_tv, deep, lock);
3635 }
3636 }
3637 *name_end = cc;
3638 }
3639 else if (lp->ll_range)
3640 {
3641 listitem_T *li = lp->ll_li;
3642
3643 /* (un)lock a range of List items. */
3644 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3645 {
3646 item_lock(&li->li_tv, deep, lock);
3647 li = li->li_next;
3648 ++lp->ll_n1;
3649 }
3650 }
3651 else if (lp->ll_list != NULL)
3652 /* (un)lock a List item. */
3653 item_lock(&lp->ll_li->li_tv, deep, lock);
3654 else
3655 /* un(lock) a Dictionary item. */
3656 item_lock(&lp->ll_di->di_tv, deep, lock);
3657
3658 return ret;
3659}
3660
3661/*
3662 * Lock or unlock an item. "deep" is nr of levels to go.
3663 */
3664 static void
3665item_lock(tv, deep, lock)
3666 typval_T *tv;
3667 int deep;
3668 int lock;
3669{
3670 static int recurse = 0;
3671 list_T *l;
3672 listitem_T *li;
3673 dict_T *d;
3674 hashitem_T *hi;
3675 int todo;
3676
3677 if (recurse >= DICT_MAXNEST)
3678 {
3679 EMSG(_("E743: variable nested too deep for (un)lock"));
3680 return;
3681 }
3682 if (deep == 0)
3683 return;
3684 ++recurse;
3685
3686 /* lock/unlock the item itself */
3687 if (lock)
3688 tv->v_lock |= VAR_LOCKED;
3689 else
3690 tv->v_lock &= ~VAR_LOCKED;
3691
3692 switch (tv->v_type)
3693 {
3694 case VAR_LIST:
3695 if ((l = tv->vval.v_list) != NULL)
3696 {
3697 if (lock)
3698 l->lv_lock |= VAR_LOCKED;
3699 else
3700 l->lv_lock &= ~VAR_LOCKED;
3701 if (deep < 0 || deep > 1)
3702 /* recursive: lock/unlock the items the List contains */
3703 for (li = l->lv_first; li != NULL; li = li->li_next)
3704 item_lock(&li->li_tv, deep - 1, lock);
3705 }
3706 break;
3707 case VAR_DICT:
3708 if ((d = tv->vval.v_dict) != NULL)
3709 {
3710 if (lock)
3711 d->dv_lock |= VAR_LOCKED;
3712 else
3713 d->dv_lock &= ~VAR_LOCKED;
3714 if (deep < 0 || deep > 1)
3715 {
3716 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003717 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003718 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3719 {
3720 if (!HASHITEM_EMPTY(hi))
3721 {
3722 --todo;
3723 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3724 }
3725 }
3726 }
3727 }
3728 }
3729 --recurse;
3730}
3731
Bram Moolenaara40058a2005-07-11 22:42:07 +00003732/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003733 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3734 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003735 */
3736 static int
3737tv_islocked(tv)
3738 typval_T *tv;
3739{
3740 return (tv->v_lock & VAR_LOCKED)
3741 || (tv->v_type == VAR_LIST
3742 && tv->vval.v_list != NULL
3743 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3744 || (tv->v_type == VAR_DICT
3745 && tv->vval.v_dict != NULL
3746 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3747}
3748
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3750/*
3751 * Delete all "menutrans_" variables.
3752 */
3753 void
3754del_menutrans_vars()
3755{
Bram Moolenaar33570922005-01-25 22:26:29 +00003756 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003757 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758
Bram Moolenaar33570922005-01-25 22:26:29 +00003759 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003760 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003761 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003762 {
3763 if (!HASHITEM_EMPTY(hi))
3764 {
3765 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003766 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3767 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003768 }
3769 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003770 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771}
3772#endif
3773
3774#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3775
3776/*
3777 * Local string buffer for the next two functions to store a variable name
3778 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3779 * get_user_var_name().
3780 */
3781
3782static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3783
3784static char_u *varnamebuf = NULL;
3785static int varnamebuflen = 0;
3786
3787/*
3788 * Function to concatenate a prefix and a variable name.
3789 */
3790 static char_u *
3791cat_prefix_varname(prefix, name)
3792 int prefix;
3793 char_u *name;
3794{
3795 int len;
3796
3797 len = (int)STRLEN(name) + 3;
3798 if (len > varnamebuflen)
3799 {
3800 vim_free(varnamebuf);
3801 len += 10; /* some additional space */
3802 varnamebuf = alloc(len);
3803 if (varnamebuf == NULL)
3804 {
3805 varnamebuflen = 0;
3806 return NULL;
3807 }
3808 varnamebuflen = len;
3809 }
3810 *varnamebuf = prefix;
3811 varnamebuf[1] = ':';
3812 STRCPY(varnamebuf + 2, name);
3813 return varnamebuf;
3814}
3815
3816/*
3817 * Function given to ExpandGeneric() to obtain the list of user defined
3818 * (global/buffer/window/built-in) variable names.
3819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 char_u *
3821get_user_var_name(xp, idx)
3822 expand_T *xp;
3823 int idx;
3824{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003825 static long_u gdone;
3826 static long_u bdone;
3827 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003828#ifdef FEAT_WINDOWS
3829 static long_u tdone;
3830#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003831 static int vidx;
3832 static hashitem_T *hi;
3833 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
3835 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003836 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003837 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003838#ifdef FEAT_WINDOWS
3839 tdone = 0;
3840#endif
3841 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003842
3843 /* Global variables */
3844 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003846 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003847 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003848 else
3849 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003850 while (HASHITEM_EMPTY(hi))
3851 ++hi;
3852 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3853 return cat_prefix_varname('g', hi->hi_key);
3854 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003856
3857 /* b: variables */
3858 ht = &curbuf->b_vars.dv_hashtab;
3859 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003861 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003862 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003863 else
3864 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003865 while (HASHITEM_EMPTY(hi))
3866 ++hi;
3867 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003869 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003871 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 return (char_u *)"b:changedtick";
3873 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003874
3875 /* w: variables */
3876 ht = &curwin->w_vars.dv_hashtab;
3877 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003879 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003880 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003881 else
3882 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003883 while (HASHITEM_EMPTY(hi))
3884 ++hi;
3885 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003887
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003888#ifdef FEAT_WINDOWS
3889 /* t: variables */
3890 ht = &curtab->tp_vars.dv_hashtab;
3891 if (tdone < ht->ht_used)
3892 {
3893 if (tdone++ == 0)
3894 hi = ht->ht_array;
3895 else
3896 ++hi;
3897 while (HASHITEM_EMPTY(hi))
3898 ++hi;
3899 return cat_prefix_varname('t', hi->hi_key);
3900 }
3901#endif
3902
Bram Moolenaar33570922005-01-25 22:26:29 +00003903 /* v: variables */
3904 if (vidx < VV_LEN)
3905 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
3907 vim_free(varnamebuf);
3908 varnamebuf = NULL;
3909 varnamebuflen = 0;
3910 return NULL;
3911}
3912
3913#endif /* FEAT_CMDL_COMPL */
3914
3915/*
3916 * types for expressions.
3917 */
3918typedef enum
3919{
3920 TYPE_UNKNOWN = 0
3921 , TYPE_EQUAL /* == */
3922 , TYPE_NEQUAL /* != */
3923 , TYPE_GREATER /* > */
3924 , TYPE_GEQUAL /* >= */
3925 , TYPE_SMALLER /* < */
3926 , TYPE_SEQUAL /* <= */
3927 , TYPE_MATCH /* =~ */
3928 , TYPE_NOMATCH /* !~ */
3929} exptype_T;
3930
3931/*
3932 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003933 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3935 */
3936
3937/*
3938 * Handle zero level expression.
3939 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003940 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003941 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 * Return OK or FAIL.
3943 */
3944 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003945eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 char_u **nextcmd;
3949 int evaluate;
3950{
3951 int ret;
3952 char_u *p;
3953
3954 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003955 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 if (ret == FAIL || !ends_excmd(*p))
3957 {
3958 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003959 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 /*
3961 * Report the invalid expression unless the expression evaluation has
3962 * been cancelled due to an aborting error, an interrupt, or an
3963 * exception.
3964 */
3965 if (!aborting())
3966 EMSG2(_(e_invexpr2), arg);
3967 ret = FAIL;
3968 }
3969 if (nextcmd != NULL)
3970 *nextcmd = check_nextcmd(p);
3971
3972 return ret;
3973}
3974
3975/*
3976 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003977 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 *
3979 * "arg" must point to the first non-white of the expression.
3980 * "arg" is advanced to the next non-white after the recognized expression.
3981 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003982 * Note: "rettv.v_lock" is not set.
3983 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 * Return OK or FAIL.
3985 */
3986 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003987eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 int evaluate;
3991{
3992 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003993 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
3995 /*
3996 * Get the first variable.
3997 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003998 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 return FAIL;
4000
4001 if ((*arg)[0] == '?')
4002 {
4003 result = FALSE;
4004 if (evaluate)
4005 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004006 int error = FALSE;
4007
4008 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004010 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004011 if (error)
4012 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
4014
4015 /*
4016 * Get the second variable.
4017 */
4018 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 return FAIL;
4021
4022 /*
4023 * Check for the ":".
4024 */
4025 if ((*arg)[0] != ':')
4026 {
4027 EMSG(_("E109: Missing ':' after '?'"));
4028 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004029 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 return FAIL;
4031 }
4032
4033 /*
4034 * Get the third variable.
4035 */
4036 *arg = skipwhite(*arg + 1);
4037 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4038 {
4039 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004040 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 return FAIL;
4042 }
4043 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004044 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
4046
4047 return OK;
4048}
4049
4050/*
4051 * Handle first level expression:
4052 * expr2 || expr2 || expr2 logical OR
4053 *
4054 * "arg" must point to the first non-white of the expression.
4055 * "arg" is advanced to the next non-white after the recognized expression.
4056 *
4057 * Return OK or FAIL.
4058 */
4059 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004060eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 int evaluate;
4064{
Bram Moolenaar33570922005-01-25 22:26:29 +00004065 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 long result;
4067 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004068 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
4070 /*
4071 * Get the first variable.
4072 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004073 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 return FAIL;
4075
4076 /*
4077 * Repeat until there is no following "||".
4078 */
4079 first = TRUE;
4080 result = FALSE;
4081 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4082 {
4083 if (evaluate && first)
4084 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004085 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004087 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004088 if (error)
4089 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 first = FALSE;
4091 }
4092
4093 /*
4094 * Get the second variable.
4095 */
4096 *arg = skipwhite(*arg + 2);
4097 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4098 return FAIL;
4099
4100 /*
4101 * Compute the result.
4102 */
4103 if (evaluate && !result)
4104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004105 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004107 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004108 if (error)
4109 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111 if (evaluate)
4112 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 rettv->v_type = VAR_NUMBER;
4114 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
4116 }
4117
4118 return OK;
4119}
4120
4121/*
4122 * Handle second level expression:
4123 * expr3 && expr3 && expr3 logical AND
4124 *
4125 * "arg" must point to the first non-white of the expression.
4126 * "arg" is advanced to the next non-white after the recognized expression.
4127 *
4128 * Return OK or FAIL.
4129 */
4130 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004131eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 int evaluate;
4135{
Bram Moolenaar33570922005-01-25 22:26:29 +00004136 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 long result;
4138 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004139 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140
4141 /*
4142 * Get the first variable.
4143 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 return FAIL;
4146
4147 /*
4148 * Repeat until there is no following "&&".
4149 */
4150 first = TRUE;
4151 result = TRUE;
4152 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4153 {
4154 if (evaluate && first)
4155 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004156 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004158 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004159 if (error)
4160 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 first = FALSE;
4162 }
4163
4164 /*
4165 * Get the second variable.
4166 */
4167 *arg = skipwhite(*arg + 2);
4168 if (eval4(arg, &var2, evaluate && result) == FAIL)
4169 return FAIL;
4170
4171 /*
4172 * Compute the result.
4173 */
4174 if (evaluate && result)
4175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004176 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 if (error)
4180 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
4182 if (evaluate)
4183 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004184 rettv->v_type = VAR_NUMBER;
4185 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187 }
4188
4189 return OK;
4190}
4191
4192/*
4193 * Handle third level expression:
4194 * var1 == var2
4195 * var1 =~ var2
4196 * var1 != var2
4197 * var1 !~ var2
4198 * var1 > var2
4199 * var1 >= var2
4200 * var1 < var2
4201 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004202 * var1 is var2
4203 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 *
4205 * "arg" must point to the first non-white of the expression.
4206 * "arg" is advanced to the next non-white after the recognized expression.
4207 *
4208 * Return OK or FAIL.
4209 */
4210 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 int evaluate;
4215{
Bram Moolenaar33570922005-01-25 22:26:29 +00004216 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 char_u *p;
4218 int i;
4219 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004220 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 int len = 2;
4222 long n1, n2;
4223 char_u *s1, *s2;
4224 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4225 regmatch_T regmatch;
4226 int ic;
4227 char_u *save_cpo;
4228
4229 /*
4230 * Get the first variable.
4231 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 return FAIL;
4234
4235 p = *arg;
4236 switch (p[0])
4237 {
4238 case '=': if (p[1] == '=')
4239 type = TYPE_EQUAL;
4240 else if (p[1] == '~')
4241 type = TYPE_MATCH;
4242 break;
4243 case '!': if (p[1] == '=')
4244 type = TYPE_NEQUAL;
4245 else if (p[1] == '~')
4246 type = TYPE_NOMATCH;
4247 break;
4248 case '>': if (p[1] != '=')
4249 {
4250 type = TYPE_GREATER;
4251 len = 1;
4252 }
4253 else
4254 type = TYPE_GEQUAL;
4255 break;
4256 case '<': if (p[1] != '=')
4257 {
4258 type = TYPE_SMALLER;
4259 len = 1;
4260 }
4261 else
4262 type = TYPE_SEQUAL;
4263 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004264 case 'i': if (p[1] == 's')
4265 {
4266 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4267 len = 5;
4268 if (!vim_isIDc(p[len]))
4269 {
4270 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4271 type_is = TRUE;
4272 }
4273 }
4274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276
4277 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004278 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 */
4280 if (type != TYPE_UNKNOWN)
4281 {
4282 /* extra question mark appended: ignore case */
4283 if (p[len] == '?')
4284 {
4285 ic = TRUE;
4286 ++len;
4287 }
4288 /* extra '#' appended: match case */
4289 else if (p[len] == '#')
4290 {
4291 ic = FALSE;
4292 ++len;
4293 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004294 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 else
4296 ic = p_ic;
4297
4298 /*
4299 * Get the second variable.
4300 */
4301 *arg = skipwhite(p + len);
4302 if (eval5(arg, &var2, evaluate) == FAIL)
4303 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004304 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 return FAIL;
4306 }
4307
4308 if (evaluate)
4309 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004310 if (type_is && rettv->v_type != var2.v_type)
4311 {
4312 /* For "is" a different type always means FALSE, for "notis"
4313 * it means TRUE. */
4314 n1 = (type == TYPE_NEQUAL);
4315 }
4316 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4317 {
4318 if (type_is)
4319 {
4320 n1 = (rettv->v_type == var2.v_type
4321 && rettv->vval.v_list == var2.vval.v_list);
4322 if (type == TYPE_NEQUAL)
4323 n1 = !n1;
4324 }
4325 else if (rettv->v_type != var2.v_type
4326 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4327 {
4328 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004329 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004330 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004331 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004332 clear_tv(rettv);
4333 clear_tv(&var2);
4334 return FAIL;
4335 }
4336 else
4337 {
4338 /* Compare two Lists for being equal or unequal. */
4339 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4340 if (type == TYPE_NEQUAL)
4341 n1 = !n1;
4342 }
4343 }
4344
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004345 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4346 {
4347 if (type_is)
4348 {
4349 n1 = (rettv->v_type == var2.v_type
4350 && rettv->vval.v_dict == var2.vval.v_dict);
4351 if (type == TYPE_NEQUAL)
4352 n1 = !n1;
4353 }
4354 else if (rettv->v_type != var2.v_type
4355 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4356 {
4357 if (rettv->v_type != var2.v_type)
4358 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4359 else
4360 EMSG(_("E736: Invalid operation for Dictionary"));
4361 clear_tv(rettv);
4362 clear_tv(&var2);
4363 return FAIL;
4364 }
4365 else
4366 {
4367 /* Compare two Dictionaries for being equal or unequal. */
4368 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4369 if (type == TYPE_NEQUAL)
4370 n1 = !n1;
4371 }
4372 }
4373
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004374 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4375 {
4376 if (rettv->v_type != var2.v_type
4377 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4378 {
4379 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004380 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004381 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004382 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004383 clear_tv(rettv);
4384 clear_tv(&var2);
4385 return FAIL;
4386 }
4387 else
4388 {
4389 /* Compare two Funcrefs for being equal or unequal. */
4390 if (rettv->vval.v_string == NULL
4391 || var2.vval.v_string == NULL)
4392 n1 = FALSE;
4393 else
4394 n1 = STRCMP(rettv->vval.v_string,
4395 var2.vval.v_string) == 0;
4396 if (type == TYPE_NEQUAL)
4397 n1 = !n1;
4398 }
4399 }
4400
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004401#ifdef FEAT_FLOAT
4402 /*
4403 * If one of the two variables is a float, compare as a float.
4404 * When using "=~" or "!~", always compare as string.
4405 */
4406 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4407 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4408 {
4409 float_T f1, f2;
4410
4411 if (rettv->v_type == VAR_FLOAT)
4412 f1 = rettv->vval.v_float;
4413 else
4414 f1 = get_tv_number(rettv);
4415 if (var2.v_type == VAR_FLOAT)
4416 f2 = var2.vval.v_float;
4417 else
4418 f2 = get_tv_number(&var2);
4419 n1 = FALSE;
4420 switch (type)
4421 {
4422 case TYPE_EQUAL: n1 = (f1 == f2); break;
4423 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4424 case TYPE_GREATER: n1 = (f1 > f2); break;
4425 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4426 case TYPE_SMALLER: n1 = (f1 < f2); break;
4427 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4428 case TYPE_UNKNOWN:
4429 case TYPE_MATCH:
4430 case TYPE_NOMATCH: break; /* avoid gcc warning */
4431 }
4432 }
4433#endif
4434
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 /*
4436 * If one of the two variables is a number, compare as a number.
4437 * When using "=~" or "!~", always compare as string.
4438 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004439 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004442 n1 = get_tv_number(rettv);
4443 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 switch (type)
4445 {
4446 case TYPE_EQUAL: n1 = (n1 == n2); break;
4447 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4448 case TYPE_GREATER: n1 = (n1 > n2); break;
4449 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4450 case TYPE_SMALLER: n1 = (n1 < n2); break;
4451 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4452 case TYPE_UNKNOWN:
4453 case TYPE_MATCH:
4454 case TYPE_NOMATCH: break; /* avoid gcc warning */
4455 }
4456 }
4457 else
4458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004459 s1 = get_tv_string_buf(rettv, buf1);
4460 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4462 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4463 else
4464 i = 0;
4465 n1 = FALSE;
4466 switch (type)
4467 {
4468 case TYPE_EQUAL: n1 = (i == 0); break;
4469 case TYPE_NEQUAL: n1 = (i != 0); break;
4470 case TYPE_GREATER: n1 = (i > 0); break;
4471 case TYPE_GEQUAL: n1 = (i >= 0); break;
4472 case TYPE_SMALLER: n1 = (i < 0); break;
4473 case TYPE_SEQUAL: n1 = (i <= 0); break;
4474
4475 case TYPE_MATCH:
4476 case TYPE_NOMATCH:
4477 /* avoid 'l' flag in 'cpoptions' */
4478 save_cpo = p_cpo;
4479 p_cpo = (char_u *)"";
4480 regmatch.regprog = vim_regcomp(s2,
4481 RE_MAGIC + RE_STRING);
4482 regmatch.rm_ic = ic;
4483 if (regmatch.regprog != NULL)
4484 {
4485 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4486 vim_free(regmatch.regprog);
4487 if (type == TYPE_NOMATCH)
4488 n1 = !n1;
4489 }
4490 p_cpo = save_cpo;
4491 break;
4492
4493 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4494 }
4495 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004496 clear_tv(rettv);
4497 clear_tv(&var2);
4498 rettv->v_type = VAR_NUMBER;
4499 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501 }
4502
4503 return OK;
4504}
4505
4506/*
4507 * Handle fourth level expression:
4508 * + number addition
4509 * - number subtraction
4510 * . string concatenation
4511 *
4512 * "arg" must point to the first non-white of the expression.
4513 * "arg" is advanced to the next non-white after the recognized expression.
4514 *
4515 * Return OK or FAIL.
4516 */
4517 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004518eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 int evaluate;
4522{
Bram Moolenaar33570922005-01-25 22:26:29 +00004523 typval_T var2;
4524 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 int op;
4526 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004527#ifdef FEAT_FLOAT
4528 float_T f1 = 0, f2 = 0;
4529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 char_u *s1, *s2;
4531 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4532 char_u *p;
4533
4534 /*
4535 * Get the first variable.
4536 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004537 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 return FAIL;
4539
4540 /*
4541 * Repeat computing, until no '+', '-' or '.' is following.
4542 */
4543 for (;;)
4544 {
4545 op = **arg;
4546 if (op != '+' && op != '-' && op != '.')
4547 break;
4548
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004549 if ((op != '+' || rettv->v_type != VAR_LIST)
4550#ifdef FEAT_FLOAT
4551 && (op == '.' || rettv->v_type != VAR_FLOAT)
4552#endif
4553 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004554 {
4555 /* For "list + ...", an illegal use of the first operand as
4556 * a number cannot be determined before evaluating the 2nd
4557 * operand: if this is also a list, all is ok.
4558 * For "something . ...", "something - ..." or "non-list + ...",
4559 * we know that the first operand needs to be a string or number
4560 * without evaluating the 2nd operand. So check before to avoid
4561 * side effects after an error. */
4562 if (evaluate && get_tv_string_chk(rettv) == NULL)
4563 {
4564 clear_tv(rettv);
4565 return FAIL;
4566 }
4567 }
4568
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 /*
4570 * Get the second variable.
4571 */
4572 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004573 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004575 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 return FAIL;
4577 }
4578
4579 if (evaluate)
4580 {
4581 /*
4582 * Compute the result.
4583 */
4584 if (op == '.')
4585 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004586 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4587 s2 = get_tv_string_buf_chk(&var2, buf2);
4588 if (s2 == NULL) /* type error ? */
4589 {
4590 clear_tv(rettv);
4591 clear_tv(&var2);
4592 return FAIL;
4593 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004594 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004595 clear_tv(rettv);
4596 rettv->v_type = VAR_STRING;
4597 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004599 else if (op == '+' && rettv->v_type == VAR_LIST
4600 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004601 {
4602 /* concatenate Lists */
4603 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4604 &var3) == FAIL)
4605 {
4606 clear_tv(rettv);
4607 clear_tv(&var2);
4608 return FAIL;
4609 }
4610 clear_tv(rettv);
4611 *rettv = var3;
4612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 else
4614 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004615 int error = FALSE;
4616
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004617#ifdef FEAT_FLOAT
4618 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004619 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004620 f1 = rettv->vval.v_float;
4621 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004622 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004623 else
4624#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004625 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004626 n1 = get_tv_number_chk(rettv, &error);
4627 if (error)
4628 {
4629 /* This can only happen for "list + non-list". For
4630 * "non-list + ..." or "something - ...", we returned
4631 * before evaluating the 2nd operand. */
4632 clear_tv(rettv);
4633 return FAIL;
4634 }
4635#ifdef FEAT_FLOAT
4636 if (var2.v_type == VAR_FLOAT)
4637 f1 = n1;
4638#endif
4639 }
4640#ifdef FEAT_FLOAT
4641 if (var2.v_type == VAR_FLOAT)
4642 {
4643 f2 = var2.vval.v_float;
4644 n2 = 0;
4645 }
4646 else
4647#endif
4648 {
4649 n2 = get_tv_number_chk(&var2, &error);
4650 if (error)
4651 {
4652 clear_tv(rettv);
4653 clear_tv(&var2);
4654 return FAIL;
4655 }
4656#ifdef FEAT_FLOAT
4657 if (rettv->v_type == VAR_FLOAT)
4658 f2 = n2;
4659#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004660 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004661 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004662
4663#ifdef FEAT_FLOAT
4664 /* If there is a float on either side the result is a float. */
4665 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4666 {
4667 if (op == '+')
4668 f1 = f1 + f2;
4669 else
4670 f1 = f1 - f2;
4671 rettv->v_type = VAR_FLOAT;
4672 rettv->vval.v_float = f1;
4673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004675#endif
4676 {
4677 if (op == '+')
4678 n1 = n1 + n2;
4679 else
4680 n1 = n1 - n2;
4681 rettv->v_type = VAR_NUMBER;
4682 rettv->vval.v_number = n1;
4683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004685 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 }
4687 }
4688 return OK;
4689}
4690
4691/*
4692 * Handle fifth level expression:
4693 * * number multiplication
4694 * / number division
4695 * % number modulo
4696 *
4697 * "arg" must point to the first non-white of the expression.
4698 * "arg" is advanced to the next non-white after the recognized expression.
4699 *
4700 * Return OK or FAIL.
4701 */
4702 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004703eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004707 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708{
Bram Moolenaar33570922005-01-25 22:26:29 +00004709 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 int op;
4711 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712#ifdef FEAT_FLOAT
4713 int use_float = FALSE;
4714 float_T f1 = 0, f2;
4715#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004716 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717
4718 /*
4719 * Get the first variable.
4720 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004721 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 return FAIL;
4723
4724 /*
4725 * Repeat computing, until no '*', '/' or '%' is following.
4726 */
4727 for (;;)
4728 {
4729 op = **arg;
4730 if (op != '*' && op != '/' && op != '%')
4731 break;
4732
4733 if (evaluate)
4734 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004735#ifdef FEAT_FLOAT
4736 if (rettv->v_type == VAR_FLOAT)
4737 {
4738 f1 = rettv->vval.v_float;
4739 use_float = TRUE;
4740 n1 = 0;
4741 }
4742 else
4743#endif
4744 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004745 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004746 if (error)
4747 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 }
4749 else
4750 n1 = 0;
4751
4752 /*
4753 * Get the second variable.
4754 */
4755 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004756 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 return FAIL;
4758
4759 if (evaluate)
4760 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004761#ifdef FEAT_FLOAT
4762 if (var2.v_type == VAR_FLOAT)
4763 {
4764 if (!use_float)
4765 {
4766 f1 = n1;
4767 use_float = TRUE;
4768 }
4769 f2 = var2.vval.v_float;
4770 n2 = 0;
4771 }
4772 else
4773#endif
4774 {
4775 n2 = get_tv_number_chk(&var2, &error);
4776 clear_tv(&var2);
4777 if (error)
4778 return FAIL;
4779#ifdef FEAT_FLOAT
4780 if (use_float)
4781 f2 = n2;
4782#endif
4783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
4785 /*
4786 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004787 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789#ifdef FEAT_FLOAT
4790 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792 if (op == '*')
4793 f1 = f1 * f2;
4794 else if (op == '/')
4795 {
4796 /* We rely on the floating point library to handle divide
4797 * by zero to result in "inf" and not a crash. */
4798 f1 = f1 / f2;
4799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004801 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004802 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004803 return FAIL;
4804 }
4805 rettv->v_type = VAR_FLOAT;
4806 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 }
4808 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811 if (op == '*')
4812 n1 = n1 * n2;
4813 else if (op == '/')
4814 {
4815 if (n2 == 0) /* give an error message? */
4816 {
4817 if (n1 == 0)
4818 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4819 else if (n1 < 0)
4820 n1 = -0x7fffffffL;
4821 else
4822 n1 = 0x7fffffffL;
4823 }
4824 else
4825 n1 = n1 / n2;
4826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004828 {
4829 if (n2 == 0) /* give an error message? */
4830 n1 = 0;
4831 else
4832 n1 = n1 % n2;
4833 }
4834 rettv->v_type = VAR_NUMBER;
4835 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
4838 }
4839
4840 return OK;
4841}
4842
4843/*
4844 * Handle sixth level expression:
4845 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004846 * "string" string constant
4847 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 * &option-name option value
4849 * @r register contents
4850 * identifier variable value
4851 * function() function call
4852 * $VAR environment variable
4853 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004854 * [expr, expr] List
4855 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 *
4857 * Also handle:
4858 * ! in front logical NOT
4859 * - in front unary minus
4860 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004861 * trailing [] subscript in String or List
4862 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 *
4864 * "arg" must point to the first non-white of the expression.
4865 * "arg" is advanced to the next non-white after the recognized expression.
4866 *
4867 * Return OK or FAIL.
4868 */
4869 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004870eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004874 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 long n;
4877 int len;
4878 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 char_u *start_leader, *end_leader;
4880 int ret = OK;
4881 char_u *alias;
4882
4883 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004884 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004885 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888
4889 /*
4890 * Skip '!' and '-' characters. They are handled later.
4891 */
4892 start_leader = *arg;
4893 while (**arg == '!' || **arg == '-' || **arg == '+')
4894 *arg = skipwhite(*arg + 1);
4895 end_leader = *arg;
4896
4897 switch (**arg)
4898 {
4899 /*
4900 * Number constant.
4901 */
4902 case '0':
4903 case '1':
4904 case '2':
4905 case '3':
4906 case '4':
4907 case '5':
4908 case '6':
4909 case '7':
4910 case '8':
4911 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004912 {
4913#ifdef FEAT_FLOAT
4914 char_u *p = skipdigits(*arg + 1);
4915 int get_float = FALSE;
4916
4917 /* We accept a float when the format matches
4918 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004919 * strict to avoid backwards compatibility problems.
4920 * Don't look for a float after the "." operator, so that
4921 * ":let vers = 1.2.3" doesn't fail. */
4922 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004924 get_float = TRUE;
4925 p = skipdigits(p + 2);
4926 if (*p == 'e' || *p == 'E')
4927 {
4928 ++p;
4929 if (*p == '-' || *p == '+')
4930 ++p;
4931 if (!vim_isdigit(*p))
4932 get_float = FALSE;
4933 else
4934 p = skipdigits(p + 1);
4935 }
4936 if (ASCII_ISALPHA(*p) || *p == '.')
4937 get_float = FALSE;
4938 }
4939 if (get_float)
4940 {
4941 float_T f;
4942
4943 *arg += string2float(*arg, &f);
4944 if (evaluate)
4945 {
4946 rettv->v_type = VAR_FLOAT;
4947 rettv->vval.v_float = f;
4948 }
4949 }
4950 else
4951#endif
4952 {
4953 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4954 *arg += len;
4955 if (evaluate)
4956 {
4957 rettv->v_type = VAR_NUMBER;
4958 rettv->vval.v_number = n;
4959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 }
4961 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963
4964 /*
4965 * String constant: "string".
4966 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004967 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 break;
4969
4970 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004973 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004974 break;
4975
4976 /*
4977 * List: [expr, expr]
4978 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004979 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 break;
4981
4982 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 * Dictionary: {key: val, key: val}
4984 */
4985 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4986 break;
4987
4988 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004989 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004991 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 break;
4993
4994 /*
4995 * Environment variable: $VAR.
4996 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004997 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 break;
4999
5000 /*
5001 * Register contents: @r.
5002 */
5003 case '@': ++*arg;
5004 if (evaluate)
5005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005006 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005007 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
5009 if (**arg != NUL)
5010 ++*arg;
5011 break;
5012
5013 /*
5014 * nested expression: (expression).
5015 */
5016 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005017 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 if (**arg == ')')
5019 ++*arg;
5020 else if (ret == OK)
5021 {
5022 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005023 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 ret = FAIL;
5025 }
5026 break;
5027
Bram Moolenaar8c711452005-01-14 21:53:12 +00005028 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 break;
5030 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031
5032 if (ret == NOTDONE)
5033 {
5034 /*
5035 * Must be a variable or function name.
5036 * Can also be a curly-braces kind of name: {expr}.
5037 */
5038 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005039 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 if (alias != NULL)
5041 s = alias;
5042
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005043 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 ret = FAIL;
5045 else
5046 {
5047 if (**arg == '(') /* recursive! */
5048 {
5049 /* If "s" is the name of a variable of type VAR_FUNC
5050 * use its contents. */
5051 s = deref_func_name(s, &len);
5052
5053 /* Invoke the function. */
5054 ret = get_func_tv(s, len, rettv, arg,
5055 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005056 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 /* Stop the expression evaluation when immediately
5058 * aborting on error, or when an interrupt occurred or
5059 * an exception was thrown but not caught. */
5060 if (aborting())
5061 {
5062 if (ret == OK)
5063 clear_tv(rettv);
5064 ret = FAIL;
5065 }
5066 }
5067 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005068 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005069 else
5070 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 }
5072
5073 if (alias != NULL)
5074 vim_free(alias);
5075 }
5076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 *arg = skipwhite(*arg);
5078
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005079 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5080 * expr(expr). */
5081 if (ret == OK)
5082 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
5084 /*
5085 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5086 */
5087 if (ret == OK && evaluate && end_leader > start_leader)
5088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005089 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005090 int val = 0;
5091#ifdef FEAT_FLOAT
5092 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005093
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005094 if (rettv->v_type == VAR_FLOAT)
5095 f = rettv->vval.v_float;
5096 else
5097#endif
5098 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005099 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005101 clear_tv(rettv);
5102 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005104 else
5105 {
5106 while (end_leader > start_leader)
5107 {
5108 --end_leader;
5109 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005110 {
5111#ifdef FEAT_FLOAT
5112 if (rettv->v_type == VAR_FLOAT)
5113 f = !f;
5114 else
5115#endif
5116 val = !val;
5117 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005118 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005119 {
5120#ifdef FEAT_FLOAT
5121 if (rettv->v_type == VAR_FLOAT)
5122 f = -f;
5123 else
5124#endif
5125 val = -val;
5126 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005127 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005128#ifdef FEAT_FLOAT
5129 if (rettv->v_type == VAR_FLOAT)
5130 {
5131 clear_tv(rettv);
5132 rettv->vval.v_float = f;
5133 }
5134 else
5135#endif
5136 {
5137 clear_tv(rettv);
5138 rettv->v_type = VAR_NUMBER;
5139 rettv->vval.v_number = val;
5140 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 }
5143
5144 return ret;
5145}
5146
5147/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005148 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5149 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005150 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5151 */
5152 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005153eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005154 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005155 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005156 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005157 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158{
5159 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005160 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005161 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 long len = -1;
5163 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005166
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005167 if (rettv->v_type == VAR_FUNC
5168#ifdef FEAT_FLOAT
5169 || rettv->v_type == VAR_FLOAT
5170#endif
5171 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005172 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005173 if (verbose)
5174 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005175 return FAIL;
5176 }
5177
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 /*
5181 * dict.name
5182 */
5183 key = *arg + 1;
5184 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5185 ;
5186 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005189 }
5190 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005191 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 /*
5193 * something[idx]
5194 *
5195 * Get the (first) variable from inside the [].
5196 */
5197 *arg = skipwhite(*arg + 1);
5198 if (**arg == ':')
5199 empty1 = TRUE;
5200 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5201 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005202 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5203 {
5204 /* not a number or string */
5205 clear_tv(&var1);
5206 return FAIL;
5207 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208
5209 /*
5210 * Get the second variable from inside the [:].
5211 */
5212 if (**arg == ':')
5213 {
5214 range = TRUE;
5215 *arg = skipwhite(*arg + 1);
5216 if (**arg == ']')
5217 empty2 = TRUE;
5218 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005220 if (!empty1)
5221 clear_tv(&var1);
5222 return FAIL;
5223 }
5224 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5225 {
5226 /* not a number or string */
5227 if (!empty1)
5228 clear_tv(&var1);
5229 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 return FAIL;
5231 }
5232 }
5233
5234 /* Check for the ']'. */
5235 if (**arg != ']')
5236 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005237 if (verbose)
5238 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 clear_tv(&var1);
5240 if (range)
5241 clear_tv(&var2);
5242 return FAIL;
5243 }
5244 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005245 }
5246
5247 if (evaluate)
5248 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 n1 = 0;
5250 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005252 n1 = get_tv_number(&var1);
5253 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 }
5255 if (range)
5256 {
5257 if (empty2)
5258 n2 = -1;
5259 else
5260 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005261 n2 = get_tv_number(&var2);
5262 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 }
5264 }
5265
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005266 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267 {
5268 case VAR_NUMBER:
5269 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005270 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 len = (long)STRLEN(s);
5272 if (range)
5273 {
5274 /* The resulting variable is a substring. If the indexes
5275 * are out of range the result is empty. */
5276 if (n1 < 0)
5277 {
5278 n1 = len + n1;
5279 if (n1 < 0)
5280 n1 = 0;
5281 }
5282 if (n2 < 0)
5283 n2 = len + n2;
5284 else if (n2 >= len)
5285 n2 = len;
5286 if (n1 >= len || n2 < 0 || n1 > n2)
5287 s = NULL;
5288 else
5289 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5290 }
5291 else
5292 {
5293 /* The resulting variable is a string of a single
5294 * character. If the index is too big or negative the
5295 * result is empty. */
5296 if (n1 >= len || n1 < 0)
5297 s = NULL;
5298 else
5299 s = vim_strnsave(s + n1, 1);
5300 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005301 clear_tv(rettv);
5302 rettv->v_type = VAR_STRING;
5303 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 break;
5305
5306 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005307 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308 if (n1 < 0)
5309 n1 = len + n1;
5310 if (!empty1 && (n1 < 0 || n1 >= len))
5311 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005312 /* For a range we allow invalid values and return an empty
5313 * list. A list index out of range is an error. */
5314 if (!range)
5315 {
5316 if (verbose)
5317 EMSGN(_(e_listidx), n1);
5318 return FAIL;
5319 }
5320 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005321 }
5322 if (range)
5323 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005324 list_T *l;
5325 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005326
5327 if (n2 < 0)
5328 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005329 else if (n2 >= len)
5330 n2 = len - 1;
5331 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005332 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333 l = list_alloc();
5334 if (l == NULL)
5335 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005336 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337 n1 <= n2; ++n1)
5338 {
5339 if (list_append_tv(l, &item->li_tv) == FAIL)
5340 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005341 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 return FAIL;
5343 }
5344 item = item->li_next;
5345 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005346 clear_tv(rettv);
5347 rettv->v_type = VAR_LIST;
5348 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005349 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 }
5351 else
5352 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005353 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005354 clear_tv(rettv);
5355 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 }
5357 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005358
5359 case VAR_DICT:
5360 if (range)
5361 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005362 if (verbose)
5363 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005364 if (len == -1)
5365 clear_tv(&var1);
5366 return FAIL;
5367 }
5368 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005369 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005370
5371 if (len == -1)
5372 {
5373 key = get_tv_string(&var1);
5374 if (*key == NUL)
5375 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005376 if (verbose)
5377 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005378 clear_tv(&var1);
5379 return FAIL;
5380 }
5381 }
5382
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005383 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005384
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005385 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005386 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005387 if (len == -1)
5388 clear_tv(&var1);
5389 if (item == NULL)
5390 return FAIL;
5391
5392 copy_tv(&item->di_tv, &var1);
5393 clear_tv(rettv);
5394 *rettv = var1;
5395 }
5396 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 }
5398 }
5399
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 return OK;
5401}
5402
5403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 * Get an option value.
5405 * "arg" points to the '&' or '+' before the option name.
5406 * "arg" is advanced to character after the option name.
5407 * Return OK or FAIL.
5408 */
5409 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005410get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005412 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 int evaluate;
5414{
5415 char_u *option_end;
5416 long numval;
5417 char_u *stringval;
5418 int opt_type;
5419 int c;
5420 int working = (**arg == '+'); /* has("+option") */
5421 int ret = OK;
5422 int opt_flags;
5423
5424 /*
5425 * Isolate the option name and find its value.
5426 */
5427 option_end = find_option_end(arg, &opt_flags);
5428 if (option_end == NULL)
5429 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005430 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 EMSG2(_("E112: Option name missing: %s"), *arg);
5432 return FAIL;
5433 }
5434
5435 if (!evaluate)
5436 {
5437 *arg = option_end;
5438 return OK;
5439 }
5440
5441 c = *option_end;
5442 *option_end = NUL;
5443 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005444 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445
5446 if (opt_type == -3) /* invalid name */
5447 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005448 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 EMSG2(_("E113: Unknown option: %s"), *arg);
5450 ret = FAIL;
5451 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005452 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 {
5454 if (opt_type == -2) /* hidden string option */
5455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005456 rettv->v_type = VAR_STRING;
5457 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 }
5459 else if (opt_type == -1) /* hidden number option */
5460 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005461 rettv->v_type = VAR_NUMBER;
5462 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 }
5464 else if (opt_type == 1) /* number option */
5465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 rettv->v_type = VAR_NUMBER;
5467 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 }
5469 else /* string option */
5470 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005471 rettv->v_type = VAR_STRING;
5472 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 }
5474 }
5475 else if (working && (opt_type == -2 || opt_type == -1))
5476 ret = FAIL;
5477
5478 *option_end = c; /* put back for error messages */
5479 *arg = option_end;
5480
5481 return ret;
5482}
5483
5484/*
5485 * Allocate a variable for a string constant.
5486 * Return OK or FAIL.
5487 */
5488 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005489get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 int evaluate;
5493{
5494 char_u *p;
5495 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 int extra = 0;
5497
5498 /*
5499 * Find the end of the string, skipping backslashed characters.
5500 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005501 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
5503 if (*p == '\\' && p[1] != NUL)
5504 {
5505 ++p;
5506 /* A "\<x>" form occupies at least 4 characters, and produces up
5507 * to 6 characters: reserve space for 2 extra */
5508 if (*p == '<')
5509 extra += 2;
5510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 }
5512
5513 if (*p != '"')
5514 {
5515 EMSG2(_("E114: Missing quote: %s"), *arg);
5516 return FAIL;
5517 }
5518
5519 /* If only parsing, set *arg and return here */
5520 if (!evaluate)
5521 {
5522 *arg = p + 1;
5523 return OK;
5524 }
5525
5526 /*
5527 * Copy the string into allocated memory, handling backslashed
5528 * characters.
5529 */
5530 name = alloc((unsigned)(p - *arg + extra));
5531 if (name == NULL)
5532 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005533 rettv->v_type = VAR_STRING;
5534 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
Bram Moolenaar8c711452005-01-14 21:53:12 +00005536 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 {
5538 if (*p == '\\')
5539 {
5540 switch (*++p)
5541 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005542 case 'b': *name++ = BS; ++p; break;
5543 case 'e': *name++ = ESC; ++p; break;
5544 case 'f': *name++ = FF; ++p; break;
5545 case 'n': *name++ = NL; ++p; break;
5546 case 'r': *name++ = CAR; ++p; break;
5547 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548
5549 case 'X': /* hex: "\x1", "\x12" */
5550 case 'x':
5551 case 'u': /* Unicode: "\u0023" */
5552 case 'U':
5553 if (vim_isxdigit(p[1]))
5554 {
5555 int n, nr;
5556 int c = toupper(*p);
5557
5558 if (c == 'X')
5559 n = 2;
5560 else
5561 n = 4;
5562 nr = 0;
5563 while (--n >= 0 && vim_isxdigit(p[1]))
5564 {
5565 ++p;
5566 nr = (nr << 4) + hex2nr(*p);
5567 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005568 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569#ifdef FEAT_MBYTE
5570 /* For "\u" store the number according to
5571 * 'encoding'. */
5572 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 else
5575#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005576 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 break;
5579
5580 /* octal: "\1", "\12", "\123" */
5581 case '0':
5582 case '1':
5583 case '2':
5584 case '3':
5585 case '4':
5586 case '5':
5587 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005588 case '7': *name = *p++ - '0';
5589 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005591 *name = (*name << 3) + *p++ - '0';
5592 if (*p >= '0' && *p <= '7')
5593 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 break;
5597
5598 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 if (extra != 0)
5601 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005602 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 break;
5604 }
5605 /* FALLTHROUGH */
5606
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 break;
5609 }
5610 }
5611 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005612 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005615 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 *arg = p + 1;
5617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 return OK;
5619}
5620
5621/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005622 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 * Return OK or FAIL.
5624 */
5625 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005626get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 int evaluate;
5630{
5631 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005632 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005633 int reduce = 0;
5634
5635 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005637 */
5638 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5639 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005641 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005642 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005643 break;
5644 ++reduce;
5645 ++p;
5646 }
5647 }
5648
Bram Moolenaar8c711452005-01-14 21:53:12 +00005649 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005652 return FAIL;
5653 }
5654
Bram Moolenaar8c711452005-01-14 21:53:12 +00005655 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005656 if (!evaluate)
5657 {
5658 *arg = p + 1;
5659 return OK;
5660 }
5661
5662 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005663 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005664 */
5665 str = alloc((unsigned)((p - *arg) - reduce));
5666 if (str == NULL)
5667 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005668 rettv->v_type = VAR_STRING;
5669 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005670
Bram Moolenaar8c711452005-01-14 21:53:12 +00005671 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005676 break;
5677 ++p;
5678 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005682 *arg = p + 1;
5683
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005684 return OK;
5685}
5686
5687/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005688 * Allocate a variable for a List and fill it from "*arg".
5689 * Return OK or FAIL.
5690 */
5691 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005692get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005693 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005694 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005695 int evaluate;
5696{
Bram Moolenaar33570922005-01-25 22:26:29 +00005697 list_T *l = NULL;
5698 typval_T tv;
5699 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005700
5701 if (evaluate)
5702 {
5703 l = list_alloc();
5704 if (l == NULL)
5705 return FAIL;
5706 }
5707
5708 *arg = skipwhite(*arg + 1);
5709 while (**arg != ']' && **arg != NUL)
5710 {
5711 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5712 goto failret;
5713 if (evaluate)
5714 {
5715 item = listitem_alloc();
5716 if (item != NULL)
5717 {
5718 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005719 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005720 list_append(l, item);
5721 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005722 else
5723 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005724 }
5725
5726 if (**arg == ']')
5727 break;
5728 if (**arg != ',')
5729 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005731 goto failret;
5732 }
5733 *arg = skipwhite(*arg + 1);
5734 }
5735
5736 if (**arg != ']')
5737 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005739failret:
5740 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005741 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005742 return FAIL;
5743 }
5744
5745 *arg = skipwhite(*arg + 1);
5746 if (evaluate)
5747 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005748 rettv->v_type = VAR_LIST;
5749 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005750 ++l->lv_refcount;
5751 }
5752
5753 return OK;
5754}
5755
5756/*
5757 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005758 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005760 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761list_alloc()
5762{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005763 list_T *l;
5764
5765 l = (list_T *)alloc_clear(sizeof(list_T));
5766 if (l != NULL)
5767 {
5768 /* Prepend the list to the list of lists for garbage collection. */
5769 if (first_list != NULL)
5770 first_list->lv_used_prev = l;
5771 l->lv_used_prev = NULL;
5772 l->lv_used_next = first_list;
5773 first_list = l;
5774 }
5775 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005776}
5777
5778/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005779 * Allocate an empty list for a return value.
5780 * Returns OK or FAIL.
5781 */
5782 static int
5783rettv_list_alloc(rettv)
5784 typval_T *rettv;
5785{
5786 list_T *l = list_alloc();
5787
5788 if (l == NULL)
5789 return FAIL;
5790
5791 rettv->vval.v_list = l;
5792 rettv->v_type = VAR_LIST;
5793 ++l->lv_refcount;
5794 return OK;
5795}
5796
5797/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005798 * Unreference a list: decrement the reference count and free it when it
5799 * becomes zero.
5800 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005801 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005803 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005805 if (l != NULL && --l->lv_refcount <= 0)
5806 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807}
5808
5809/*
5810 * Free a list, including all items it points to.
5811 * Ignores the reference count.
5812 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005813 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005814list_free(l, recurse)
5815 list_T *l;
5816 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005817{
Bram Moolenaar33570922005-01-25 22:26:29 +00005818 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005819
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005820 /* Remove the list from the list of lists for garbage collection. */
5821 if (l->lv_used_prev == NULL)
5822 first_list = l->lv_used_next;
5823 else
5824 l->lv_used_prev->lv_used_next = l->lv_used_next;
5825 if (l->lv_used_next != NULL)
5826 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5827
Bram Moolenaard9fba312005-06-26 22:34:35 +00005828 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005829 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005830 /* Remove the item before deleting it. */
5831 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005832 if (recurse || (item->li_tv.v_type != VAR_LIST
5833 && item->li_tv.v_type != VAR_DICT))
5834 clear_tv(&item->li_tv);
5835 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836 }
5837 vim_free(l);
5838}
5839
5840/*
5841 * Allocate a list item.
5842 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005843 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005844listitem_alloc()
5845{
Bram Moolenaar33570922005-01-25 22:26:29 +00005846 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847}
5848
5849/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005850 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005851 */
5852 static void
5853listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005854 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005855{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005856 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857 vim_free(item);
5858}
5859
5860/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005861 * Remove a list item from a List and free it. Also clears the value.
5862 */
5863 static void
5864listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005865 list_T *l;
5866 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005867{
5868 list_remove(l, item, item);
5869 listitem_free(item);
5870}
5871
5872/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873 * Get the number of items in a list.
5874 */
5875 static long
5876list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005877 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879 if (l == NULL)
5880 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005881 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882}
5883
5884/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005885 * Return TRUE when two lists have exactly the same values.
5886 */
5887 static int
5888list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005889 list_T *l1;
5890 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005891 int ic; /* ignore case for strings */
5892{
Bram Moolenaar33570922005-01-25 22:26:29 +00005893 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005894
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005895 if (l1 == NULL || l2 == NULL)
5896 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005897 if (l1 == l2)
5898 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005899 if (list_len(l1) != list_len(l2))
5900 return FALSE;
5901
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005902 for (item1 = l1->lv_first, item2 = l2->lv_first;
5903 item1 != NULL && item2 != NULL;
5904 item1 = item1->li_next, item2 = item2->li_next)
5905 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5906 return FALSE;
5907 return item1 == NULL && item2 == NULL;
5908}
5909
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01005910#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_MZSCHEME) \
5911 || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005912/*
5913 * Return the dictitem that an entry in a hashtable points to.
5914 */
5915 dictitem_T *
5916dict_lookup(hi)
5917 hashitem_T *hi;
5918{
5919 return HI2DI(hi);
5920}
5921#endif
5922
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005923/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005924 * Return TRUE when two dictionaries have exactly the same key/values.
5925 */
5926 static int
5927dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005928 dict_T *d1;
5929 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005930 int ic; /* ignore case for strings */
5931{
Bram Moolenaar33570922005-01-25 22:26:29 +00005932 hashitem_T *hi;
5933 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005934 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005935
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005936 if (d1 == NULL || d2 == NULL)
5937 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005938 if (d1 == d2)
5939 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005940 if (dict_len(d1) != dict_len(d2))
5941 return FALSE;
5942
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005943 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005944 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005945 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005946 if (!HASHITEM_EMPTY(hi))
5947 {
5948 item2 = dict_find(d2, hi->hi_key, -1);
5949 if (item2 == NULL)
5950 return FALSE;
5951 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5952 return FALSE;
5953 --todo;
5954 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005955 }
5956 return TRUE;
5957}
5958
5959/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005960 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005961 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005962 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005963 */
5964 static int
5965tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005966 typval_T *tv1;
5967 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005968 int ic; /* ignore case */
5969{
5970 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005971 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005972 static int recursive = 0; /* cach recursive loops */
5973 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005974
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005975 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005976 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005977 /* Catch lists and dicts that have an endless loop by limiting
5978 * recursiveness to 1000. We guess they are equal then. */
5979 if (recursive >= 1000)
5980 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005981
5982 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005983 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005984 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005985 ++recursive;
5986 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5987 --recursive;
5988 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005989
5990 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005991 ++recursive;
5992 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5993 --recursive;
5994 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005995
5996 case VAR_FUNC:
5997 return (tv1->vval.v_string != NULL
5998 && tv2->vval.v_string != NULL
5999 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6000
6001 case VAR_NUMBER:
6002 return tv1->vval.v_number == tv2->vval.v_number;
6003
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006004#ifdef FEAT_FLOAT
6005 case VAR_FLOAT:
6006 return tv1->vval.v_float == tv2->vval.v_float;
6007#endif
6008
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006009 case VAR_STRING:
6010 s1 = get_tv_string_buf(tv1, buf1);
6011 s2 = get_tv_string_buf(tv2, buf2);
6012 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006013 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006014
6015 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006016 return TRUE;
6017}
6018
6019/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020 * Locate item with index "n" in list "l" and return it.
6021 * A negative index is counted from the end; -1 is the last item.
6022 * Returns NULL when "n" is out of range.
6023 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006024 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006026 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027 long n;
6028{
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 long idx;
6031
6032 if (l == NULL)
6033 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006034
6035 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006037 n = l->lv_len + n;
6038
6039 /* Check for index out of range. */
6040 if (n < 0 || n >= l->lv_len)
6041 return NULL;
6042
6043 /* When there is a cached index may start search from there. */
6044 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006046 if (n < l->lv_idx / 2)
6047 {
6048 /* closest to the start of the list */
6049 item = l->lv_first;
6050 idx = 0;
6051 }
6052 else if (n > (l->lv_idx + l->lv_len) / 2)
6053 {
6054 /* closest to the end of the list */
6055 item = l->lv_last;
6056 idx = l->lv_len - 1;
6057 }
6058 else
6059 {
6060 /* closest to the cached index */
6061 item = l->lv_idx_item;
6062 idx = l->lv_idx;
6063 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064 }
6065 else
6066 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006067 if (n < l->lv_len / 2)
6068 {
6069 /* closest to the start of the list */
6070 item = l->lv_first;
6071 idx = 0;
6072 }
6073 else
6074 {
6075 /* closest to the end of the list */
6076 item = l->lv_last;
6077 idx = l->lv_len - 1;
6078 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006080
6081 while (n > idx)
6082 {
6083 /* search forward */
6084 item = item->li_next;
6085 ++idx;
6086 }
6087 while (n < idx)
6088 {
6089 /* search backward */
6090 item = item->li_prev;
6091 --idx;
6092 }
6093
6094 /* cache the used index */
6095 l->lv_idx = idx;
6096 l->lv_idx_item = item;
6097
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098 return item;
6099}
6100
6101/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006102 * Get list item "l[idx]" as a number.
6103 */
6104 static long
6105list_find_nr(l, idx, errorp)
6106 list_T *l;
6107 long idx;
6108 int *errorp; /* set to TRUE when something wrong */
6109{
6110 listitem_T *li;
6111
6112 li = list_find(l, idx);
6113 if (li == NULL)
6114 {
6115 if (errorp != NULL)
6116 *errorp = TRUE;
6117 return -1L;
6118 }
6119 return get_tv_number_chk(&li->li_tv, errorp);
6120}
6121
6122/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006123 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6124 */
6125 char_u *
6126list_find_str(l, idx)
6127 list_T *l;
6128 long idx;
6129{
6130 listitem_T *li;
6131
6132 li = list_find(l, idx - 1);
6133 if (li == NULL)
6134 {
6135 EMSGN(_(e_listidx), idx);
6136 return NULL;
6137 }
6138 return get_tv_string(&li->li_tv);
6139}
6140
6141/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006142 * Locate "item" list "l" and return its index.
6143 * Returns -1 when "item" is not in the list.
6144 */
6145 static long
6146list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 list_T *l;
6148 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006149{
6150 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006151 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006152
6153 if (l == NULL)
6154 return -1;
6155 idx = 0;
6156 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6157 ++idx;
6158 if (li == NULL)
6159 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006160 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006161}
6162
6163/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164 * Append item "item" to the end of list "l".
6165 */
6166 static void
6167list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006168 list_T *l;
6169 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006170{
6171 if (l->lv_last == NULL)
6172 {
6173 /* empty list */
6174 l->lv_first = item;
6175 l->lv_last = item;
6176 item->li_prev = NULL;
6177 }
6178 else
6179 {
6180 l->lv_last->li_next = item;
6181 item->li_prev = l->lv_last;
6182 l->lv_last = item;
6183 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006184 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185 item->li_next = NULL;
6186}
6187
6188/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006189 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006190 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006192 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006194 list_T *l;
6195 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006196{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006197 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006198
Bram Moolenaar05159a02005-02-26 23:04:13 +00006199 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006201 copy_tv(tv, &li->li_tv);
6202 list_append(l, li);
6203 return OK;
6204}
6205
6206/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006207 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006208 * Return FAIL when out of memory.
6209 */
6210 int
6211list_append_dict(list, dict)
6212 list_T *list;
6213 dict_T *dict;
6214{
6215 listitem_T *li = listitem_alloc();
6216
6217 if (li == NULL)
6218 return FAIL;
6219 li->li_tv.v_type = VAR_DICT;
6220 li->li_tv.v_lock = 0;
6221 li->li_tv.vval.v_dict = dict;
6222 list_append(list, li);
6223 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224 return OK;
6225}
6226
6227/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006228 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006229 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006230 * Returns FAIL when out of memory.
6231 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006232 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006233list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006234 list_T *l;
6235 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006236 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006237{
6238 listitem_T *li = listitem_alloc();
6239
6240 if (li == NULL)
6241 return FAIL;
6242 list_append(l, li);
6243 li->li_tv.v_type = VAR_STRING;
6244 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006245 if (str == NULL)
6246 li->li_tv.vval.v_string = NULL;
6247 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006248 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006249 return FAIL;
6250 return OK;
6251}
6252
6253/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006254 * Append "n" to list "l".
6255 * Returns FAIL when out of memory.
6256 */
6257 static int
6258list_append_number(l, n)
6259 list_T *l;
6260 varnumber_T n;
6261{
6262 listitem_T *li;
6263
6264 li = listitem_alloc();
6265 if (li == NULL)
6266 return FAIL;
6267 li->li_tv.v_type = VAR_NUMBER;
6268 li->li_tv.v_lock = 0;
6269 li->li_tv.vval.v_number = n;
6270 list_append(l, li);
6271 return OK;
6272}
6273
6274/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006275 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006276 * If "item" is NULL append at the end.
6277 * Return FAIL when out of memory.
6278 */
6279 static int
6280list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006281 list_T *l;
6282 typval_T *tv;
6283 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006284{
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006286
6287 if (ni == NULL)
6288 return FAIL;
6289 copy_tv(tv, &ni->li_tv);
6290 if (item == NULL)
6291 /* Append new item at end of list. */
6292 list_append(l, ni);
6293 else
6294 {
6295 /* Insert new item before existing item. */
6296 ni->li_prev = item->li_prev;
6297 ni->li_next = item;
6298 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006299 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006300 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006301 ++l->lv_idx;
6302 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006303 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006304 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006305 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006306 l->lv_idx_item = NULL;
6307 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006308 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006309 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006310 }
6311 return OK;
6312}
6313
6314/*
6315 * Extend "l1" with "l2".
6316 * If "bef" is NULL append at the end, otherwise insert before this item.
6317 * Returns FAIL when out of memory.
6318 */
6319 static int
6320list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006321 list_T *l1;
6322 list_T *l2;
6323 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006324{
Bram Moolenaar33570922005-01-25 22:26:29 +00006325 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006326 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006327
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006328 /* We also quit the loop when we have inserted the original item count of
6329 * the list, avoid a hang when we extend a list with itself. */
6330 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006331 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6332 return FAIL;
6333 return OK;
6334}
6335
6336/*
6337 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6338 * Return FAIL when out of memory.
6339 */
6340 static int
6341list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 list_T *l1;
6343 list_T *l2;
6344 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006345{
Bram Moolenaar33570922005-01-25 22:26:29 +00006346 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006347
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006348 if (l1 == NULL || l2 == NULL)
6349 return FAIL;
6350
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006351 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006352 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006353 if (l == NULL)
6354 return FAIL;
6355 tv->v_type = VAR_LIST;
6356 tv->vval.v_list = l;
6357
6358 /* append all items from the second list */
6359 return list_extend(l, l2, NULL);
6360}
6361
6362/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006363 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006364 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006365 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006366 * Returns NULL when out of memory.
6367 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006368 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006369list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006370 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006371 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006372 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006373{
Bram Moolenaar33570922005-01-25 22:26:29 +00006374 list_T *copy;
6375 listitem_T *item;
6376 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377
6378 if (orig == NULL)
6379 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006380
6381 copy = list_alloc();
6382 if (copy != NULL)
6383 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006384 if (copyID != 0)
6385 {
6386 /* Do this before adding the items, because one of the items may
6387 * refer back to this list. */
6388 orig->lv_copyID = copyID;
6389 orig->lv_copylist = copy;
6390 }
6391 for (item = orig->lv_first; item != NULL && !got_int;
6392 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393 {
6394 ni = listitem_alloc();
6395 if (ni == NULL)
6396 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006397 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006398 {
6399 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6400 {
6401 vim_free(ni);
6402 break;
6403 }
6404 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006406 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006407 list_append(copy, ni);
6408 }
6409 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006410 if (item != NULL)
6411 {
6412 list_unref(copy);
6413 copy = NULL;
6414 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415 }
6416
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417 return copy;
6418}
6419
6420/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006421 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006422 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006425list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006426 list_T *l;
6427 listitem_T *item;
6428 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429{
Bram Moolenaar33570922005-01-25 22:26:29 +00006430 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432 /* notify watchers */
6433 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006434 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006435 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006436 list_fix_watch(l, ip);
6437 if (ip == item2)
6438 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006439 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006440
6441 if (item2->li_next == NULL)
6442 l->lv_last = item->li_prev;
6443 else
6444 item2->li_next->li_prev = item->li_prev;
6445 if (item->li_prev == NULL)
6446 l->lv_first = item2->li_next;
6447 else
6448 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006449 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006450}
6451
6452/*
6453 * Return an allocated string with the string representation of a list.
6454 * May return NULL.
6455 */
6456 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006457list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006459 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006460{
6461 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006462
6463 if (tv->vval.v_list == NULL)
6464 return NULL;
6465 ga_init2(&ga, (int)sizeof(char), 80);
6466 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006467 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006468 {
6469 vim_free(ga.ga_data);
6470 return NULL;
6471 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006472 ga_append(&ga, ']');
6473 ga_append(&ga, NUL);
6474 return (char_u *)ga.ga_data;
6475}
6476
6477/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006478 * Join list "l" into a string in "*gap", using separator "sep".
6479 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006480 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006481 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006482 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006483list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006484 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006485 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006486 char_u *sep;
6487 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006488 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006489{
6490 int first = TRUE;
6491 char_u *tofree;
6492 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006494 char_u *s;
6495
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006496 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006497 {
6498 if (first)
6499 first = FALSE;
6500 else
6501 ga_concat(gap, sep);
6502
6503 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006504 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006505 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006506 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006507 if (s != NULL)
6508 ga_concat(gap, s);
6509 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006510 if (s == NULL)
6511 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006512 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006513 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006514 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006515}
6516
6517/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006518 * Garbage collection for lists and dictionaries.
6519 *
6520 * We use reference counts to be able to free most items right away when they
6521 * are no longer used. But for composite items it's possible that it becomes
6522 * unused while the reference count is > 0: When there is a recursive
6523 * reference. Example:
6524 * :let l = [1, 2, 3]
6525 * :let d = {9: l}
6526 * :let l[1] = d
6527 *
6528 * Since this is quite unusual we handle this with garbage collection: every
6529 * once in a while find out which lists and dicts are not referenced from any
6530 * variable.
6531 *
6532 * Here is a good reference text about garbage collection (refers to Python
6533 * but it applies to all reference-counting mechanisms):
6534 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006535 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006536
6537/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006538 * Do garbage collection for lists and dicts.
6539 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006540 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006541 int
6542garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006543{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006544 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006545 buf_T *buf;
6546 win_T *wp;
6547 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006548 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006549 int did_free;
6550 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006551#ifdef FEAT_WINDOWS
6552 tabpage_T *tp;
6553#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006554
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006555 /* Only do this once. */
6556 want_garbage_collect = FALSE;
6557 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006558 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006559
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006560 /* We advance by two because we add one for items referenced through
6561 * previous_funccal. */
6562 current_copyID += COPYID_INC;
6563 copyID = current_copyID;
6564
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006565 /*
6566 * 1. Go through all accessible variables and mark all lists and dicts
6567 * with copyID.
6568 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006569
6570 /* Don't free variables in the previous_funccal list unless they are only
6571 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006572 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006573 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6574 {
6575 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6576 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6577 }
6578
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006579 /* script-local variables */
6580 for (i = 1; i <= ga_scripts.ga_len; ++i)
6581 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6582
6583 /* buffer-local variables */
6584 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6585 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6586
6587 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006588 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006589 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6590
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006591#ifdef FEAT_WINDOWS
6592 /* tabpage-local variables */
6593 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6594 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6595#endif
6596
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006597 /* global variables */
6598 set_ref_in_ht(&globvarht, copyID);
6599
6600 /* function-local variables */
6601 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6602 {
6603 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6604 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6605 }
6606
Bram Moolenaard812df62008-11-09 12:46:09 +00006607 /* v: vars */
6608 set_ref_in_ht(&vimvarht, copyID);
6609
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006610 /*
6611 * 2. Free lists and dictionaries that are not referenced.
6612 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006613 did_free = free_unref_items(copyID);
6614
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006615 /*
6616 * 3. Check if any funccal can be freed now.
6617 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006618 for (pfc = &previous_funccal; *pfc != NULL; )
6619 {
6620 if (can_free_funccal(*pfc, copyID))
6621 {
6622 fc = *pfc;
6623 *pfc = fc->caller;
6624 free_funccal(fc, TRUE);
6625 did_free = TRUE;
6626 did_free_funccal = TRUE;
6627 }
6628 else
6629 pfc = &(*pfc)->caller;
6630 }
6631 if (did_free_funccal)
6632 /* When a funccal was freed some more items might be garbage
6633 * collected, so run again. */
6634 (void)garbage_collect();
6635
6636 return did_free;
6637}
6638
6639/*
6640 * Free lists and dictionaries that are no longer referenced.
6641 */
6642 static int
6643free_unref_items(copyID)
6644 int copyID;
6645{
6646 dict_T *dd;
6647 list_T *ll;
6648 int did_free = FALSE;
6649
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006650 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006651 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006652 */
6653 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006654 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006655 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006656 /* Free the Dictionary and ordinary items it contains, but don't
6657 * recurse into Lists and Dictionaries, they will be in the list
6658 * of dicts or list of lists. */
6659 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006660 did_free = TRUE;
6661
6662 /* restart, next dict may also have been freed */
6663 dd = first_dict;
6664 }
6665 else
6666 dd = dd->dv_used_next;
6667
6668 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006669 * Go through the list of lists and free items without the copyID.
6670 * But don't free a list that has a watcher (used in a for loop), these
6671 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006672 */
6673 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006674 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6675 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006676 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006677 /* Free the List and ordinary items it contains, but don't recurse
6678 * into Lists and Dictionaries, they will be in the list of dicts
6679 * or list of lists. */
6680 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006681 did_free = TRUE;
6682
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006683 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006684 ll = first_list;
6685 }
6686 else
6687 ll = ll->lv_used_next;
6688
6689 return did_free;
6690}
6691
6692/*
6693 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6694 */
6695 static void
6696set_ref_in_ht(ht, copyID)
6697 hashtab_T *ht;
6698 int copyID;
6699{
6700 int todo;
6701 hashitem_T *hi;
6702
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006703 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006704 for (hi = ht->ht_array; todo > 0; ++hi)
6705 if (!HASHITEM_EMPTY(hi))
6706 {
6707 --todo;
6708 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6709 }
6710}
6711
6712/*
6713 * Mark all lists and dicts referenced through list "l" with "copyID".
6714 */
6715 static void
6716set_ref_in_list(l, copyID)
6717 list_T *l;
6718 int copyID;
6719{
6720 listitem_T *li;
6721
6722 for (li = l->lv_first; li != NULL; li = li->li_next)
6723 set_ref_in_item(&li->li_tv, copyID);
6724}
6725
6726/*
6727 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6728 */
6729 static void
6730set_ref_in_item(tv, copyID)
6731 typval_T *tv;
6732 int copyID;
6733{
6734 dict_T *dd;
6735 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006736
6737 switch (tv->v_type)
6738 {
6739 case VAR_DICT:
6740 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006741 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006742 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006743 /* Didn't see this dict yet. */
6744 dd->dv_copyID = copyID;
6745 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006746 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006747 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006748
6749 case VAR_LIST:
6750 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006751 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006752 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006753 /* Didn't see this list yet. */
6754 ll->lv_copyID = copyID;
6755 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006756 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006757 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006758 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006759 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006760}
6761
6762/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006763 * Allocate an empty header for a dictionary.
6764 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006765 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006766dict_alloc()
6767{
Bram Moolenaar33570922005-01-25 22:26:29 +00006768 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006769
Bram Moolenaar33570922005-01-25 22:26:29 +00006770 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006771 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006772 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006773 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006774 if (first_dict != NULL)
6775 first_dict->dv_used_prev = d;
6776 d->dv_used_next = first_dict;
6777 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006778 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006779
Bram Moolenaar33570922005-01-25 22:26:29 +00006780 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006781 d->dv_lock = 0;
6782 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006783 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006784 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006785 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006786}
6787
6788/*
6789 * Unreference a Dictionary: decrement the reference count and free it when it
6790 * becomes zero.
6791 */
6792 static void
6793dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006794 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006795{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006796 if (d != NULL && --d->dv_refcount <= 0)
6797 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006798}
6799
6800/*
6801 * Free a Dictionary, including all items it contains.
6802 * Ignores the reference count.
6803 */
6804 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006805dict_free(d, recurse)
6806 dict_T *d;
6807 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006808{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006809 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006810 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006811 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006812
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006813 /* Remove the dict from the list of dicts for garbage collection. */
6814 if (d->dv_used_prev == NULL)
6815 first_dict = d->dv_used_next;
6816 else
6817 d->dv_used_prev->dv_used_next = d->dv_used_next;
6818 if (d->dv_used_next != NULL)
6819 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6820
6821 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006822 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006823 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006824 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006825 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006826 if (!HASHITEM_EMPTY(hi))
6827 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006828 /* Remove the item before deleting it, just in case there is
6829 * something recursive causing trouble. */
6830 di = HI2DI(hi);
6831 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006832 if (recurse || (di->di_tv.v_type != VAR_LIST
6833 && di->di_tv.v_type != VAR_DICT))
6834 clear_tv(&di->di_tv);
6835 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006836 --todo;
6837 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006838 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006839 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006840 vim_free(d);
6841}
6842
6843/*
6844 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006845 * The "key" is copied to the new item.
6846 * Note that the value of the item "di_tv" still needs to be initialized!
6847 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006848 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006849 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006850dictitem_alloc(key)
6851 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006852{
Bram Moolenaar33570922005-01-25 22:26:29 +00006853 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006854
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006855 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006856 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006857 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006858 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006859 di->di_flags = 0;
6860 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006861 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006862}
6863
6864/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006865 * Make a copy of a Dictionary item.
6866 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006867 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006868dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006869 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006870{
Bram Moolenaar33570922005-01-25 22:26:29 +00006871 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006872
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006873 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6874 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006875 if (di != NULL)
6876 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006877 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006878 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006879 copy_tv(&org->di_tv, &di->di_tv);
6880 }
6881 return di;
6882}
6883
6884/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006885 * Remove item "item" from Dictionary "dict" and free it.
6886 */
6887 static void
6888dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006889 dict_T *dict;
6890 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006891{
Bram Moolenaar33570922005-01-25 22:26:29 +00006892 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006893
Bram Moolenaar33570922005-01-25 22:26:29 +00006894 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006895 if (HASHITEM_EMPTY(hi))
6896 EMSG2(_(e_intern2), "dictitem_remove()");
6897 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006898 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006899 dictitem_free(item);
6900}
6901
6902/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006903 * Free a dict item. Also clears the value.
6904 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006905 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006906dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006907 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006908{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006909 clear_tv(&item->di_tv);
6910 vim_free(item);
6911}
6912
6913/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006914 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6915 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006916 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006917 * Returns NULL when out of memory.
6918 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006919 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006920dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006921 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006922 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006923 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006924{
Bram Moolenaar33570922005-01-25 22:26:29 +00006925 dict_T *copy;
6926 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006927 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006928 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006929
6930 if (orig == NULL)
6931 return NULL;
6932
6933 copy = dict_alloc();
6934 if (copy != NULL)
6935 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006936 if (copyID != 0)
6937 {
6938 orig->dv_copyID = copyID;
6939 orig->dv_copydict = copy;
6940 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006941 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006942 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006943 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006944 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006945 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006946 --todo;
6947
6948 di = dictitem_alloc(hi->hi_key);
6949 if (di == NULL)
6950 break;
6951 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006952 {
6953 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6954 copyID) == FAIL)
6955 {
6956 vim_free(di);
6957 break;
6958 }
6959 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006960 else
6961 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6962 if (dict_add(copy, di) == FAIL)
6963 {
6964 dictitem_free(di);
6965 break;
6966 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006967 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006968 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006969
Bram Moolenaare9a41262005-01-15 22:18:47 +00006970 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006971 if (todo > 0)
6972 {
6973 dict_unref(copy);
6974 copy = NULL;
6975 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006976 }
6977
6978 return copy;
6979}
6980
6981/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006982 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006983 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006984 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006985 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006986dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006987 dict_T *d;
6988 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006989{
Bram Moolenaar33570922005-01-25 22:26:29 +00006990 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006991}
6992
Bram Moolenaar8c711452005-01-14 21:53:12 +00006993/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006994 * Add a number or string entry to dictionary "d".
6995 * When "str" is NULL use number "nr", otherwise use "str".
6996 * Returns FAIL when out of memory and when key already exists.
6997 */
6998 int
6999dict_add_nr_str(d, key, nr, str)
7000 dict_T *d;
7001 char *key;
7002 long nr;
7003 char_u *str;
7004{
7005 dictitem_T *item;
7006
7007 item = dictitem_alloc((char_u *)key);
7008 if (item == NULL)
7009 return FAIL;
7010 item->di_tv.v_lock = 0;
7011 if (str == NULL)
7012 {
7013 item->di_tv.v_type = VAR_NUMBER;
7014 item->di_tv.vval.v_number = nr;
7015 }
7016 else
7017 {
7018 item->di_tv.v_type = VAR_STRING;
7019 item->di_tv.vval.v_string = vim_strsave(str);
7020 }
7021 if (dict_add(d, item) == FAIL)
7022 {
7023 dictitem_free(item);
7024 return FAIL;
7025 }
7026 return OK;
7027}
7028
7029/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007030 * Get the number of items in a Dictionary.
7031 */
7032 static long
7033dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007034 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007035{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007036 if (d == NULL)
7037 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007038 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007039}
7040
7041/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007042 * Find item "key[len]" in Dictionary "d".
7043 * If "len" is negative use strlen(key).
7044 * Returns NULL when not found.
7045 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007046 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007047dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007048 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007049 char_u *key;
7050 int len;
7051{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007052#define AKEYLEN 200
7053 char_u buf[AKEYLEN];
7054 char_u *akey;
7055 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007056 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007057
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007058 if (len < 0)
7059 akey = key;
7060 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007061 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007062 tofree = akey = vim_strnsave(key, len);
7063 if (akey == NULL)
7064 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007065 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007066 else
7067 {
7068 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007069 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007070 akey = buf;
7071 }
7072
Bram Moolenaar33570922005-01-25 22:26:29 +00007073 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007074 vim_free(tofree);
7075 if (HASHITEM_EMPTY(hi))
7076 return NULL;
7077 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007078}
7079
7080/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007081 * Get a string item from a dictionary.
7082 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007083 * Returns NULL if the entry doesn't exist or out of memory.
7084 */
7085 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007086get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007087 dict_T *d;
7088 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007089 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007090{
7091 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007092 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007093
7094 di = dict_find(d, key, -1);
7095 if (di == NULL)
7096 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007097 s = get_tv_string(&di->di_tv);
7098 if (save && s != NULL)
7099 s = vim_strsave(s);
7100 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007101}
7102
7103/*
7104 * Get a number item from a dictionary.
7105 * Returns 0 if the entry doesn't exist or out of memory.
7106 */
7107 long
7108get_dict_number(d, key)
7109 dict_T *d;
7110 char_u *key;
7111{
7112 dictitem_T *di;
7113
7114 di = dict_find(d, key, -1);
7115 if (di == NULL)
7116 return 0;
7117 return get_tv_number(&di->di_tv);
7118}
7119
7120/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007121 * Return an allocated string with the string representation of a Dictionary.
7122 * May return NULL.
7123 */
7124 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007125dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007126 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007127 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007128{
7129 garray_T ga;
7130 int first = TRUE;
7131 char_u *tofree;
7132 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007134 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007135 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007136 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007137
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007138 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007139 return NULL;
7140 ga_init2(&ga, (int)sizeof(char), 80);
7141 ga_append(&ga, '{');
7142
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007143 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007144 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007145 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007146 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007147 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007148 --todo;
7149
7150 if (first)
7151 first = FALSE;
7152 else
7153 ga_concat(&ga, (char_u *)", ");
7154
7155 tofree = string_quote(hi->hi_key, FALSE);
7156 if (tofree != NULL)
7157 {
7158 ga_concat(&ga, tofree);
7159 vim_free(tofree);
7160 }
7161 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007162 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007163 if (s != NULL)
7164 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007165 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007166 if (s == NULL)
7167 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007168 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007169 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007170 if (todo > 0)
7171 {
7172 vim_free(ga.ga_data);
7173 return NULL;
7174 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175
7176 ga_append(&ga, '}');
7177 ga_append(&ga, NUL);
7178 return (char_u *)ga.ga_data;
7179}
7180
7181/*
7182 * Allocate a variable for a Dictionary and fill it from "*arg".
7183 * Return OK or FAIL. Returns NOTDONE for {expr}.
7184 */
7185 static int
7186get_dict_tv(arg, rettv, evaluate)
7187 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007189 int evaluate;
7190{
Bram Moolenaar33570922005-01-25 22:26:29 +00007191 dict_T *d = NULL;
7192 typval_T tvkey;
7193 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007194 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007195 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007196 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007197 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198
7199 /*
7200 * First check if it's not a curly-braces thing: {expr}.
7201 * Must do this without evaluating, otherwise a function may be called
7202 * twice. Unfortunately this means we need to call eval1() twice for the
7203 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007204 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007205 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007206 if (*start != '}')
7207 {
7208 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7209 return FAIL;
7210 if (*start == '}')
7211 return NOTDONE;
7212 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213
7214 if (evaluate)
7215 {
7216 d = dict_alloc();
7217 if (d == NULL)
7218 return FAIL;
7219 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007220 tvkey.v_type = VAR_UNKNOWN;
7221 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222
7223 *arg = skipwhite(*arg + 1);
7224 while (**arg != '}' && **arg != NUL)
7225 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007226 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227 goto failret;
7228 if (**arg != ':')
7229 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007230 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007231 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007232 goto failret;
7233 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007234 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007236 key = get_tv_string_buf_chk(&tvkey, buf);
7237 if (key == NULL || *key == NUL)
7238 {
7239 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7240 if (key != NULL)
7241 EMSG(_(e_emptykey));
7242 clear_tv(&tvkey);
7243 goto failret;
7244 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246
7247 *arg = skipwhite(*arg + 1);
7248 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7249 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007250 if (evaluate)
7251 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252 goto failret;
7253 }
7254 if (evaluate)
7255 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007256 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257 if (item != NULL)
7258 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007259 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007260 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261 clear_tv(&tv);
7262 goto failret;
7263 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007264 item = dictitem_alloc(key);
7265 clear_tv(&tvkey);
7266 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007269 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007270 if (dict_add(d, item) == FAIL)
7271 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272 }
7273 }
7274
7275 if (**arg == '}')
7276 break;
7277 if (**arg != ',')
7278 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007279 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280 goto failret;
7281 }
7282 *arg = skipwhite(*arg + 1);
7283 }
7284
7285 if (**arg != '}')
7286 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007287 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288failret:
7289 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007290 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291 return FAIL;
7292 }
7293
7294 *arg = skipwhite(*arg + 1);
7295 if (evaluate)
7296 {
7297 rettv->v_type = VAR_DICT;
7298 rettv->vval.v_dict = d;
7299 ++d->dv_refcount;
7300 }
7301
7302 return OK;
7303}
7304
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007306 * Return a string with the string representation of a variable.
7307 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007308 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007309 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007310 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007311 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007312 */
7313 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007314echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007316 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007317 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007318 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007319{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 static int recurse = 0;
7321 char_u *r = NULL;
7322
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007325 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326 *tofree = NULL;
7327 return NULL;
7328 }
7329 ++recurse;
7330
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007331 switch (tv->v_type)
7332 {
7333 case VAR_FUNC:
7334 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007335 r = tv->vval.v_string;
7336 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007337
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007338 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007339 if (tv->vval.v_list == NULL)
7340 {
7341 *tofree = NULL;
7342 r = NULL;
7343 }
7344 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7345 {
7346 *tofree = NULL;
7347 r = (char_u *)"[...]";
7348 }
7349 else
7350 {
7351 tv->vval.v_list->lv_copyID = copyID;
7352 *tofree = list2string(tv, copyID);
7353 r = *tofree;
7354 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007355 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007356
Bram Moolenaar8c711452005-01-14 21:53:12 +00007357 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007358 if (tv->vval.v_dict == NULL)
7359 {
7360 *tofree = NULL;
7361 r = NULL;
7362 }
7363 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7364 {
7365 *tofree = NULL;
7366 r = (char_u *)"{...}";
7367 }
7368 else
7369 {
7370 tv->vval.v_dict->dv_copyID = copyID;
7371 *tofree = dict2string(tv, copyID);
7372 r = *tofree;
7373 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007375
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007376 case VAR_STRING:
7377 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378 *tofree = NULL;
7379 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007380 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007381
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007382#ifdef FEAT_FLOAT
7383 case VAR_FLOAT:
7384 *tofree = NULL;
7385 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7386 r = numbuf;
7387 break;
7388#endif
7389
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007390 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007391 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007393 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394
7395 --recurse;
7396 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007397}
7398
7399/*
7400 * Return a string with the string representation of a variable.
7401 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7402 * "numbuf" is used for a number.
7403 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007404 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007405 */
7406 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007407tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007408 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007409 char_u **tofree;
7410 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007411 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007412{
7413 switch (tv->v_type)
7414 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007415 case VAR_FUNC:
7416 *tofree = string_quote(tv->vval.v_string, TRUE);
7417 return *tofree;
7418 case VAR_STRING:
7419 *tofree = string_quote(tv->vval.v_string, FALSE);
7420 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007421#ifdef FEAT_FLOAT
7422 case VAR_FLOAT:
7423 *tofree = NULL;
7424 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7425 return numbuf;
7426#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007427 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007428 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007429 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007430 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007431 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007432 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007433 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007434 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007435}
7436
7437/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007438 * Return string "str" in ' quotes, doubling ' characters.
7439 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007441 */
7442 static char_u *
7443string_quote(str, function)
7444 char_u *str;
7445 int function;
7446{
Bram Moolenaar33570922005-01-25 22:26:29 +00007447 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007448 char_u *p, *r, *s;
7449
Bram Moolenaar33570922005-01-25 22:26:29 +00007450 len = (function ? 13 : 3);
7451 if (str != NULL)
7452 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007453 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007454 for (p = str; *p != NUL; mb_ptr_adv(p))
7455 if (*p == '\'')
7456 ++len;
7457 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007458 s = r = alloc(len);
7459 if (r != NULL)
7460 {
7461 if (function)
7462 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007464 r += 10;
7465 }
7466 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007468 if (str != NULL)
7469 for (p = str; *p != NUL; )
7470 {
7471 if (*p == '\'')
7472 *r++ = '\'';
7473 MB_COPY_CHAR(p, r);
7474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007475 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007476 if (function)
7477 *r++ = ')';
7478 *r++ = NUL;
7479 }
7480 return s;
7481}
7482
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007483#ifdef FEAT_FLOAT
7484/*
7485 * Convert the string "text" to a floating point number.
7486 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7487 * this always uses a decimal point.
7488 * Returns the length of the text that was consumed.
7489 */
7490 static int
7491string2float(text, value)
7492 char_u *text;
7493 float_T *value; /* result stored here */
7494{
7495 char *s = (char *)text;
7496 float_T f;
7497
7498 f = strtod(s, &s);
7499 *value = f;
7500 return (int)((char_u *)s - text);
7501}
7502#endif
7503
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007504/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 * Get the value of an environment variable.
7506 * "arg" is pointing to the '$'. It is advanced to after the name.
7507 * If the environment variable was not set, silently assume it is empty.
7508 * Always return OK.
7509 */
7510 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007511get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 int evaluate;
7515{
7516 char_u *string = NULL;
7517 int len;
7518 int cc;
7519 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007520 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521
7522 ++*arg;
7523 name = *arg;
7524 len = get_env_len(arg);
7525 if (evaluate)
7526 {
7527 if (len != 0)
7528 {
7529 cc = name[len];
7530 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007531 /* first try vim_getenv(), fast for normal environment vars */
7532 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007534 {
7535 if (!mustfree)
7536 string = vim_strsave(string);
7537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 else
7539 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007540 if (mustfree)
7541 vim_free(string);
7542
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 /* next try expanding things like $VIM and ${HOME} */
7544 string = expand_env_save(name - 1);
7545 if (string != NULL && *string == '$')
7546 {
7547 vim_free(string);
7548 string = NULL;
7549 }
7550 }
7551 name[len] = cc;
7552 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007553 rettv->v_type = VAR_STRING;
7554 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 }
7556
7557 return OK;
7558}
7559
7560/*
7561 * Array with names and number of arguments of all internal functions
7562 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7563 */
7564static struct fst
7565{
7566 char *f_name; /* function name */
7567 char f_min_argc; /* minimal number of arguments */
7568 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007570 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571} functions[] =
7572{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007573#ifdef FEAT_FLOAT
7574 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007575 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007576#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007577 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 {"append", 2, 2, f_append},
7579 {"argc", 0, 0, f_argc},
7580 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007581 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007582#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007583 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007584 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007585 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007588 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 {"bufexists", 1, 1, f_bufexists},
7590 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7591 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7592 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7593 {"buflisted", 1, 1, f_buflisted},
7594 {"bufloaded", 1, 1, f_bufloaded},
7595 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007596 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 {"bufwinnr", 1, 1, f_bufwinnr},
7598 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007599 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007600 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007601#ifdef FEAT_FLOAT
7602 {"ceil", 1, 1, f_ceil},
7603#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007604 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 {"char2nr", 1, 1, f_char2nr},
7606 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007607 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007609#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007610 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007611 {"complete_add", 1, 1, f_complete_add},
7612 {"complete_check", 0, 0, f_complete_check},
7613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007615 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007616#ifdef FEAT_FLOAT
7617 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007618 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007619#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007620 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007622 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007623 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 {"delete", 1, 1, f_delete},
7625 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007626 {"diff_filler", 1, 1, f_diff_filler},
7627 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007628 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007630 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 {"eventhandler", 0, 0, f_eventhandler},
7632 {"executable", 1, 1, f_executable},
7633 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007634#ifdef FEAT_FLOAT
7635 {"exp", 1, 1, f_exp},
7636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007638 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007639 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7641 {"filereadable", 1, 1, f_filereadable},
7642 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007643 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007644 {"finddir", 1, 3, f_finddir},
7645 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007646#ifdef FEAT_FLOAT
7647 {"float2nr", 1, 1, f_float2nr},
7648 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007649 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007650#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007651 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {"fnamemodify", 2, 2, f_fnamemodify},
7653 {"foldclosed", 1, 1, f_foldclosed},
7654 {"foldclosedend", 1, 1, f_foldclosedend},
7655 {"foldlevel", 1, 1, f_foldlevel},
7656 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007657 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007659 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007660 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007661 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007662 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 {"getbufvar", 2, 2, f_getbufvar},
7664 {"getchar", 0, 1, f_getchar},
7665 {"getcharmod", 0, 0, f_getcharmod},
7666 {"getcmdline", 0, 0, f_getcmdline},
7667 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007668 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007670 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007671 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 {"getfsize", 1, 1, f_getfsize},
7673 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007674 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007675 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007676 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007677 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007678 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007679 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007680 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007681 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007683 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007684 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 {"getwinposx", 0, 0, f_getwinposx},
7686 {"getwinposy", 0, 0, f_getwinposy},
7687 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007688 {"glob", 1, 2, f_glob},
7689 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007691 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007692 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007693 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7695 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7696 {"histadd", 2, 2, f_histadd},
7697 {"histdel", 1, 2, f_histdel},
7698 {"histget", 1, 2, f_histget},
7699 {"histnr", 1, 1, f_histnr},
7700 {"hlID", 1, 1, f_hlID},
7701 {"hlexists", 1, 1, f_hlexists},
7702 {"hostname", 0, 0, f_hostname},
7703 {"iconv", 3, 3, f_iconv},
7704 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007705 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007706 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007708 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 {"inputrestore", 0, 0, f_inputrestore},
7710 {"inputsave", 0, 0, f_inputsave},
7711 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007712 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007714 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007716 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007719 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 {"libcall", 3, 3, f_libcall},
7721 {"libcallnr", 3, 3, f_libcallnr},
7722 {"line", 1, 1, f_line},
7723 {"line2byte", 1, 1, f_line2byte},
7724 {"lispindent", 1, 1, f_lispindent},
7725 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007726#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007727 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007728 {"log10", 1, 1, f_log10},
7729#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007730 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007731 {"maparg", 1, 3, f_maparg},
7732 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007733 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007734 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007735 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007736 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007737 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007738 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007739 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007740 {"max", 1, 1, f_max},
7741 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007742#ifdef vim_mkdir
7743 {"mkdir", 1, 3, f_mkdir},
7744#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007745 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007746#ifdef FEAT_MZSCHEME
7747 {"mzeval", 1, 1, f_mzeval},
7748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 {"nextnonblank", 1, 1, f_nextnonblank},
7750 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007751 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007752#ifdef FEAT_FLOAT
7753 {"pow", 2, 2, f_pow},
7754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007756 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007757 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007759 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007760 {"reltime", 0, 2, f_reltime},
7761 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {"remote_expr", 2, 3, f_remote_expr},
7763 {"remote_foreground", 1, 1, f_remote_foreground},
7764 {"remote_peek", 1, 2, f_remote_peek},
7765 {"remote_read", 1, 1, f_remote_read},
7766 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007767 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007769 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007771 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007772#ifdef FEAT_FLOAT
7773 {"round", 1, 1, f_round},
7774#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007775 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007776 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007777 {"searchpair", 3, 7, f_searchpair},
7778 {"searchpairpos", 3, 7, f_searchpairpos},
7779 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 {"server2client", 2, 2, f_server2client},
7781 {"serverlist", 0, 0, f_serverlist},
7782 {"setbufvar", 3, 3, f_setbufvar},
7783 {"setcmdpos", 1, 1, f_setcmdpos},
7784 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007785 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007786 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007787 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007788 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007790 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007791 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007793 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007795#ifdef FEAT_FLOAT
7796 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007797 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007798#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007799 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007800 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007801 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007802 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007803 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007804#ifdef FEAT_FLOAT
7805 {"sqrt", 1, 1, f_sqrt},
7806 {"str2float", 1, 1, f_str2float},
7807#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007808 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809#ifdef HAVE_STRFTIME
7810 {"strftime", 1, 2, f_strftime},
7811#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007812 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007813 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 {"strlen", 1, 1, f_strlen},
7815 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007816 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 {"strtrans", 1, 1, f_strtrans},
7818 {"submatch", 1, 1, f_submatch},
7819 {"substitute", 4, 4, f_substitute},
7820 {"synID", 3, 3, f_synID},
7821 {"synIDattr", 2, 3, f_synIDattr},
7822 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007823 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007824 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007825 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007826 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007827 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007828 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007829 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007830#ifdef FEAT_FLOAT
7831 {"tan", 1, 1, f_tan},
7832 {"tanh", 1, 1, f_tanh},
7833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007835 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 {"tolower", 1, 1, f_tolower},
7837 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007838 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007839#ifdef FEAT_FLOAT
7840 {"trunc", 1, 1, f_trunc},
7841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007843 {"undofile", 1, 1, f_undofile},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007844 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 {"virtcol", 1, 1, f_virtcol},
7846 {"visualmode", 0, 1, f_visualmode},
7847 {"winbufnr", 1, 1, f_winbufnr},
7848 {"wincol", 0, 0, f_wincol},
7849 {"winheight", 1, 1, f_winheight},
7850 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007851 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007853 {"winrestview", 1, 1, f_winrestview},
7854 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007856 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857};
7858
7859#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7860
7861/*
7862 * Function given to ExpandGeneric() to obtain the list of internal
7863 * or user defined function names.
7864 */
7865 char_u *
7866get_function_name(xp, idx)
7867 expand_T *xp;
7868 int idx;
7869{
7870 static int intidx = -1;
7871 char_u *name;
7872
7873 if (idx == 0)
7874 intidx = -1;
7875 if (intidx < 0)
7876 {
7877 name = get_user_func_name(xp, idx);
7878 if (name != NULL)
7879 return name;
7880 }
7881 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7882 {
7883 STRCPY(IObuff, functions[intidx].f_name);
7884 STRCAT(IObuff, "(");
7885 if (functions[intidx].f_max_argc == 0)
7886 STRCAT(IObuff, ")");
7887 return IObuff;
7888 }
7889
7890 return NULL;
7891}
7892
7893/*
7894 * Function given to ExpandGeneric() to obtain the list of internal or
7895 * user defined variable or function names.
7896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 char_u *
7898get_expr_name(xp, idx)
7899 expand_T *xp;
7900 int idx;
7901{
7902 static int intidx = -1;
7903 char_u *name;
7904
7905 if (idx == 0)
7906 intidx = -1;
7907 if (intidx < 0)
7908 {
7909 name = get_function_name(xp, idx);
7910 if (name != NULL)
7911 return name;
7912 }
7913 return get_user_var_name(xp, ++intidx);
7914}
7915
7916#endif /* FEAT_CMDL_COMPL */
7917
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007918#if defined(EBCDIC) || defined(PROTO)
7919/*
7920 * Compare struct fst by function name.
7921 */
7922 static int
7923compare_func_name(s1, s2)
7924 const void *s1;
7925 const void *s2;
7926{
7927 struct fst *p1 = (struct fst *)s1;
7928 struct fst *p2 = (struct fst *)s2;
7929
7930 return STRCMP(p1->f_name, p2->f_name);
7931}
7932
7933/*
7934 * Sort the function table by function name.
7935 * The sorting of the table above is ASCII dependant.
7936 * On machines using EBCDIC we have to sort it.
7937 */
7938 static void
7939sortFunctions()
7940{
7941 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7942
7943 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
7944}
7945#endif
7946
7947
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948/*
7949 * Find internal function in table above.
7950 * Return index, or -1 if not found
7951 */
7952 static int
7953find_internal_func(name)
7954 char_u *name; /* name of the function */
7955{
7956 int first = 0;
7957 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7958 int cmp;
7959 int x;
7960
7961 /*
7962 * Find the function name in the table. Binary search.
7963 */
7964 while (first <= last)
7965 {
7966 x = first + ((unsigned)(last - first) >> 1);
7967 cmp = STRCMP(name, functions[x].f_name);
7968 if (cmp < 0)
7969 last = x - 1;
7970 else if (cmp > 0)
7971 first = x + 1;
7972 else
7973 return x;
7974 }
7975 return -1;
7976}
7977
7978/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007979 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7980 * name it contains, otherwise return "name".
7981 */
7982 static char_u *
7983deref_func_name(name, lenp)
7984 char_u *name;
7985 int *lenp;
7986{
Bram Moolenaar33570922005-01-25 22:26:29 +00007987 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007988 int cc;
7989
7990 cc = name[*lenp];
7991 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007992 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007993 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007994 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007995 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007996 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007997 {
7998 *lenp = 0;
7999 return (char_u *)""; /* just in case */
8000 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008001 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008002 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008003 }
8004
8005 return name;
8006}
8007
8008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 * Allocate a variable for the result of a function.
8010 * Return OK or FAIL.
8011 */
8012 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008013get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8014 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 char_u *name; /* name of the function */
8016 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 char_u **arg; /* argument, pointing to the '(' */
8019 linenr_T firstline; /* first line of range */
8020 linenr_T lastline; /* last line of range */
8021 int *doesrange; /* return: function handled range */
8022 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008023 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024{
8025 char_u *argp;
8026 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008027 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 int argcount = 0; /* number of arguments found */
8029
8030 /*
8031 * Get the arguments.
8032 */
8033 argp = *arg;
8034 while (argcount < MAX_FUNC_ARGS)
8035 {
8036 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8037 if (*argp == ')' || *argp == ',' || *argp == NUL)
8038 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8040 {
8041 ret = FAIL;
8042 break;
8043 }
8044 ++argcount;
8045 if (*argp != ',')
8046 break;
8047 }
8048 if (*argp == ')')
8049 ++argp;
8050 else
8051 ret = FAIL;
8052
8053 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008054 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008055 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008057 {
8058 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008059 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008060 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008061 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063
8064 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008065 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066
8067 *arg = skipwhite(argp);
8068 return ret;
8069}
8070
8071
8072/*
8073 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008074 * Return OK when the function can't be called, FAIL otherwise.
8075 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 */
8077 static int
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008078call_func(func_name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008079 doesrange, evaluate, selfdict)
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008080 char_u *func_name; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008082 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008084 typval_T *argvars; /* vars for arguments, must have "argcount"
8085 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 linenr_T firstline; /* first line of range */
8087 linenr_T lastline; /* last line of range */
8088 int *doesrange; /* return: function handled range */
8089 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008090 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091{
8092 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093#define ERROR_UNKNOWN 0
8094#define ERROR_TOOMANY 1
8095#define ERROR_TOOFEW 2
8096#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008097#define ERROR_DICT 4
8098#define ERROR_NONE 5
8099#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 int error = ERROR_NONE;
8101 int i;
8102 int llen;
8103 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104#define FLEN_FIXED 40
8105 char_u fname_buf[FLEN_FIXED + 1];
8106 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008107 char_u *name;
8108
8109 /* Make a copy of the name, if it comes from a funcref variable it could
8110 * be changed or deleted in the called function. */
8111 name = vim_strnsave(func_name, len);
8112 if (name == NULL)
8113 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114
8115 /*
8116 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8117 * Change <SNR>123_name() to K_SNR 123_name().
8118 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 llen = eval_fname_script(name);
8121 if (llen > 0)
8122 {
8123 fname_buf[0] = K_SPECIAL;
8124 fname_buf[1] = KS_EXTRA;
8125 fname_buf[2] = (int)KE_SNR;
8126 i = 3;
8127 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8128 {
8129 if (current_SID <= 0)
8130 error = ERROR_SCRIPT;
8131 else
8132 {
8133 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8134 i = (int)STRLEN(fname_buf);
8135 }
8136 }
8137 if (i + STRLEN(name + llen) < FLEN_FIXED)
8138 {
8139 STRCPY(fname_buf + i, name + llen);
8140 fname = fname_buf;
8141 }
8142 else
8143 {
8144 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8145 if (fname == NULL)
8146 error = ERROR_OTHER;
8147 else
8148 {
8149 mch_memmove(fname, fname_buf, (size_t)i);
8150 STRCPY(fname + i, name + llen);
8151 }
8152 }
8153 }
8154 else
8155 fname = name;
8156
8157 *doesrange = FALSE;
8158
8159
8160 /* execute the function if no errors detected and executing */
8161 if (evaluate && error == ERROR_NONE)
8162 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008163 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8164 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 error = ERROR_UNKNOWN;
8166
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008167 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {
8169 /*
8170 * User defined function.
8171 */
8172 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008173
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008175 /* Trigger FuncUndefined event, may load the function. */
8176 if (fp == NULL
8177 && apply_autocmds(EVENT_FUNCUNDEFINED,
8178 fname, fname, TRUE, NULL)
8179 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008181 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 fp = find_func(fname);
8183 }
8184#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008185 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008186 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008187 {
8188 /* loaded a package, search for the function again */
8189 fp = find_func(fname);
8190 }
8191
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 if (fp != NULL)
8193 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008194 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008196 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008198 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008200 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008201 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 else
8203 {
8204 /*
8205 * Call the user function.
8206 * Save and restore search patterns, script variables and
8207 * redo buffer.
8208 */
8209 save_search_patterns();
8210 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008211 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008212 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008213 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008214 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8215 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8216 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008217 /* Function was unreferenced while being used, free it
8218 * now. */
8219 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 restoreRedobuff();
8221 restore_search_patterns();
8222 error = ERROR_NONE;
8223 }
8224 }
8225 }
8226 else
8227 {
8228 /*
8229 * Find the function name in the table, call its implementation.
8230 */
8231 i = find_internal_func(fname);
8232 if (i >= 0)
8233 {
8234 if (argcount < functions[i].f_min_argc)
8235 error = ERROR_TOOFEW;
8236 else if (argcount > functions[i].f_max_argc)
8237 error = ERROR_TOOMANY;
8238 else
8239 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008240 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008241 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 error = ERROR_NONE;
8243 }
8244 }
8245 }
8246 /*
8247 * The function call (or "FuncUndefined" autocommand sequence) might
8248 * have been aborted by an error, an interrupt, or an explicitly thrown
8249 * exception that has not been caught so far. This situation can be
8250 * tested for by calling aborting(). For an error in an internal
8251 * function or for the "E132" error in call_user_func(), however, the
8252 * throw point at which the "force_abort" flag (temporarily reset by
8253 * emsg()) is normally updated has not been reached yet. We need to
8254 * update that flag first to make aborting() reliable.
8255 */
8256 update_force_abort();
8257 }
8258 if (error == ERROR_NONE)
8259 ret = OK;
8260
8261 /*
8262 * Report an error unless the argument evaluation or function call has been
8263 * cancelled due to an aborting error, an interrupt, or an exception.
8264 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008265 if (!aborting())
8266 {
8267 switch (error)
8268 {
8269 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008270 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008271 break;
8272 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008273 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008274 break;
8275 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008276 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008277 name);
8278 break;
8279 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008280 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008281 name);
8282 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008283 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008284 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008285 name);
8286 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008287 }
8288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 if (fname != name && fname != fname_buf)
8291 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008292 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293
8294 return ret;
8295}
8296
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008297/*
8298 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008299 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008300 */
8301 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008302emsg_funcname(ermsg, name)
8303 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008304 char_u *name;
8305{
8306 char_u *p;
8307
8308 if (*name == K_SPECIAL)
8309 p = concat_str((char_u *)"<SNR>", name + 3);
8310 else
8311 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008312 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008313 if (p != name)
8314 vim_free(p);
8315}
8316
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008317/*
8318 * Return TRUE for a non-zero Number and a non-empty String.
8319 */
8320 static int
8321non_zero_arg(argvars)
8322 typval_T *argvars;
8323{
8324 return ((argvars[0].v_type == VAR_NUMBER
8325 && argvars[0].vval.v_number != 0)
8326 || (argvars[0].v_type == VAR_STRING
8327 && argvars[0].vval.v_string != NULL
8328 && *argvars[0].vval.v_string != NUL));
8329}
8330
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331/*********************************************
8332 * Implementation of the built-in functions
8333 */
8334
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008335#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008336static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8337
8338/*
8339 * Get the float value of "argvars[0]" into "f".
8340 * Returns FAIL when the argument is not a Number or Float.
8341 */
8342 static int
8343get_float_arg(argvars, f)
8344 typval_T *argvars;
8345 float_T *f;
8346{
8347 if (argvars[0].v_type == VAR_FLOAT)
8348 {
8349 *f = argvars[0].vval.v_float;
8350 return OK;
8351 }
8352 if (argvars[0].v_type == VAR_NUMBER)
8353 {
8354 *f = (float_T)argvars[0].vval.v_number;
8355 return OK;
8356 }
8357 EMSG(_("E808: Number or Float required"));
8358 return FAIL;
8359}
8360
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008361/*
8362 * "abs(expr)" function
8363 */
8364 static void
8365f_abs(argvars, rettv)
8366 typval_T *argvars;
8367 typval_T *rettv;
8368{
8369 if (argvars[0].v_type == VAR_FLOAT)
8370 {
8371 rettv->v_type = VAR_FLOAT;
8372 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8373 }
8374 else
8375 {
8376 varnumber_T n;
8377 int error = FALSE;
8378
8379 n = get_tv_number_chk(&argvars[0], &error);
8380 if (error)
8381 rettv->vval.v_number = -1;
8382 else if (n > 0)
8383 rettv->vval.v_number = n;
8384 else
8385 rettv->vval.v_number = -n;
8386 }
8387}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008388
8389/*
8390 * "acos()" function
8391 */
8392 static void
8393f_acos(argvars, rettv)
8394 typval_T *argvars;
8395 typval_T *rettv;
8396{
8397 float_T f;
8398
8399 rettv->v_type = VAR_FLOAT;
8400 if (get_float_arg(argvars, &f) == OK)
8401 rettv->vval.v_float = acos(f);
8402 else
8403 rettv->vval.v_float = 0.0;
8404}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008405#endif
8406
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008408 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 */
8410 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008411f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008412 typval_T *argvars;
8413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414{
Bram Moolenaar33570922005-01-25 22:26:29 +00008415 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008417 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008418 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008420 if ((l = argvars[0].vval.v_list) != NULL
8421 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8422 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008423 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008424 }
8425 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008426 EMSG(_(e_listreq));
8427}
8428
8429/*
8430 * "append(lnum, string/list)" function
8431 */
8432 static void
8433f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008434 typval_T *argvars;
8435 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008436{
8437 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008438 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008439 list_T *l = NULL;
8440 listitem_T *li = NULL;
8441 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008442 long added = 0;
8443
Bram Moolenaar0d660222005-01-07 21:51:51 +00008444 lnum = get_tv_lnum(argvars);
8445 if (lnum >= 0
8446 && lnum <= curbuf->b_ml.ml_line_count
8447 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008448 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008449 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008450 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008451 l = argvars[1].vval.v_list;
8452 if (l == NULL)
8453 return;
8454 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008455 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008456 for (;;)
8457 {
8458 if (l == NULL)
8459 tv = &argvars[1]; /* append a string */
8460 else if (li == NULL)
8461 break; /* end of list */
8462 else
8463 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008464 line = get_tv_string_chk(tv);
8465 if (line == NULL) /* type error */
8466 {
8467 rettv->vval.v_number = 1; /* Failed */
8468 break;
8469 }
8470 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008471 ++added;
8472 if (l == NULL)
8473 break;
8474 li = li->li_next;
8475 }
8476
8477 appended_lines_mark(lnum, added);
8478 if (curwin->w_cursor.lnum > lnum)
8479 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008481 else
8482 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483}
8484
8485/*
8486 * "argc()" function
8487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008489f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008490 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008493 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494}
8495
8496/*
8497 * "argidx()" function
8498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008500f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008501 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008504 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505}
8506
8507/*
8508 * "argv(nr)" function
8509 */
8510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008511f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008512 typval_T *argvars;
8513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514{
8515 int idx;
8516
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008517 if (argvars[0].v_type != VAR_UNKNOWN)
8518 {
8519 idx = get_tv_number_chk(&argvars[0], NULL);
8520 if (idx >= 0 && idx < ARGCOUNT)
8521 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8522 else
8523 rettv->vval.v_string = NULL;
8524 rettv->v_type = VAR_STRING;
8525 }
8526 else if (rettv_list_alloc(rettv) == OK)
8527 for (idx = 0; idx < ARGCOUNT; ++idx)
8528 list_append_string(rettv->vval.v_list,
8529 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530}
8531
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008532#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008533/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008534 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008535 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008536 static void
8537f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008538 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008539 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008540{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008541 float_T f;
8542
8543 rettv->v_type = VAR_FLOAT;
8544 if (get_float_arg(argvars, &f) == OK)
8545 rettv->vval.v_float = asin(f);
8546 else
8547 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008548}
8549
8550/*
8551 * "atan()" function
8552 */
8553 static void
8554f_atan(argvars, rettv)
8555 typval_T *argvars;
8556 typval_T *rettv;
8557{
8558 float_T f;
8559
8560 rettv->v_type = VAR_FLOAT;
8561 if (get_float_arg(argvars, &f) == OK)
8562 rettv->vval.v_float = atan(f);
8563 else
8564 rettv->vval.v_float = 0.0;
8565}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008566
8567/*
8568 * "atan2()" function
8569 */
8570 static void
8571f_atan2(argvars, rettv)
8572 typval_T *argvars;
8573 typval_T *rettv;
8574{
8575 float_T fx, fy;
8576
8577 rettv->v_type = VAR_FLOAT;
8578 if (get_float_arg(argvars, &fx) == OK
8579 && get_float_arg(&argvars[1], &fy) == OK)
8580 rettv->vval.v_float = atan2(fx, fy);
8581 else
8582 rettv->vval.v_float = 0.0;
8583}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008584#endif
8585
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586/*
8587 * "browse(save, title, initdir, default)" function
8588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008591 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593{
8594#ifdef FEAT_BROWSE
8595 int save;
8596 char_u *title;
8597 char_u *initdir;
8598 char_u *defname;
8599 char_u buf[NUMBUFLEN];
8600 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008601 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008603 save = get_tv_number_chk(&argvars[0], &error);
8604 title = get_tv_string_chk(&argvars[1]);
8605 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8606 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008608 if (error || title == NULL || initdir == NULL || defname == NULL)
8609 rettv->vval.v_string = NULL;
8610 else
8611 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008612 do_browse(save ? BROWSE_SAVE : 0,
8613 title, defname, NULL, initdir, NULL, curbuf);
8614#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008615 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008616#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008617 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008618}
8619
8620/*
8621 * "browsedir(title, initdir)" function
8622 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008624f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008625 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008626 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008627{
8628#ifdef FEAT_BROWSE
8629 char_u *title;
8630 char_u *initdir;
8631 char_u buf[NUMBUFLEN];
8632
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008633 title = get_tv_string_chk(&argvars[0]);
8634 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008636 if (title == NULL || initdir == NULL)
8637 rettv->vval.v_string = NULL;
8638 else
8639 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008640 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008642 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008644 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645}
8646
Bram Moolenaar33570922005-01-25 22:26:29 +00008647static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008648
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649/*
8650 * Find a buffer by number or exact name.
8651 */
8652 static buf_T *
8653find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008654 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655{
8656 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008658 if (avar->v_type == VAR_NUMBER)
8659 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008660 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008662 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008663 if (buf == NULL)
8664 {
8665 /* No full path name match, try a match with a URL or a "nofile"
8666 * buffer, these don't use the full path. */
8667 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8668 if (buf->b_fname != NULL
8669 && (path_with_url(buf->b_fname)
8670#ifdef FEAT_QUICKFIX
8671 || bt_nofile(buf)
8672#endif
8673 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008674 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008675 break;
8676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 }
8678 return buf;
8679}
8680
8681/*
8682 * "bufexists(expr)" function
8683 */
8684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008685f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008686 typval_T *argvars;
8687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008689 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690}
8691
8692/*
8693 * "buflisted(expr)" function
8694 */
8695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008696f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008697 typval_T *argvars;
8698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699{
8700 buf_T *buf;
8701
8702 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008703 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704}
8705
8706/*
8707 * "bufloaded(expr)" function
8708 */
8709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008710f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008711 typval_T *argvars;
8712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713{
8714 buf_T *buf;
8715
8716 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008717 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718}
8719
Bram Moolenaar33570922005-01-25 22:26:29 +00008720static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008721
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722/*
8723 * Get buffer by number or pattern.
8724 */
8725 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008726get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008727 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008729 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 int save_magic;
8731 char_u *save_cpo;
8732 buf_T *buf;
8733
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008734 if (tv->v_type == VAR_NUMBER)
8735 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008736 if (tv->v_type != VAR_STRING)
8737 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 if (name == NULL || *name == NUL)
8739 return curbuf;
8740 if (name[0] == '$' && name[1] == NUL)
8741 return lastbuf;
8742
8743 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8744 save_magic = p_magic;
8745 p_magic = TRUE;
8746 save_cpo = p_cpo;
8747 p_cpo = (char_u *)"";
8748
8749 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8750 TRUE, FALSE));
8751
8752 p_magic = save_magic;
8753 p_cpo = save_cpo;
8754
8755 /* If not found, try expanding the name, like done for bufexists(). */
8756 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008757 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758
8759 return buf;
8760}
8761
8762/*
8763 * "bufname(expr)" function
8764 */
8765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008766f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008767 typval_T *argvars;
8768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769{
8770 buf_T *buf;
8771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008772 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008774 buf = get_buf_tv(&argvars[0]);
8775 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008779 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 --emsg_off;
8781}
8782
8783/*
8784 * "bufnr(expr)" function
8785 */
8786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008787f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008788 typval_T *argvars;
8789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790{
8791 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008792 int error = FALSE;
8793 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008795 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008797 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008798 --emsg_off;
8799
8800 /* If the buffer isn't found and the second argument is not zero create a
8801 * new buffer. */
8802 if (buf == NULL
8803 && argvars[1].v_type != VAR_UNKNOWN
8804 && get_tv_number_chk(&argvars[1], &error) != 0
8805 && !error
8806 && (name = get_tv_string_chk(&argvars[0])) != NULL
8807 && !error)
8808 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8809
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008811 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008813 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814}
8815
8816/*
8817 * "bufwinnr(nr)" function
8818 */
8819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008820f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008821 typval_T *argvars;
8822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823{
8824#ifdef FEAT_WINDOWS
8825 win_T *wp;
8826 int winnr = 0;
8827#endif
8828 buf_T *buf;
8829
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008830 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008832 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833#ifdef FEAT_WINDOWS
8834 for (wp = firstwin; wp; wp = wp->w_next)
8835 {
8836 ++winnr;
8837 if (wp->w_buffer == buf)
8838 break;
8839 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008840 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008842 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843#endif
8844 --emsg_off;
8845}
8846
8847/*
8848 * "byte2line(byte)" function
8849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008851f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008852 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854{
8855#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857#else
8858 long boff = 0;
8859
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008860 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008864 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 (linenr_T)0, &boff);
8866#endif
8867}
8868
8869/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008870 * "byteidx()" function
8871 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008873f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008874 typval_T *argvars;
8875 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008876{
8877#ifdef FEAT_MBYTE
8878 char_u *t;
8879#endif
8880 char_u *str;
8881 long idx;
8882
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008883 str = get_tv_string_chk(&argvars[0]);
8884 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008885 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008886 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008887 return;
8888
8889#ifdef FEAT_MBYTE
8890 t = str;
8891 for ( ; idx > 0; idx--)
8892 {
8893 if (*t == NUL) /* EOL reached */
8894 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008895 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008896 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008897 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008898#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008899 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008900 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008901#endif
8902}
8903
8904/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008905 * "call(func, arglist)" function
8906 */
8907 static void
8908f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008909 typval_T *argvars;
8910 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008911{
8912 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008913 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008914 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008915 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008916 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008917 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008918
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008919 if (argvars[1].v_type != VAR_LIST)
8920 {
8921 EMSG(_(e_listreq));
8922 return;
8923 }
8924 if (argvars[1].vval.v_list == NULL)
8925 return;
8926
8927 if (argvars[0].v_type == VAR_FUNC)
8928 func = argvars[0].vval.v_string;
8929 else
8930 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008931 if (*func == NUL)
8932 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008933
Bram Moolenaare9a41262005-01-15 22:18:47 +00008934 if (argvars[2].v_type != VAR_UNKNOWN)
8935 {
8936 if (argvars[2].v_type != VAR_DICT)
8937 {
8938 EMSG(_(e_dictreq));
8939 return;
8940 }
8941 selfdict = argvars[2].vval.v_dict;
8942 }
8943
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008944 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8945 item = item->li_next)
8946 {
8947 if (argc == MAX_FUNC_ARGS)
8948 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008949 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008950 break;
8951 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008952 /* Make a copy of each argument. This is needed to be able to set
8953 * v_lock to VAR_FIXED in the copy without changing the original list.
8954 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008955 copy_tv(&item->li_tv, &argv[argc++]);
8956 }
8957
8958 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008959 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008960 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8961 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008962
8963 /* Free the arguments. */
8964 while (argc > 0)
8965 clear_tv(&argv[--argc]);
8966}
8967
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008968#ifdef FEAT_FLOAT
8969/*
8970 * "ceil({float})" function
8971 */
8972 static void
8973f_ceil(argvars, rettv)
8974 typval_T *argvars;
8975 typval_T *rettv;
8976{
8977 float_T f;
8978
8979 rettv->v_type = VAR_FLOAT;
8980 if (get_float_arg(argvars, &f) == OK)
8981 rettv->vval.v_float = ceil(f);
8982 else
8983 rettv->vval.v_float = 0.0;
8984}
8985#endif
8986
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008987/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008988 * "changenr()" function
8989 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008990 static void
8991f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008992 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008993 typval_T *rettv;
8994{
8995 rettv->vval.v_number = curbuf->b_u_seq_cur;
8996}
8997
8998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 * "char2nr(string)" function
9000 */
9001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009003 typval_T *argvars;
9004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005{
9006#ifdef FEAT_MBYTE
9007 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009008 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 else
9010#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009011 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012}
9013
9014/*
9015 * "cindent(lnum)" function
9016 */
9017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 typval_T *argvars;
9020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021{
9022#ifdef FEAT_CINDENT
9023 pos_T pos;
9024 linenr_T lnum;
9025
9026 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9029 {
9030 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009031 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 curwin->w_cursor = pos;
9033 }
9034 else
9035#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009036 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037}
9038
9039/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009040 * "clearmatches()" function
9041 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009042 static void
9043f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009044 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009045 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009046{
9047#ifdef FEAT_SEARCH_EXTRA
9048 clear_matches(curwin);
9049#endif
9050}
9051
9052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 * "col(string)" function
9054 */
9055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009056f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009057 typval_T *argvars;
9058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059{
9060 colnr_T col = 0;
9061 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009062 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009064 fp = var2fpos(&argvars[0], FALSE, &fnum);
9065 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 {
9067 if (fp->col == MAXCOL)
9068 {
9069 /* '> can be MAXCOL, get the length of the line then */
9070 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009071 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 else
9073 col = MAXCOL;
9074 }
9075 else
9076 {
9077 col = fp->col + 1;
9078#ifdef FEAT_VIRTUALEDIT
9079 /* col(".") when the cursor is on the NUL at the end of the line
9080 * because of "coladd" can be seen as an extra column. */
9081 if (virtual_active() && fp == &curwin->w_cursor)
9082 {
9083 char_u *p = ml_get_cursor();
9084
9085 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9086 curwin->w_virtcol - curwin->w_cursor.coladd))
9087 {
9088# ifdef FEAT_MBYTE
9089 int l;
9090
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009091 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 col += l;
9093# else
9094 if (*p != NUL && p[1] == NUL)
9095 ++col;
9096# endif
9097 }
9098 }
9099#endif
9100 }
9101 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009102 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103}
9104
Bram Moolenaar572cb562005-08-05 21:35:02 +00009105#if defined(FEAT_INS_EXPAND)
9106/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009107 * "complete()" function
9108 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009109 static void
9110f_complete(argvars, rettv)
9111 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009112 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009113{
9114 int startcol;
9115
9116 if ((State & INSERT) == 0)
9117 {
9118 EMSG(_("E785: complete() can only be used in Insert mode"));
9119 return;
9120 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009121
9122 /* Check for undo allowed here, because if something was already inserted
9123 * the line was already saved for undo and this check isn't done. */
9124 if (!undo_allowed())
9125 return;
9126
Bram Moolenaarade00832006-03-10 21:46:58 +00009127 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9128 {
9129 EMSG(_(e_invarg));
9130 return;
9131 }
9132
9133 startcol = get_tv_number_chk(&argvars[0], NULL);
9134 if (startcol <= 0)
9135 return;
9136
9137 set_completion(startcol - 1, argvars[1].vval.v_list);
9138}
9139
9140/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009141 * "complete_add()" function
9142 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009143 static void
9144f_complete_add(argvars, rettv)
9145 typval_T *argvars;
9146 typval_T *rettv;
9147{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009148 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009149}
9150
9151/*
9152 * "complete_check()" function
9153 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009154 static void
9155f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009156 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009157 typval_T *rettv;
9158{
9159 int saved = RedrawingDisabled;
9160
9161 RedrawingDisabled = 0;
9162 ins_compl_check_keys(0);
9163 rettv->vval.v_number = compl_interrupted;
9164 RedrawingDisabled = saved;
9165}
9166#endif
9167
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168/*
9169 * "confirm(message, buttons[, default [, type]])" function
9170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009172f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009173 typval_T *argvars UNUSED;
9174 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175{
9176#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9177 char_u *message;
9178 char_u *buttons = NULL;
9179 char_u buf[NUMBUFLEN];
9180 char_u buf2[NUMBUFLEN];
9181 int def = 1;
9182 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009183 char_u *typestr;
9184 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009186 message = get_tv_string_chk(&argvars[0]);
9187 if (message == NULL)
9188 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009189 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009191 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9192 if (buttons == NULL)
9193 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009194 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009196 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009197 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009199 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9200 if (typestr == NULL)
9201 error = TRUE;
9202 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009204 switch (TOUPPER_ASC(*typestr))
9205 {
9206 case 'E': type = VIM_ERROR; break;
9207 case 'Q': type = VIM_QUESTION; break;
9208 case 'I': type = VIM_INFO; break;
9209 case 'W': type = VIM_WARNING; break;
9210 case 'G': type = VIM_GENERIC; break;
9211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 }
9213 }
9214 }
9215 }
9216
9217 if (buttons == NULL || *buttons == NUL)
9218 buttons = (char_u *)_("&Ok");
9219
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009220 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009221 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223#endif
9224}
9225
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009226/*
9227 * "copy()" function
9228 */
9229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009230f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009231 typval_T *argvars;
9232 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009233{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009234 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009235}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009237#ifdef FEAT_FLOAT
9238/*
9239 * "cos()" function
9240 */
9241 static void
9242f_cos(argvars, rettv)
9243 typval_T *argvars;
9244 typval_T *rettv;
9245{
9246 float_T f;
9247
9248 rettv->v_type = VAR_FLOAT;
9249 if (get_float_arg(argvars, &f) == OK)
9250 rettv->vval.v_float = cos(f);
9251 else
9252 rettv->vval.v_float = 0.0;
9253}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009254
9255/*
9256 * "cosh()" function
9257 */
9258 static void
9259f_cosh(argvars, rettv)
9260 typval_T *argvars;
9261 typval_T *rettv;
9262{
9263 float_T f;
9264
9265 rettv->v_type = VAR_FLOAT;
9266 if (get_float_arg(argvars, &f) == OK)
9267 rettv->vval.v_float = cosh(f);
9268 else
9269 rettv->vval.v_float = 0.0;
9270}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009271#endif
9272
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009274 * "count()" function
9275 */
9276 static void
9277f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009278 typval_T *argvars;
9279 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009280{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009281 long n = 0;
9282 int ic = FALSE;
9283
Bram Moolenaare9a41262005-01-15 22:18:47 +00009284 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009285 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009286 listitem_T *li;
9287 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009288 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009289
Bram Moolenaare9a41262005-01-15 22:18:47 +00009290 if ((l = argvars[0].vval.v_list) != NULL)
9291 {
9292 li = l->lv_first;
9293 if (argvars[2].v_type != VAR_UNKNOWN)
9294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009295 int error = FALSE;
9296
9297 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009298 if (argvars[3].v_type != VAR_UNKNOWN)
9299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009300 idx = get_tv_number_chk(&argvars[3], &error);
9301 if (!error)
9302 {
9303 li = list_find(l, idx);
9304 if (li == NULL)
9305 EMSGN(_(e_listidx), idx);
9306 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009307 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009308 if (error)
9309 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009310 }
9311
9312 for ( ; li != NULL; li = li->li_next)
9313 if (tv_equal(&li->li_tv, &argvars[1], ic))
9314 ++n;
9315 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009316 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009317 else if (argvars[0].v_type == VAR_DICT)
9318 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009319 int todo;
9320 dict_T *d;
9321 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009322
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009323 if ((d = argvars[0].vval.v_dict) != NULL)
9324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009325 int error = FALSE;
9326
Bram Moolenaare9a41262005-01-15 22:18:47 +00009327 if (argvars[2].v_type != VAR_UNKNOWN)
9328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009330 if (argvars[3].v_type != VAR_UNKNOWN)
9331 EMSG(_(e_invarg));
9332 }
9333
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009334 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009335 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009336 {
9337 if (!HASHITEM_EMPTY(hi))
9338 {
9339 --todo;
9340 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9341 ++n;
9342 }
9343 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009344 }
9345 }
9346 else
9347 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009348 rettv->vval.v_number = n;
9349}
9350
9351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9353 *
9354 * Checks the existence of a cscope connection.
9355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009357f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009358 typval_T *argvars UNUSED;
9359 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360{
9361#ifdef FEAT_CSCOPE
9362 int num = 0;
9363 char_u *dbpath = NULL;
9364 char_u *prepend = NULL;
9365 char_u buf[NUMBUFLEN];
9366
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009367 if (argvars[0].v_type != VAR_UNKNOWN
9368 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370 num = (int)get_tv_number(&argvars[0]);
9371 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009372 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009373 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 }
9375
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009376 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377#endif
9378}
9379
9380/*
9381 * "cursor(lnum, col)" function
9382 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009383 * Moves the cursor to the specified line and column.
9384 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009387f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009388 typval_T *argvars;
9389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390{
9391 long line, col;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009392#ifdef FEAT_CONCEAL
9393 linenr_T oldline = curwin->w_cursor.lnum;
9394#endif
Bram Moolenaara5525202006-03-02 22:52:09 +00009395#ifdef FEAT_VIRTUALEDIT
9396 long coladd = 0;
9397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009399 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009400 if (argvars[1].v_type == VAR_UNKNOWN)
9401 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009402 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009403
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009404 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009405 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009406 line = pos.lnum;
9407 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009408#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009409 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009410#endif
9411 }
9412 else
9413 {
9414 line = get_tv_lnum(argvars);
9415 col = get_tv_number_chk(&argvars[1], NULL);
9416#ifdef FEAT_VIRTUALEDIT
9417 if (argvars[2].v_type != VAR_UNKNOWN)
9418 coladd = get_tv_number_chk(&argvars[2], NULL);
9419#endif
9420 }
9421 if (line < 0 || col < 0
9422#ifdef FEAT_VIRTUALEDIT
9423 || coladd < 0
9424#endif
9425 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009426 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 if (line > 0)
9428 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 if (col > 0)
9430 curwin->w_cursor.col = col - 1;
9431#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009432 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433#endif
9434
9435 /* Make sure the cursor is in a valid position. */
9436 check_cursor();
9437#ifdef FEAT_MBYTE
9438 /* Correct cursor for multi-byte character. */
9439 if (has_mbyte)
9440 mb_adjust_cursor();
9441#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009442
9443 curwin->w_set_curswant = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009444#ifdef FEAT_CONCEAL
9445 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
9446 {
9447 update_single_line(curwin, oldline);
9448 update_single_line(curwin, curwin->w_cursor.lnum);
9449 }
9450#endif
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009451 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452}
9453
9454/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009455 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 */
9457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009459 typval_T *argvars;
9460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009462 int noref = 0;
9463
9464 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009465 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009466 if (noref < 0 || noref > 1)
9467 EMSG(_(e_invarg));
9468 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009469 {
9470 current_copyID += COPYID_INC;
9471 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473}
9474
9475/*
9476 * "delete()" function
9477 */
9478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009479f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009480 typval_T *argvars;
9481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482{
9483 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009484 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009486 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487}
9488
9489/*
9490 * "did_filetype()" function
9491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009493f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009494 typval_T *argvars UNUSED;
9495 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496{
9497#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009498 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499#endif
9500}
9501
9502/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009503 * "diff_filler()" function
9504 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009507 typval_T *argvars UNUSED;
9508 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009509{
9510#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009511 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009512#endif
9513}
9514
9515/*
9516 * "diff_hlID()" function
9517 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009520 typval_T *argvars UNUSED;
9521 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009522{
9523#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009524 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009525 static linenr_T prev_lnum = 0;
9526 static int changedtick = 0;
9527 static int fnum = 0;
9528 static int change_start = 0;
9529 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009530 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009531 int filler_lines;
9532 int col;
9533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009534 if (lnum < 0) /* ignore type error in {lnum} arg */
9535 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009536 if (lnum != prev_lnum
9537 || changedtick != curbuf->b_changedtick
9538 || fnum != curbuf->b_fnum)
9539 {
9540 /* New line, buffer, change: need to get the values. */
9541 filler_lines = diff_check(curwin, lnum);
9542 if (filler_lines < 0)
9543 {
9544 if (filler_lines == -1)
9545 {
9546 change_start = MAXCOL;
9547 change_end = -1;
9548 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9549 hlID = HLF_ADD; /* added line */
9550 else
9551 hlID = HLF_CHD; /* changed line */
9552 }
9553 else
9554 hlID = HLF_ADD; /* added line */
9555 }
9556 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009557 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009558 prev_lnum = lnum;
9559 changedtick = curbuf->b_changedtick;
9560 fnum = curbuf->b_fnum;
9561 }
9562
9563 if (hlID == HLF_CHD || hlID == HLF_TXD)
9564 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009565 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009566 if (col >= change_start && col <= change_end)
9567 hlID = HLF_TXD; /* changed text */
9568 else
9569 hlID = HLF_CHD; /* changed line */
9570 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009571 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009572#endif
9573}
9574
9575/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009576 * "empty({expr})" function
9577 */
9578 static void
9579f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009580 typval_T *argvars;
9581 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009582{
9583 int n;
9584
9585 switch (argvars[0].v_type)
9586 {
9587 case VAR_STRING:
9588 case VAR_FUNC:
9589 n = argvars[0].vval.v_string == NULL
9590 || *argvars[0].vval.v_string == NUL;
9591 break;
9592 case VAR_NUMBER:
9593 n = argvars[0].vval.v_number == 0;
9594 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009595#ifdef FEAT_FLOAT
9596 case VAR_FLOAT:
9597 n = argvars[0].vval.v_float == 0.0;
9598 break;
9599#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009600 case VAR_LIST:
9601 n = argvars[0].vval.v_list == NULL
9602 || argvars[0].vval.v_list->lv_first == NULL;
9603 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009604 case VAR_DICT:
9605 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009606 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009607 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009608 default:
9609 EMSG2(_(e_intern2), "f_empty()");
9610 n = 0;
9611 }
9612
9613 rettv->vval.v_number = n;
9614}
9615
9616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 * "escape({string}, {chars})" function
9618 */
9619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009621 typval_T *argvars;
9622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
9624 char_u buf[NUMBUFLEN];
9625
Bram Moolenaar758711c2005-02-02 23:11:38 +00009626 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9627 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009628 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629}
9630
9631/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009632 * "eval()" function
9633 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009634 static void
9635f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009636 typval_T *argvars;
9637 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009638{
9639 char_u *s;
9640
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009641 s = get_tv_string_chk(&argvars[0]);
9642 if (s != NULL)
9643 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009644
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009645 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9646 {
9647 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009648 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009649 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009650 else if (*s != NUL)
9651 EMSG(_(e_trailing));
9652}
9653
9654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 * "eventhandler()" function
9656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009659 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009662 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663}
9664
9665/*
9666 * "executable()" function
9667 */
9668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009670 typval_T *argvars;
9671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009673 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674}
9675
9676/*
9677 * "exists()" function
9678 */
9679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009680f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009681 typval_T *argvars;
9682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683{
9684 char_u *p;
9685 char_u *name;
9686 int n = FALSE;
9687 int len = 0;
9688
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009689 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 if (*p == '$') /* environment variable */
9691 {
9692 /* first try "normal" environment variables (fast) */
9693 if (mch_getenv(p + 1) != NULL)
9694 n = TRUE;
9695 else
9696 {
9697 /* try expanding things like $VIM and ${HOME} */
9698 p = expand_env_save(p);
9699 if (p != NULL && *p != '$')
9700 n = TRUE;
9701 vim_free(p);
9702 }
9703 }
9704 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009707 if (*skipwhite(p) != NUL)
9708 n = FALSE; /* trailing garbage */
9709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 else if (*p == '*') /* internal or user defined function */
9711 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009712 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 }
9714 else if (*p == ':')
9715 {
9716 n = cmd_exists(p + 1);
9717 }
9718 else if (*p == '#')
9719 {
9720#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009721 if (p[1] == '#')
9722 n = autocmd_supported(p + 2);
9723 else
9724 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725#endif
9726 }
9727 else /* internal variable */
9728 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009729 char_u *tofree;
9730 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009732 /* get_name_len() takes care of expanding curly braces */
9733 name = p;
9734 len = get_name_len(&p, &tofree, TRUE, FALSE);
9735 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009737 if (tofree != NULL)
9738 name = tofree;
9739 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9740 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009742 /* handle d.key, l[idx], f(expr) */
9743 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9744 if (n)
9745 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 }
9747 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009748 if (*p != NUL)
9749 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009751 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 }
9753
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009754 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755}
9756
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009757#ifdef FEAT_FLOAT
9758/*
9759 * "exp()" function
9760 */
9761 static void
9762f_exp(argvars, rettv)
9763 typval_T *argvars;
9764 typval_T *rettv;
9765{
9766 float_T f;
9767
9768 rettv->v_type = VAR_FLOAT;
9769 if (get_float_arg(argvars, &f) == OK)
9770 rettv->vval.v_float = exp(f);
9771 else
9772 rettv->vval.v_float = 0.0;
9773}
9774#endif
9775
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776/*
9777 * "expand()" function
9778 */
9779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009780f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009781 typval_T *argvars;
9782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783{
9784 char_u *s;
9785 int len;
9786 char_u *errormsg;
9787 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9788 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009789 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009791 rettv->v_type = VAR_STRING;
9792 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 if (*s == '%' || *s == '#' || *s == '<')
9794 {
9795 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009796 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 --emsg_off;
9798 }
9799 else
9800 {
9801 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009802 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009803 if (argvars[1].v_type != VAR_UNKNOWN
9804 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009806 if (!error)
9807 {
9808 ExpandInit(&xpc);
9809 xpc.xp_context = EXPAND_FILES;
9810 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009811 }
9812 else
9813 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 }
9815}
9816
9817/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009818 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009819 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009820 */
9821 static void
9822f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009823 typval_T *argvars;
9824 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009825{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009826 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009827 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009828 list_T *l1, *l2;
9829 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009830 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009831 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009832
Bram Moolenaare9a41262005-01-15 22:18:47 +00009833 l1 = argvars[0].vval.v_list;
9834 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009835 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9836 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009837 {
9838 if (argvars[2].v_type != VAR_UNKNOWN)
9839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009840 before = get_tv_number_chk(&argvars[2], &error);
9841 if (error)
9842 return; /* type error; errmsg already given */
9843
Bram Moolenaar758711c2005-02-02 23:11:38 +00009844 if (before == l1->lv_len)
9845 item = NULL;
9846 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009847 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009848 item = list_find(l1, before);
9849 if (item == NULL)
9850 {
9851 EMSGN(_(e_listidx), before);
9852 return;
9853 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009854 }
9855 }
9856 else
9857 item = NULL;
9858 list_extend(l1, l2, item);
9859
Bram Moolenaare9a41262005-01-15 22:18:47 +00009860 copy_tv(&argvars[0], rettv);
9861 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009862 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009863 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9864 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009865 dict_T *d1, *d2;
9866 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009867 char_u *action;
9868 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009869 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009870 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009871
9872 d1 = argvars[0].vval.v_dict;
9873 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009874 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9875 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009876 {
9877 /* Check the third argument. */
9878 if (argvars[2].v_type != VAR_UNKNOWN)
9879 {
9880 static char *(av[]) = {"keep", "force", "error"};
9881
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009882 action = get_tv_string_chk(&argvars[2]);
9883 if (action == NULL)
9884 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009885 for (i = 0; i < 3; ++i)
9886 if (STRCMP(action, av[i]) == 0)
9887 break;
9888 if (i == 3)
9889 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009890 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009891 return;
9892 }
9893 }
9894 else
9895 action = (char_u *)"force";
9896
9897 /* Go over all entries in the second dict and add them to the
9898 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009899 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009900 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009901 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009902 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009903 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009904 --todo;
9905 di1 = dict_find(d1, hi2->hi_key, -1);
9906 if (di1 == NULL)
9907 {
9908 di1 = dictitem_copy(HI2DI(hi2));
9909 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9910 dictitem_free(di1);
9911 }
9912 else if (*action == 'e')
9913 {
9914 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9915 break;
9916 }
9917 else if (*action == 'f')
9918 {
9919 clear_tv(&di1->di_tv);
9920 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9921 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009922 }
9923 }
9924
Bram Moolenaare9a41262005-01-15 22:18:47 +00009925 copy_tv(&argvars[0], rettv);
9926 }
9927 }
9928 else
9929 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009930}
9931
9932/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009933 * "feedkeys()" function
9934 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009935 static void
9936f_feedkeys(argvars, rettv)
9937 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009938 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009939{
9940 int remap = TRUE;
9941 char_u *keys, *flags;
9942 char_u nbuf[NUMBUFLEN];
9943 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009944 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009945
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009946 /* This is not allowed in the sandbox. If the commands would still be
9947 * executed in the sandbox it would be OK, but it probably happens later,
9948 * when "sandbox" is no longer set. */
9949 if (check_secure())
9950 return;
9951
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009952 keys = get_tv_string(&argvars[0]);
9953 if (*keys != NUL)
9954 {
9955 if (argvars[1].v_type != VAR_UNKNOWN)
9956 {
9957 flags = get_tv_string_buf(&argvars[1], nbuf);
9958 for ( ; *flags != NUL; ++flags)
9959 {
9960 switch (*flags)
9961 {
9962 case 'n': remap = FALSE; break;
9963 case 'm': remap = TRUE; break;
9964 case 't': typed = TRUE; break;
9965 }
9966 }
9967 }
9968
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009969 /* Need to escape K_SPECIAL and CSI before putting the string in the
9970 * typeahead buffer. */
9971 keys_esc = vim_strsave_escape_csi(keys);
9972 if (keys_esc != NULL)
9973 {
9974 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009975 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009976 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009977 if (vgetc_busy)
9978 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009979 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009980 }
9981}
9982
9983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 * "filereadable()" function
9985 */
9986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009987f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009988 typval_T *argvars;
9989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990{
Bram Moolenaarc236c162008-07-13 17:41:49 +00009991 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 char_u *p;
9993 int n;
9994
Bram Moolenaarc236c162008-07-13 17:41:49 +00009995#ifndef O_NONBLOCK
9996# define O_NONBLOCK 0
9997#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +00009999 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10000 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 {
10002 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010003 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 }
10005 else
10006 n = FALSE;
10007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010008 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009}
10010
10011/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010012 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 * rights to write into.
10014 */
10015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010016f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010017 typval_T *argvars;
10018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010020 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021}
10022
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010023static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010024
10025 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010026findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010027 typval_T *argvars;
10028 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010029 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010030{
10031#ifdef FEAT_SEARCHPATH
10032 char_u *fname;
10033 char_u *fresult = NULL;
10034 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10035 char_u *p;
10036 char_u pathbuf[NUMBUFLEN];
10037 int count = 1;
10038 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010039 int error = FALSE;
10040#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010041
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010042 rettv->vval.v_string = NULL;
10043 rettv->v_type = VAR_STRING;
10044
10045#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010046 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010047
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010048 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010049 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010050 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10051 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010052 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010053 else
10054 {
10055 if (*p != NUL)
10056 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010057
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010058 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010059 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010060 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010061 }
10062
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010063 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10064 error = TRUE;
10065
10066 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010067 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010068 do
10069 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010070 if (rettv->v_type == VAR_STRING)
10071 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010072 fresult = find_file_in_path_option(first ? fname : NULL,
10073 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010074 0, first, path,
10075 find_what,
10076 curbuf->b_ffname,
10077 find_what == FINDFILE_DIR
10078 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010079 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010080
10081 if (fresult != NULL && rettv->v_type == VAR_LIST)
10082 list_append_string(rettv->vval.v_list, fresult, -1);
10083
10084 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010085 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010086
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010087 if (rettv->v_type == VAR_STRING)
10088 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010089#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010090}
10091
Bram Moolenaar33570922005-01-25 22:26:29 +000010092static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10093static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010094
10095/*
10096 * Implementation of map() and filter().
10097 */
10098 static void
10099filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010100 typval_T *argvars;
10101 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010102 int map;
10103{
10104 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010105 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010106 listitem_T *li, *nli;
10107 list_T *l = NULL;
10108 dictitem_T *di;
10109 hashtab_T *ht;
10110 hashitem_T *hi;
10111 dict_T *d = NULL;
10112 typval_T save_val;
10113 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010114 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010115 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010116 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010117 int save_did_emsg;
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010118 int index = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010119
Bram Moolenaare9a41262005-01-15 22:18:47 +000010120 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010121 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010122 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010123 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010124 return;
10125 }
10126 else if (argvars[0].v_type == VAR_DICT)
10127 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010128 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010129 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010130 return;
10131 }
10132 else
10133 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010134 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010135 return;
10136 }
10137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010138 expr = get_tv_string_buf_chk(&argvars[1], buf);
10139 /* On type errors, the preceding call has already displayed an error
10140 * message. Avoid a misleading error message for an empty string that
10141 * was not passed as argument. */
10142 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010144 prepare_vimvar(VV_VAL, &save_val);
10145 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010146
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010147 /* We reset "did_emsg" to be able to detect whether an error
10148 * occurred during evaluation of the expression. */
10149 save_did_emsg = did_emsg;
10150 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010151
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010152 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010153 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010155 vimvars[VV_KEY].vv_type = VAR_STRING;
10156
10157 ht = &d->dv_hashtab;
10158 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010159 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010161 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010162 if (!HASHITEM_EMPTY(hi))
10163 {
10164 --todo;
10165 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010166 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010167 break;
10168 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010169 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010170 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010171 break;
10172 if (!map && rem)
10173 dictitem_remove(d, di);
10174 clear_tv(&vimvars[VV_KEY].vv_tv);
10175 }
10176 }
10177 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010178 }
10179 else
10180 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010181 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010183 for (li = l->lv_first; li != NULL; li = nli)
10184 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010185 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010186 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010187 nli = li->li_next;
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010188 vimvars[VV_KEY].vv_nr = index;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010189 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010190 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010191 break;
10192 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010193 listitem_remove(l, li);
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010194 ++index;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010195 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010196 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010197
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010198 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010199 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010200
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010201 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010202 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010203
10204 copy_tv(&argvars[0], rettv);
10205}
10206
10207 static int
10208filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010209 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010210 char_u *expr;
10211 int map;
10212 int *remp;
10213{
Bram Moolenaar33570922005-01-25 22:26:29 +000010214 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010215 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010216 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010217
Bram Moolenaar33570922005-01-25 22:26:29 +000010218 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010219 s = expr;
10220 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010221 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010222 if (*s != NUL) /* check for trailing chars after expr */
10223 {
10224 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010225 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010226 }
10227 if (map)
10228 {
10229 /* map(): replace the list item value */
10230 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010231 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010232 *tv = rettv;
10233 }
10234 else
10235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010236 int error = FALSE;
10237
Bram Moolenaare9a41262005-01-15 22:18:47 +000010238 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010239 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010240 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010241 /* On type error, nothing has been removed; return FAIL to stop the
10242 * loop. The error message was given by get_tv_number_chk(). */
10243 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010244 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010245 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010246 retval = OK;
10247theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010248 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010249 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010250}
10251
10252/*
10253 * "filter()" function
10254 */
10255 static void
10256f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010257 typval_T *argvars;
10258 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010259{
10260 filter_map(argvars, rettv, FALSE);
10261}
10262
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010263/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010264 * "finddir({fname}[, {path}[, {count}]])" function
10265 */
10266 static void
10267f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010268 typval_T *argvars;
10269 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010270{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010271 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010272}
10273
10274/*
10275 * "findfile({fname}[, {path}[, {count}]])" function
10276 */
10277 static void
10278f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010279 typval_T *argvars;
10280 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010281{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010282 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010283}
10284
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010285#ifdef FEAT_FLOAT
10286/*
10287 * "float2nr({float})" function
10288 */
10289 static void
10290f_float2nr(argvars, rettv)
10291 typval_T *argvars;
10292 typval_T *rettv;
10293{
10294 float_T f;
10295
10296 if (get_float_arg(argvars, &f) == OK)
10297 {
10298 if (f < -0x7fffffff)
10299 rettv->vval.v_number = -0x7fffffff;
10300 else if (f > 0x7fffffff)
10301 rettv->vval.v_number = 0x7fffffff;
10302 else
10303 rettv->vval.v_number = (varnumber_T)f;
10304 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010305}
10306
10307/*
10308 * "floor({float})" function
10309 */
10310 static void
10311f_floor(argvars, rettv)
10312 typval_T *argvars;
10313 typval_T *rettv;
10314{
10315 float_T f;
10316
10317 rettv->v_type = VAR_FLOAT;
10318 if (get_float_arg(argvars, &f) == OK)
10319 rettv->vval.v_float = floor(f);
10320 else
10321 rettv->vval.v_float = 0.0;
10322}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010323
10324/*
10325 * "fmod()" function
10326 */
10327 static void
10328f_fmod(argvars, rettv)
10329 typval_T *argvars;
10330 typval_T *rettv;
10331{
10332 float_T fx, fy;
10333
10334 rettv->v_type = VAR_FLOAT;
10335 if (get_float_arg(argvars, &fx) == OK
10336 && get_float_arg(&argvars[1], &fy) == OK)
10337 rettv->vval.v_float = fmod(fx, fy);
10338 else
10339 rettv->vval.v_float = 0.0;
10340}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010341#endif
10342
Bram Moolenaar0d660222005-01-07 21:51:51 +000010343/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010344 * "fnameescape({string})" function
10345 */
10346 static void
10347f_fnameescape(argvars, rettv)
10348 typval_T *argvars;
10349 typval_T *rettv;
10350{
10351 rettv->vval.v_string = vim_strsave_fnameescape(
10352 get_tv_string(&argvars[0]), FALSE);
10353 rettv->v_type = VAR_STRING;
10354}
10355
10356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 * "fnamemodify({fname}, {mods})" function
10358 */
10359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010360f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010361 typval_T *argvars;
10362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363{
10364 char_u *fname;
10365 char_u *mods;
10366 int usedlen = 0;
10367 int len;
10368 char_u *fbuf = NULL;
10369 char_u buf[NUMBUFLEN];
10370
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010371 fname = get_tv_string_chk(&argvars[0]);
10372 mods = get_tv_string_buf_chk(&argvars[1], buf);
10373 if (fname == NULL || mods == NULL)
10374 fname = NULL;
10375 else
10376 {
10377 len = (int)STRLEN(fname);
10378 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010381 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010383 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010385 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 vim_free(fbuf);
10387}
10388
Bram Moolenaar33570922005-01-25 22:26:29 +000010389static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390
10391/*
10392 * "foldclosed()" function
10393 */
10394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010395foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010396 typval_T *argvars;
10397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 int end;
10399{
10400#ifdef FEAT_FOLDING
10401 linenr_T lnum;
10402 linenr_T first, last;
10403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010404 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10406 {
10407 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10408 {
10409 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010410 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010412 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 return;
10414 }
10415 }
10416#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010417 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418}
10419
10420/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010421 * "foldclosed()" function
10422 */
10423 static void
10424f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010425 typval_T *argvars;
10426 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010427{
10428 foldclosed_both(argvars, rettv, FALSE);
10429}
10430
10431/*
10432 * "foldclosedend()" function
10433 */
10434 static void
10435f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010436 typval_T *argvars;
10437 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010438{
10439 foldclosed_both(argvars, rettv, TRUE);
10440}
10441
10442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 * "foldlevel()" function
10444 */
10445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010446f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010447 typval_T *argvars;
10448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449{
10450#ifdef FEAT_FOLDING
10451 linenr_T lnum;
10452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010453 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010455 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457}
10458
10459/*
10460 * "foldtext()" function
10461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010463f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010464 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466{
10467#ifdef FEAT_FOLDING
10468 linenr_T lnum;
10469 char_u *s;
10470 char_u *r;
10471 int len;
10472 char *txt;
10473#endif
10474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010475 rettv->v_type = VAR_STRING;
10476 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010478 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10479 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10480 <= curbuf->b_ml.ml_line_count
10481 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 {
10483 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010484 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10485 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 {
10487 if (!linewhite(lnum))
10488 break;
10489 ++lnum;
10490 }
10491
10492 /* Find interesting text in this line. */
10493 s = skipwhite(ml_get(lnum));
10494 /* skip C comment-start */
10495 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010496 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010498 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010499 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010500 {
10501 s = skipwhite(ml_get(lnum + 1));
10502 if (*s == '*')
10503 s = skipwhite(s + 1);
10504 }
10505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506 txt = _("+-%s%3ld lines: ");
10507 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010508 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 + 20 /* for %3ld */
10510 + STRLEN(s))); /* concatenated */
10511 if (r != NULL)
10512 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010513 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10514 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10515 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 len = (int)STRLEN(r);
10517 STRCAT(r, s);
10518 /* remove 'foldmarker' and 'commentstring' */
10519 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010520 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 }
10522 }
10523#endif
10524}
10525
10526/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010527 * "foldtextresult(lnum)" function
10528 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010530f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010531 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010532 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010533{
10534#ifdef FEAT_FOLDING
10535 linenr_T lnum;
10536 char_u *text;
10537 char_u buf[51];
10538 foldinfo_T foldinfo;
10539 int fold_count;
10540#endif
10541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010542 rettv->v_type = VAR_STRING;
10543 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010544#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010545 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010546 /* treat illegal types and illegal string values for {lnum} the same */
10547 if (lnum < 0)
10548 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010549 fold_count = foldedCount(curwin, lnum, &foldinfo);
10550 if (fold_count > 0)
10551 {
10552 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10553 &foldinfo, buf);
10554 if (text == buf)
10555 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010557 }
10558#endif
10559}
10560
10561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 * "foreground()" function
10563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010565f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010566 typval_T *argvars UNUSED;
10567 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569#ifdef FEAT_GUI
10570 if (gui.in_use)
10571 gui_mch_set_foreground();
10572#else
10573# ifdef WIN32
10574 win32_set_foreground();
10575# endif
10576#endif
10577}
10578
10579/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010580 * "function()" function
10581 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010583f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010584 typval_T *argvars;
10585 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010586{
10587 char_u *s;
10588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010589 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010590 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010591 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010592 /* Don't check an autoload name for existence here. */
10593 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010594 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010595 else
10596 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010597 rettv->vval.v_string = vim_strsave(s);
10598 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010599 }
10600}
10601
10602/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010603 * "garbagecollect()" function
10604 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010605 static void
10606f_garbagecollect(argvars, rettv)
10607 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010608 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010609{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010610 /* This is postponed until we are back at the toplevel, because we may be
10611 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10612 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010613
10614 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10615 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010616}
10617
10618/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010619 * "get()" function
10620 */
10621 static void
10622f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010623 typval_T *argvars;
10624 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010625{
Bram Moolenaar33570922005-01-25 22:26:29 +000010626 listitem_T *li;
10627 list_T *l;
10628 dictitem_T *di;
10629 dict_T *d;
10630 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010631
Bram Moolenaare9a41262005-01-15 22:18:47 +000010632 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010633 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010634 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010635 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010636 int error = FALSE;
10637
10638 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10639 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010640 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010641 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010642 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010643 else if (argvars[0].v_type == VAR_DICT)
10644 {
10645 if ((d = argvars[0].vval.v_dict) != NULL)
10646 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010647 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010648 if (di != NULL)
10649 tv = &di->di_tv;
10650 }
10651 }
10652 else
10653 EMSG2(_(e_listdictarg), "get()");
10654
10655 if (tv == NULL)
10656 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010657 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010658 copy_tv(&argvars[2], rettv);
10659 }
10660 else
10661 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010662}
10663
Bram Moolenaar342337a2005-07-21 21:11:17 +000010664static 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 +000010665
10666/*
10667 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010668 * Return a range (from start to end) of lines in rettv from the specified
10669 * buffer.
10670 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010671 */
10672 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010673get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010674 buf_T *buf;
10675 linenr_T start;
10676 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010677 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010678 typval_T *rettv;
10679{
10680 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010681
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010682 if (retlist && rettv_list_alloc(rettv) == FAIL)
10683 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010684
10685 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10686 return;
10687
10688 if (!retlist)
10689 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010690 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10691 p = ml_get_buf(buf, start, FALSE);
10692 else
10693 p = (char_u *)"";
10694
10695 rettv->v_type = VAR_STRING;
10696 rettv->vval.v_string = vim_strsave(p);
10697 }
10698 else
10699 {
10700 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010701 return;
10702
10703 if (start < 1)
10704 start = 1;
10705 if (end > buf->b_ml.ml_line_count)
10706 end = buf->b_ml.ml_line_count;
10707 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010708 if (list_append_string(rettv->vval.v_list,
10709 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010710 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010711 }
10712}
10713
10714/*
10715 * "getbufline()" function
10716 */
10717 static void
10718f_getbufline(argvars, rettv)
10719 typval_T *argvars;
10720 typval_T *rettv;
10721{
10722 linenr_T lnum;
10723 linenr_T end;
10724 buf_T *buf;
10725
10726 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10727 ++emsg_off;
10728 buf = get_buf_tv(&argvars[0]);
10729 --emsg_off;
10730
Bram Moolenaar661b1822005-07-28 22:36:45 +000010731 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010732 if (argvars[2].v_type == VAR_UNKNOWN)
10733 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010734 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010735 end = get_tv_lnum_buf(&argvars[2], buf);
10736
Bram Moolenaar342337a2005-07-21 21:11:17 +000010737 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010738}
10739
Bram Moolenaar0d660222005-01-07 21:51:51 +000010740/*
10741 * "getbufvar()" function
10742 */
10743 static void
10744f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010745 typval_T *argvars;
10746 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010747{
10748 buf_T *buf;
10749 buf_T *save_curbuf;
10750 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010751 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010752
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010753 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10754 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010755 ++emsg_off;
10756 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010757
10758 rettv->v_type = VAR_STRING;
10759 rettv->vval.v_string = NULL;
10760
10761 if (buf != NULL && varname != NULL)
10762 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010763 /* set curbuf to be our buf, temporarily */
10764 save_curbuf = curbuf;
10765 curbuf = buf;
10766
Bram Moolenaar0d660222005-01-07 21:51:51 +000010767 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010768 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010769 else
10770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010771 if (*varname == NUL)
10772 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10773 * scope prefix before the NUL byte is required by
10774 * find_var_in_ht(). */
10775 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010776 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010777 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010778 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010779 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010780 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010781
10782 /* restore previous notion of curbuf */
10783 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010784 }
10785
10786 --emsg_off;
10787}
10788
10789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 * "getchar()" function
10791 */
10792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010793f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010794 typval_T *argvars;
10795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796{
10797 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010798 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010800 /* Position the cursor. Needed after a message that ends in a space. */
10801 windgoto(msg_row, msg_col);
10802
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 ++no_mapping;
10804 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010805 for (;;)
10806 {
10807 if (argvars[0].v_type == VAR_UNKNOWN)
10808 /* getchar(): blocking wait. */
10809 n = safe_vgetc();
10810 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10811 /* getchar(1): only check if char avail */
10812 n = vpeekc();
10813 else if (error || vpeekc() == NUL)
10814 /* illegal argument or getchar(0) and no char avail: return zero */
10815 n = 0;
10816 else
10817 /* getchar(0) and char avail: return char */
10818 n = safe_vgetc();
10819 if (n == K_IGNORE)
10820 continue;
10821 break;
10822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823 --no_mapping;
10824 --allow_keys;
10825
Bram Moolenaar219b8702006-11-01 14:32:36 +000010826 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10827 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10828 vimvars[VV_MOUSE_COL].vv_nr = 0;
10829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010830 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 if (IS_SPECIAL(n) || mod_mask != 0)
10832 {
10833 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10834 int i = 0;
10835
10836 /* Turn a special key into three bytes, plus modifier. */
10837 if (mod_mask != 0)
10838 {
10839 temp[i++] = K_SPECIAL;
10840 temp[i++] = KS_MODIFIER;
10841 temp[i++] = mod_mask;
10842 }
10843 if (IS_SPECIAL(n))
10844 {
10845 temp[i++] = K_SPECIAL;
10846 temp[i++] = K_SECOND(n);
10847 temp[i++] = K_THIRD(n);
10848 }
10849#ifdef FEAT_MBYTE
10850 else if (has_mbyte)
10851 i += (*mb_char2bytes)(n, temp + i);
10852#endif
10853 else
10854 temp[i++] = n;
10855 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010856 rettv->v_type = VAR_STRING;
10857 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010858
10859#ifdef FEAT_MOUSE
10860 if (n == K_LEFTMOUSE
10861 || n == K_LEFTMOUSE_NM
10862 || n == K_LEFTDRAG
10863 || n == K_LEFTRELEASE
10864 || n == K_LEFTRELEASE_NM
10865 || n == K_MIDDLEMOUSE
10866 || n == K_MIDDLEDRAG
10867 || n == K_MIDDLERELEASE
10868 || n == K_RIGHTMOUSE
10869 || n == K_RIGHTDRAG
10870 || n == K_RIGHTRELEASE
10871 || n == K_X1MOUSE
10872 || n == K_X1DRAG
10873 || n == K_X1RELEASE
10874 || n == K_X2MOUSE
10875 || n == K_X2DRAG
10876 || n == K_X2RELEASE
10877 || n == K_MOUSEDOWN
10878 || n == K_MOUSEUP)
10879 {
10880 int row = mouse_row;
10881 int col = mouse_col;
10882 win_T *win;
10883 linenr_T lnum;
10884# ifdef FEAT_WINDOWS
10885 win_T *wp;
10886# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010887 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010888
10889 if (row >= 0 && col >= 0)
10890 {
10891 /* Find the window at the mouse coordinates and compute the
10892 * text position. */
10893 win = mouse_find_win(&row, &col);
10894 (void)mouse_comp_pos(win, &row, &col, &lnum);
10895# ifdef FEAT_WINDOWS
10896 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010897 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010898# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010899 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010900 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10901 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10902 }
10903 }
10904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 }
10906}
10907
10908/*
10909 * "getcharmod()" function
10910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010913 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010916 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917}
10918
10919/*
10920 * "getcmdline()" function
10921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010923f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010924 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010927 rettv->v_type = VAR_STRING;
10928 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929}
10930
10931/*
10932 * "getcmdpos()" function
10933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010935f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010936 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010939 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940}
10941
10942/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010943 * "getcmdtype()" function
10944 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010945 static void
10946f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010947 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010948 typval_T *rettv;
10949{
10950 rettv->v_type = VAR_STRING;
10951 rettv->vval.v_string = alloc(2);
10952 if (rettv->vval.v_string != NULL)
10953 {
10954 rettv->vval.v_string[0] = get_cmdline_type();
10955 rettv->vval.v_string[1] = NUL;
10956 }
10957}
10958
10959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 * "getcwd()" function
10961 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010963f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010964 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966{
10967 char_u cwd[MAXPATHL];
10968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010969 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010971 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 else
10973 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010974 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010976 if (rettv->vval.v_string != NULL)
10977 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978#endif
10979 }
10980}
10981
10982/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010983 * "getfontname()" function
10984 */
10985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010986f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010987 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010988 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010989{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010990 rettv->v_type = VAR_STRING;
10991 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010992#ifdef FEAT_GUI
10993 if (gui.in_use)
10994 {
10995 GuiFont font;
10996 char_u *name = NULL;
10997
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010998 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010999 {
11000 /* Get the "Normal" font. Either the name saved by
11001 * hl_set_font_name() or from the font ID. */
11002 font = gui.norm_font;
11003 name = hl_get_font_name();
11004 }
11005 else
11006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011007 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011008 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11009 return;
11010 font = gui_mch_get_font(name, FALSE);
11011 if (font == NOFONT)
11012 return; /* Invalid font name, return empty string. */
11013 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011014 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011015 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011016 gui_mch_free_font(font);
11017 }
11018#endif
11019}
11020
11021/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011022 * "getfperm({fname})" function
11023 */
11024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011026 typval_T *argvars;
11027 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011028{
11029 char_u *fname;
11030 struct stat st;
11031 char_u *perm = NULL;
11032 char_u flags[] = "rwx";
11033 int i;
11034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011035 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011036
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011037 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011038 if (mch_stat((char *)fname, &st) >= 0)
11039 {
11040 perm = vim_strsave((char_u *)"---------");
11041 if (perm != NULL)
11042 {
11043 for (i = 0; i < 9; i++)
11044 {
11045 if (st.st_mode & (1 << (8 - i)))
11046 perm[i] = flags[i % 3];
11047 }
11048 }
11049 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011050 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011051}
11052
11053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 * "getfsize({fname})" function
11055 */
11056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011057f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011058 typval_T *argvars;
11059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060{
11061 char_u *fname;
11062 struct stat st;
11063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011064 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011066 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067
11068 if (mch_stat((char *)fname, &st) >= 0)
11069 {
11070 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011071 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011073 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011075
11076 /* non-perfect check for overflow */
11077 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11078 rettv->vval.v_number = -2;
11079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 }
11081 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011082 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083}
11084
11085/*
11086 * "getftime({fname})" function
11087 */
11088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011089f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011090 typval_T *argvars;
11091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092{
11093 char_u *fname;
11094 struct stat st;
11095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011096 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097
11098 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011099 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011101 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102}
11103
11104/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011105 * "getftype({fname})" function
11106 */
11107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011108f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011109 typval_T *argvars;
11110 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011111{
11112 char_u *fname;
11113 struct stat st;
11114 char_u *type = NULL;
11115 char *t;
11116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011117 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011120 if (mch_lstat((char *)fname, &st) >= 0)
11121 {
11122#ifdef S_ISREG
11123 if (S_ISREG(st.st_mode))
11124 t = "file";
11125 else if (S_ISDIR(st.st_mode))
11126 t = "dir";
11127# ifdef S_ISLNK
11128 else if (S_ISLNK(st.st_mode))
11129 t = "link";
11130# endif
11131# ifdef S_ISBLK
11132 else if (S_ISBLK(st.st_mode))
11133 t = "bdev";
11134# endif
11135# ifdef S_ISCHR
11136 else if (S_ISCHR(st.st_mode))
11137 t = "cdev";
11138# endif
11139# ifdef S_ISFIFO
11140 else if (S_ISFIFO(st.st_mode))
11141 t = "fifo";
11142# endif
11143# ifdef S_ISSOCK
11144 else if (S_ISSOCK(st.st_mode))
11145 t = "fifo";
11146# endif
11147 else
11148 t = "other";
11149#else
11150# ifdef S_IFMT
11151 switch (st.st_mode & S_IFMT)
11152 {
11153 case S_IFREG: t = "file"; break;
11154 case S_IFDIR: t = "dir"; break;
11155# ifdef S_IFLNK
11156 case S_IFLNK: t = "link"; break;
11157# endif
11158# ifdef S_IFBLK
11159 case S_IFBLK: t = "bdev"; break;
11160# endif
11161# ifdef S_IFCHR
11162 case S_IFCHR: t = "cdev"; break;
11163# endif
11164# ifdef S_IFIFO
11165 case S_IFIFO: t = "fifo"; break;
11166# endif
11167# ifdef S_IFSOCK
11168 case S_IFSOCK: t = "socket"; break;
11169# endif
11170 default: t = "other";
11171 }
11172# else
11173 if (mch_isdir(fname))
11174 t = "dir";
11175 else
11176 t = "file";
11177# endif
11178#endif
11179 type = vim_strsave((char_u *)t);
11180 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011181 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011182}
11183
11184/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011185 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011186 */
11187 static void
11188f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011189 typval_T *argvars;
11190 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011191{
11192 linenr_T lnum;
11193 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011194 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011195
11196 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011197 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011198 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011199 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011200 retlist = FALSE;
11201 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011202 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011203 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011204 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011205 retlist = TRUE;
11206 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011207
Bram Moolenaar342337a2005-07-21 21:11:17 +000011208 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011209}
11210
11211/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011212 * "getmatches()" function
11213 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011214 static void
11215f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011216 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011217 typval_T *rettv;
11218{
11219#ifdef FEAT_SEARCH_EXTRA
11220 dict_T *dict;
11221 matchitem_T *cur = curwin->w_match_head;
11222
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011223 if (rettv_list_alloc(rettv) == OK)
11224 {
11225 while (cur != NULL)
11226 {
11227 dict = dict_alloc();
11228 if (dict == NULL)
11229 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011230 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11231 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11232 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11233 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11234 list_append_dict(rettv->vval.v_list, dict);
11235 cur = cur->next;
11236 }
11237 }
11238#endif
11239}
11240
11241/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011242 * "getpid()" function
11243 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011244 static void
11245f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011246 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011247 typval_T *rettv;
11248{
11249 rettv->vval.v_number = mch_get_pid();
11250}
11251
11252/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011253 * "getpos(string)" function
11254 */
11255 static void
11256f_getpos(argvars, rettv)
11257 typval_T *argvars;
11258 typval_T *rettv;
11259{
11260 pos_T *fp;
11261 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011262 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011263
11264 if (rettv_list_alloc(rettv) == OK)
11265 {
11266 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011267 fp = var2fpos(&argvars[0], TRUE, &fnum);
11268 if (fnum != -1)
11269 list_append_number(l, (varnumber_T)fnum);
11270 else
11271 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011272 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11273 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011274 list_append_number(l, (fp != NULL)
11275 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011276 : (varnumber_T)0);
11277 list_append_number(l,
11278#ifdef FEAT_VIRTUALEDIT
11279 (fp != NULL) ? (varnumber_T)fp->coladd :
11280#endif
11281 (varnumber_T)0);
11282 }
11283 else
11284 rettv->vval.v_number = FALSE;
11285}
11286
11287/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011288 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011289 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011290 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011291f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011292 typval_T *argvars UNUSED;
11293 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011294{
11295#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011296 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011297#endif
11298
Bram Moolenaar2641f772005-03-25 21:58:17 +000011299#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011300 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011301 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011302 wp = NULL;
11303 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11304 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011305 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011306 if (wp == NULL)
11307 return;
11308 }
11309
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011310 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011311 }
11312#endif
11313}
11314
11315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316 * "getreg()" function
11317 */
11318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011319f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011320 typval_T *argvars;
11321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322{
11323 char_u *strregname;
11324 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011325 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011326 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011328 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011330 strregname = get_tv_string_chk(&argvars[0]);
11331 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011332 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011333 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011336 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337 regname = (strregname == NULL ? '"' : *strregname);
11338 if (regname == 0)
11339 regname = '"';
11340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011341 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011342 rettv->vval.v_string = error ? NULL :
11343 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344}
11345
11346/*
11347 * "getregtype()" function
11348 */
11349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011350f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011351 typval_T *argvars;
11352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353{
11354 char_u *strregname;
11355 int regname;
11356 char_u buf[NUMBUFLEN + 2];
11357 long reglen = 0;
11358
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011359 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011360 {
11361 strregname = get_tv_string_chk(&argvars[0]);
11362 if (strregname == NULL) /* type error; errmsg already given */
11363 {
11364 rettv->v_type = VAR_STRING;
11365 rettv->vval.v_string = NULL;
11366 return;
11367 }
11368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 else
11370 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011371 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372
11373 regname = (strregname == NULL ? '"' : *strregname);
11374 if (regname == 0)
11375 regname = '"';
11376
11377 buf[0] = NUL;
11378 buf[1] = NUL;
11379 switch (get_reg_type(regname, &reglen))
11380 {
11381 case MLINE: buf[0] = 'V'; break;
11382 case MCHAR: buf[0] = 'v'; break;
11383#ifdef FEAT_VISUAL
11384 case MBLOCK:
11385 buf[0] = Ctrl_V;
11386 sprintf((char *)buf + 1, "%ld", reglen + 1);
11387 break;
11388#endif
11389 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 rettv->v_type = VAR_STRING;
11391 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392}
11393
11394/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011395 * "gettabvar()" function
11396 */
11397 static void
11398f_gettabvar(argvars, rettv)
11399 typval_T *argvars;
11400 typval_T *rettv;
11401{
11402 tabpage_T *tp;
11403 dictitem_T *v;
11404 char_u *varname;
11405
11406 rettv->v_type = VAR_STRING;
11407 rettv->vval.v_string = NULL;
11408
11409 varname = get_tv_string_chk(&argvars[1]);
11410 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11411 if (tp != NULL && varname != NULL)
11412 {
11413 /* look up the variable */
11414 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11415 if (v != NULL)
11416 copy_tv(&v->di_tv, rettv);
11417 }
11418}
11419
11420/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011421 * "gettabwinvar()" function
11422 */
11423 static void
11424f_gettabwinvar(argvars, rettv)
11425 typval_T *argvars;
11426 typval_T *rettv;
11427{
11428 getwinvar(argvars, rettv, 1);
11429}
11430
11431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 * "getwinposx()" function
11433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011436 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440#ifdef FEAT_GUI
11441 if (gui.in_use)
11442 {
11443 int x, y;
11444
11445 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447 }
11448#endif
11449}
11450
11451/*
11452 * "getwinposy()" function
11453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011455f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011456 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011459 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460#ifdef FEAT_GUI
11461 if (gui.in_use)
11462 {
11463 int x, y;
11464
11465 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 }
11468#endif
11469}
11470
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011471/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011472 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011473 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011474 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011475find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011476 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011477 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011478{
11479#ifdef FEAT_WINDOWS
11480 win_T *wp;
11481#endif
11482 int nr;
11483
11484 nr = get_tv_number_chk(vp, NULL);
11485
11486#ifdef FEAT_WINDOWS
11487 if (nr < 0)
11488 return NULL;
11489 if (nr == 0)
11490 return curwin;
11491
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011492 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11493 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011494 if (--nr <= 0)
11495 break;
11496 return wp;
11497#else
11498 if (nr == 0 || nr == 1)
11499 return curwin;
11500 return NULL;
11501#endif
11502}
11503
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504/*
11505 * "getwinvar()" function
11506 */
11507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011508f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011509 typval_T *argvars;
11510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011512 getwinvar(argvars, rettv, 0);
11513}
11514
11515/*
11516 * getwinvar() and gettabwinvar()
11517 */
11518 static void
11519getwinvar(argvars, rettv, off)
11520 typval_T *argvars;
11521 typval_T *rettv;
11522 int off; /* 1 for gettabwinvar() */
11523{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524 win_T *win, *oldcurwin;
11525 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011526 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011527 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011529#ifdef FEAT_WINDOWS
11530 if (off == 1)
11531 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11532 else
11533 tp = curtab;
11534#endif
11535 win = find_win_by_nr(&argvars[off], tp);
11536 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011537 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011539 rettv->v_type = VAR_STRING;
11540 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541
11542 if (win != NULL && varname != NULL)
11543 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011544 /* Set curwin to be our win, temporarily. Also set curbuf, so
11545 * that we can get buffer-local options. */
11546 oldcurwin = curwin;
11547 curwin = win;
11548 curbuf = win->w_buffer;
11549
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011551 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552 else
11553 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011554 if (*varname == NUL)
11555 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11556 * scope prefix before the NUL byte is required by
11557 * find_var_in_ht(). */
11558 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011560 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011562 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011564
11565 /* restore previous notion of curwin */
11566 curwin = oldcurwin;
11567 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568 }
11569
11570 --emsg_off;
11571}
11572
11573/*
11574 * "glob()" function
11575 */
11576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011577f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011578 typval_T *argvars;
11579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011581 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011583 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011585 /* When the optional second argument is non-zero, don't remove matches
11586 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11587 if (argvars[1].v_type != VAR_UNKNOWN
11588 && get_tv_number_chk(&argvars[1], &error))
11589 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011590 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011591 if (!error)
11592 {
11593 ExpandInit(&xpc);
11594 xpc.xp_context = EXPAND_FILES;
11595 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11596 NULL, flags, WILD_ALL);
11597 }
11598 else
11599 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600}
11601
11602/*
11603 * "globpath()" function
11604 */
11605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011606f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011607 typval_T *argvars;
11608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011610 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011612 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011613 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011615 /* When the optional second argument is non-zero, don't remove matches
11616 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11617 if (argvars[2].v_type != VAR_UNKNOWN
11618 && get_tv_number_chk(&argvars[2], &error))
11619 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011620 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011621 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011622 rettv->vval.v_string = NULL;
11623 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011624 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11625 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626}
11627
11628/*
11629 * "has()" function
11630 */
11631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011632f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011633 typval_T *argvars;
11634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635{
11636 int i;
11637 char_u *name;
11638 int n = FALSE;
11639 static char *(has_list[]) =
11640 {
11641#ifdef AMIGA
11642 "amiga",
11643# ifdef FEAT_ARP
11644 "arp",
11645# endif
11646#endif
11647#ifdef __BEOS__
11648 "beos",
11649#endif
11650#ifdef MSDOS
11651# ifdef DJGPP
11652 "dos32",
11653# else
11654 "dos16",
11655# endif
11656#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011657#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658 "mac",
11659#endif
11660#if defined(MACOS_X_UNIX)
11661 "macunix",
11662#endif
11663#ifdef OS2
11664 "os2",
11665#endif
11666#ifdef __QNX__
11667 "qnx",
11668#endif
11669#ifdef RISCOS
11670 "riscos",
11671#endif
11672#ifdef UNIX
11673 "unix",
11674#endif
11675#ifdef VMS
11676 "vms",
11677#endif
11678#ifdef WIN16
11679 "win16",
11680#endif
11681#ifdef WIN32
11682 "win32",
11683#endif
11684#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11685 "win32unix",
11686#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011687#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 "win64",
11689#endif
11690#ifdef EBCDIC
11691 "ebcdic",
11692#endif
11693#ifndef CASE_INSENSITIVE_FILENAME
11694 "fname_case",
11695#endif
11696#ifdef FEAT_ARABIC
11697 "arabic",
11698#endif
11699#ifdef FEAT_AUTOCMD
11700 "autocmd",
11701#endif
11702#ifdef FEAT_BEVAL
11703 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011704# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11705 "balloon_multiline",
11706# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707#endif
11708#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11709 "builtin_terms",
11710# ifdef ALL_BUILTIN_TCAPS
11711 "all_builtin_terms",
11712# endif
11713#endif
11714#ifdef FEAT_BYTEOFF
11715 "byte_offset",
11716#endif
11717#ifdef FEAT_CINDENT
11718 "cindent",
11719#endif
11720#ifdef FEAT_CLIENTSERVER
11721 "clientserver",
11722#endif
11723#ifdef FEAT_CLIPBOARD
11724 "clipboard",
11725#endif
11726#ifdef FEAT_CMDL_COMPL
11727 "cmdline_compl",
11728#endif
11729#ifdef FEAT_CMDHIST
11730 "cmdline_hist",
11731#endif
11732#ifdef FEAT_COMMENTS
11733 "comments",
11734#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011735#ifdef FEAT_CONCEAL
11736 "conceal",
11737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738#ifdef FEAT_CRYPT
11739 "cryptv",
11740#endif
11741#ifdef FEAT_CSCOPE
11742 "cscope",
11743#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011744#ifdef FEAT_CURSORBIND
11745 "cursorbind",
11746#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011747#ifdef CURSOR_SHAPE
11748 "cursorshape",
11749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750#ifdef DEBUG
11751 "debug",
11752#endif
11753#ifdef FEAT_CON_DIALOG
11754 "dialog_con",
11755#endif
11756#ifdef FEAT_GUI_DIALOG
11757 "dialog_gui",
11758#endif
11759#ifdef FEAT_DIFF
11760 "diff",
11761#endif
11762#ifdef FEAT_DIGRAPHS
11763 "digraphs",
11764#endif
11765#ifdef FEAT_DND
11766 "dnd",
11767#endif
11768#ifdef FEAT_EMACS_TAGS
11769 "emacs_tags",
11770#endif
11771 "eval", /* always present, of course! */
11772#ifdef FEAT_EX_EXTRA
11773 "ex_extra",
11774#endif
11775#ifdef FEAT_SEARCH_EXTRA
11776 "extra_search",
11777#endif
11778#ifdef FEAT_FKMAP
11779 "farsi",
11780#endif
11781#ifdef FEAT_SEARCHPATH
11782 "file_in_path",
11783#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011784#if defined(UNIX) && !defined(USE_SYSTEM)
11785 "filterpipe",
11786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787#ifdef FEAT_FIND_ID
11788 "find_in_path",
11789#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011790#ifdef FEAT_FLOAT
11791 "float",
11792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793#ifdef FEAT_FOLDING
11794 "folding",
11795#endif
11796#ifdef FEAT_FOOTER
11797 "footer",
11798#endif
11799#if !defined(USE_SYSTEM) && defined(UNIX)
11800 "fork",
11801#endif
11802#ifdef FEAT_GETTEXT
11803 "gettext",
11804#endif
11805#ifdef FEAT_GUI
11806 "gui",
11807#endif
11808#ifdef FEAT_GUI_ATHENA
11809# ifdef FEAT_GUI_NEXTAW
11810 "gui_neXtaw",
11811# else
11812 "gui_athena",
11813# endif
11814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815#ifdef FEAT_GUI_GTK
11816 "gui_gtk",
11817# ifdef HAVE_GTK2
11818 "gui_gtk2",
11819# endif
11820#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011821#ifdef FEAT_GUI_GNOME
11822 "gui_gnome",
11823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824#ifdef FEAT_GUI_MAC
11825 "gui_mac",
11826#endif
11827#ifdef FEAT_GUI_MOTIF
11828 "gui_motif",
11829#endif
11830#ifdef FEAT_GUI_PHOTON
11831 "gui_photon",
11832#endif
11833#ifdef FEAT_GUI_W16
11834 "gui_win16",
11835#endif
11836#ifdef FEAT_GUI_W32
11837 "gui_win32",
11838#endif
11839#ifdef FEAT_HANGULIN
11840 "hangul_input",
11841#endif
11842#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11843 "iconv",
11844#endif
11845#ifdef FEAT_INS_EXPAND
11846 "insert_expand",
11847#endif
11848#ifdef FEAT_JUMPLIST
11849 "jumplist",
11850#endif
11851#ifdef FEAT_KEYMAP
11852 "keymap",
11853#endif
11854#ifdef FEAT_LANGMAP
11855 "langmap",
11856#endif
11857#ifdef FEAT_LIBCALL
11858 "libcall",
11859#endif
11860#ifdef FEAT_LINEBREAK
11861 "linebreak",
11862#endif
11863#ifdef FEAT_LISP
11864 "lispindent",
11865#endif
11866#ifdef FEAT_LISTCMDS
11867 "listcmds",
11868#endif
11869#ifdef FEAT_LOCALMAP
11870 "localmap",
11871#endif
11872#ifdef FEAT_MENU
11873 "menu",
11874#endif
11875#ifdef FEAT_SESSION
11876 "mksession",
11877#endif
11878#ifdef FEAT_MODIFY_FNAME
11879 "modify_fname",
11880#endif
11881#ifdef FEAT_MOUSE
11882 "mouse",
11883#endif
11884#ifdef FEAT_MOUSESHAPE
11885 "mouseshape",
11886#endif
11887#if defined(UNIX) || defined(VMS)
11888# ifdef FEAT_MOUSE_DEC
11889 "mouse_dec",
11890# endif
11891# ifdef FEAT_MOUSE_GPM
11892 "mouse_gpm",
11893# endif
11894# ifdef FEAT_MOUSE_JSB
11895 "mouse_jsbterm",
11896# endif
11897# ifdef FEAT_MOUSE_NET
11898 "mouse_netterm",
11899# endif
11900# ifdef FEAT_MOUSE_PTERM
11901 "mouse_pterm",
11902# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011903# ifdef FEAT_SYSMOUSE
11904 "mouse_sysmouse",
11905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906# ifdef FEAT_MOUSE_XTERM
11907 "mouse_xterm",
11908# endif
11909#endif
11910#ifdef FEAT_MBYTE
11911 "multi_byte",
11912#endif
11913#ifdef FEAT_MBYTE_IME
11914 "multi_byte_ime",
11915#endif
11916#ifdef FEAT_MULTI_LANG
11917 "multi_lang",
11918#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011919#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011920#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011921 "mzscheme",
11922#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924#ifdef FEAT_OLE
11925 "ole",
11926#endif
11927#ifdef FEAT_OSFILETYPE
11928 "osfiletype",
11929#endif
11930#ifdef FEAT_PATH_EXTRA
11931 "path_extra",
11932#endif
11933#ifdef FEAT_PERL
11934#ifndef DYNAMIC_PERL
11935 "perl",
11936#endif
11937#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020011938#ifdef FEAT_PERSISTENT_UNDO
11939 "persistent_undo",
11940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941#ifdef FEAT_PYTHON
11942#ifndef DYNAMIC_PYTHON
11943 "python",
11944#endif
11945#endif
11946#ifdef FEAT_POSTSCRIPT
11947 "postscript",
11948#endif
11949#ifdef FEAT_PRINTER
11950 "printer",
11951#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011952#ifdef FEAT_PROFILE
11953 "profile",
11954#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011955#ifdef FEAT_RELTIME
11956 "reltime",
11957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958#ifdef FEAT_QUICKFIX
11959 "quickfix",
11960#endif
11961#ifdef FEAT_RIGHTLEFT
11962 "rightleft",
11963#endif
11964#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11965 "ruby",
11966#endif
11967#ifdef FEAT_SCROLLBIND
11968 "scrollbind",
11969#endif
11970#ifdef FEAT_CMDL_INFO
11971 "showcmd",
11972 "cmdline_info",
11973#endif
11974#ifdef FEAT_SIGNS
11975 "signs",
11976#endif
11977#ifdef FEAT_SMARTINDENT
11978 "smartindent",
11979#endif
11980#ifdef FEAT_SNIFF
11981 "sniff",
11982#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000011983#ifdef STARTUPTIME
11984 "startuptime",
11985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986#ifdef FEAT_STL_OPT
11987 "statusline",
11988#endif
11989#ifdef FEAT_SUN_WORKSHOP
11990 "sun_workshop",
11991#endif
11992#ifdef FEAT_NETBEANS_INTG
11993 "netbeans_intg",
11994#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011995#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011996 "spell",
11997#endif
11998#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999 "syntax",
12000#endif
12001#if defined(USE_SYSTEM) || !defined(UNIX)
12002 "system",
12003#endif
12004#ifdef FEAT_TAG_BINS
12005 "tag_binary",
12006#endif
12007#ifdef FEAT_TAG_OLDSTATIC
12008 "tag_old_static",
12009#endif
12010#ifdef FEAT_TAG_ANYWHITE
12011 "tag_any_white",
12012#endif
12013#ifdef FEAT_TCL
12014# ifndef DYNAMIC_TCL
12015 "tcl",
12016# endif
12017#endif
12018#ifdef TERMINFO
12019 "terminfo",
12020#endif
12021#ifdef FEAT_TERMRESPONSE
12022 "termresponse",
12023#endif
12024#ifdef FEAT_TEXTOBJ
12025 "textobjects",
12026#endif
12027#ifdef HAVE_TGETENT
12028 "tgetent",
12029#endif
12030#ifdef FEAT_TITLE
12031 "title",
12032#endif
12033#ifdef FEAT_TOOLBAR
12034 "toolbar",
12035#endif
12036#ifdef FEAT_USR_CMDS
12037 "user-commands", /* was accidentally included in 5.4 */
12038 "user_commands",
12039#endif
12040#ifdef FEAT_VIMINFO
12041 "viminfo",
12042#endif
12043#ifdef FEAT_VERTSPLIT
12044 "vertsplit",
12045#endif
12046#ifdef FEAT_VIRTUALEDIT
12047 "virtualedit",
12048#endif
12049#ifdef FEAT_VISUAL
12050 "visual",
12051#endif
12052#ifdef FEAT_VISUALEXTRA
12053 "visualextra",
12054#endif
12055#ifdef FEAT_VREPLACE
12056 "vreplace",
12057#endif
12058#ifdef FEAT_WILDIGN
12059 "wildignore",
12060#endif
12061#ifdef FEAT_WILDMENU
12062 "wildmenu",
12063#endif
12064#ifdef FEAT_WINDOWS
12065 "windows",
12066#endif
12067#ifdef FEAT_WAK
12068 "winaltkeys",
12069#endif
12070#ifdef FEAT_WRITEBACKUP
12071 "writebackup",
12072#endif
12073#ifdef FEAT_XIM
12074 "xim",
12075#endif
12076#ifdef FEAT_XFONTSET
12077 "xfontset",
12078#endif
12079#ifdef USE_XSMP
12080 "xsmp",
12081#endif
12082#ifdef USE_XSMP_INTERACT
12083 "xsmp_interact",
12084#endif
12085#ifdef FEAT_XCLIPBOARD
12086 "xterm_clipboard",
12087#endif
12088#ifdef FEAT_XTERM_SAVE
12089 "xterm_save",
12090#endif
12091#if defined(UNIX) && defined(FEAT_X11)
12092 "X11",
12093#endif
12094 NULL
12095 };
12096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012097 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098 for (i = 0; has_list[i] != NULL; ++i)
12099 if (STRICMP(name, has_list[i]) == 0)
12100 {
12101 n = TRUE;
12102 break;
12103 }
12104
12105 if (n == FALSE)
12106 {
12107 if (STRNICMP(name, "patch", 5) == 0)
12108 n = has_patch(atoi((char *)name + 5));
12109 else if (STRICMP(name, "vim_starting") == 0)
12110 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012111#ifdef FEAT_MBYTE
12112 else if (STRICMP(name, "multi_byte_encoding") == 0)
12113 n = has_mbyte;
12114#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012115#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12116 else if (STRICMP(name, "balloon_multiline") == 0)
12117 n = multiline_balloon_available();
12118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119#ifdef DYNAMIC_TCL
12120 else if (STRICMP(name, "tcl") == 0)
12121 n = tcl_enabled(FALSE);
12122#endif
12123#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12124 else if (STRICMP(name, "iconv") == 0)
12125 n = iconv_enabled(FALSE);
12126#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012127#ifdef DYNAMIC_MZSCHEME
12128 else if (STRICMP(name, "mzscheme") == 0)
12129 n = mzscheme_enabled(FALSE);
12130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131#ifdef DYNAMIC_RUBY
12132 else if (STRICMP(name, "ruby") == 0)
12133 n = ruby_enabled(FALSE);
12134#endif
12135#ifdef DYNAMIC_PYTHON
12136 else if (STRICMP(name, "python") == 0)
12137 n = python_enabled(FALSE);
12138#endif
12139#ifdef DYNAMIC_PERL
12140 else if (STRICMP(name, "perl") == 0)
12141 n = perl_enabled(FALSE);
12142#endif
12143#ifdef FEAT_GUI
12144 else if (STRICMP(name, "gui_running") == 0)
12145 n = (gui.in_use || gui.starting);
12146# ifdef FEAT_GUI_W32
12147 else if (STRICMP(name, "gui_win32s") == 0)
12148 n = gui_is_win32s();
12149# endif
12150# ifdef FEAT_BROWSE
12151 else if (STRICMP(name, "browse") == 0)
12152 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12153# endif
12154#endif
12155#ifdef FEAT_SYN_HL
12156 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012157 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158#endif
12159#if defined(WIN3264)
12160 else if (STRICMP(name, "win95") == 0)
12161 n = mch_windows95();
12162#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012163#ifdef FEAT_NETBEANS_INTG
12164 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012165 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167 }
12168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012169 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170}
12171
12172/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012173 * "has_key()" function
12174 */
12175 static void
12176f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012177 typval_T *argvars;
12178 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012179{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012180 if (argvars[0].v_type != VAR_DICT)
12181 {
12182 EMSG(_(e_dictreq));
12183 return;
12184 }
12185 if (argvars[0].vval.v_dict == NULL)
12186 return;
12187
12188 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012189 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012190}
12191
12192/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012193 * "haslocaldir()" function
12194 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012195 static void
12196f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012197 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012198 typval_T *rettv;
12199{
12200 rettv->vval.v_number = (curwin->w_localdir != NULL);
12201}
12202
12203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204 * "hasmapto()" function
12205 */
12206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012207f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012208 typval_T *argvars;
12209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210{
12211 char_u *name;
12212 char_u *mode;
12213 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012214 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012216 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012217 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218 mode = (char_u *)"nvo";
12219 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012220 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012221 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012222 if (argvars[2].v_type != VAR_UNKNOWN)
12223 abbr = get_tv_number(&argvars[2]);
12224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225
Bram Moolenaar2c932302006-03-18 21:42:09 +000012226 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012227 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012229 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230}
12231
12232/*
12233 * "histadd()" function
12234 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012236f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012237 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239{
12240#ifdef FEAT_CMDHIST
12241 int histype;
12242 char_u *str;
12243 char_u buf[NUMBUFLEN];
12244#endif
12245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012246 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247 if (check_restricted() || check_secure())
12248 return;
12249#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012250 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12251 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252 if (histype >= 0)
12253 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012254 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255 if (*str != NUL)
12256 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012257 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012259 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260 return;
12261 }
12262 }
12263#endif
12264}
12265
12266/*
12267 * "histdel()" function
12268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012270f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012271 typval_T *argvars UNUSED;
12272 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273{
12274#ifdef FEAT_CMDHIST
12275 int n;
12276 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012277 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012279 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12280 if (str == NULL)
12281 n = 0;
12282 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012284 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012285 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012287 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012288 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289 else
12290 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012291 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012292 get_tv_string_buf(&argvars[1], buf));
12293 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294#endif
12295}
12296
12297/*
12298 * "histget()" function
12299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012301f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012302 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012303 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304{
12305#ifdef FEAT_CMDHIST
12306 int type;
12307 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012308 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012310 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12311 if (str == NULL)
12312 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012314 {
12315 type = get_histtype(str);
12316 if (argvars[1].v_type == VAR_UNKNOWN)
12317 idx = get_history_idx(type);
12318 else
12319 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12320 /* -1 on type error */
12321 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012324 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012326 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327}
12328
12329/*
12330 * "histnr()" function
12331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012333f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012334 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336{
12337 int i;
12338
12339#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012340 char_u *history = get_tv_string_chk(&argvars[0]);
12341
12342 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 if (i >= HIST_CMD && i < HIST_COUNT)
12344 i = get_history_idx(i);
12345 else
12346#endif
12347 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012348 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349}
12350
12351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352 * "highlightID(name)" function
12353 */
12354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012355f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012356 typval_T *argvars;
12357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012359 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360}
12361
12362/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012363 * "highlight_exists()" function
12364 */
12365 static void
12366f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012367 typval_T *argvars;
12368 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012369{
12370 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12371}
12372
12373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 * "hostname()" function
12375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012377f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012378 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380{
12381 char_u hostname[256];
12382
12383 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012384 rettv->v_type = VAR_STRING;
12385 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386}
12387
12388/*
12389 * iconv() function
12390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012392f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012393 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395{
12396#ifdef FEAT_MBYTE
12397 char_u buf1[NUMBUFLEN];
12398 char_u buf2[NUMBUFLEN];
12399 char_u *from, *to, *str;
12400 vimconv_T vimconv;
12401#endif
12402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012403 rettv->v_type = VAR_STRING;
12404 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405
12406#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012407 str = get_tv_string(&argvars[0]);
12408 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12409 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410 vimconv.vc_type = CONV_NONE;
12411 convert_setup(&vimconv, from, to);
12412
12413 /* If the encodings are equal, no conversion needed. */
12414 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012415 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012417 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418
12419 convert_setup(&vimconv, NULL, NULL);
12420 vim_free(from);
12421 vim_free(to);
12422#endif
12423}
12424
12425/*
12426 * "indent()" function
12427 */
12428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012429f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012430 typval_T *argvars;
12431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432{
12433 linenr_T lnum;
12434
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012435 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012437 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012439 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440}
12441
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012442/*
12443 * "index()" function
12444 */
12445 static void
12446f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012447 typval_T *argvars;
12448 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012449{
Bram Moolenaar33570922005-01-25 22:26:29 +000012450 list_T *l;
12451 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012452 long idx = 0;
12453 int ic = FALSE;
12454
12455 rettv->vval.v_number = -1;
12456 if (argvars[0].v_type != VAR_LIST)
12457 {
12458 EMSG(_(e_listreq));
12459 return;
12460 }
12461 l = argvars[0].vval.v_list;
12462 if (l != NULL)
12463 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012464 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012465 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012467 int error = FALSE;
12468
Bram Moolenaar758711c2005-02-02 23:11:38 +000012469 /* Start at specified item. Use the cached index that list_find()
12470 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012471 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012472 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012473 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012474 ic = get_tv_number_chk(&argvars[3], &error);
12475 if (error)
12476 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012477 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012478
Bram Moolenaar758711c2005-02-02 23:11:38 +000012479 for ( ; item != NULL; item = item->li_next, ++idx)
12480 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012481 {
12482 rettv->vval.v_number = idx;
12483 break;
12484 }
12485 }
12486}
12487
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488static int inputsecret_flag = 0;
12489
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012490static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12491
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012493 * This function is used by f_input() and f_inputdialog() functions. The third
12494 * argument to f_input() specifies the type of completion to use at the
12495 * prompt. The third argument to f_inputdialog() specifies the value to return
12496 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497 */
12498 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012499get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012500 typval_T *argvars;
12501 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012502 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012504 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505 char_u *p = NULL;
12506 int c;
12507 char_u buf[NUMBUFLEN];
12508 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012509 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012510 int xp_type = EXPAND_NOTHING;
12511 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012513 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012514 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515
12516#ifdef NO_CONSOLE_INPUT
12517 /* While starting up, there is no place to enter text. */
12518 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520#endif
12521
12522 cmd_silent = FALSE; /* Want to see the prompt. */
12523 if (prompt != NULL)
12524 {
12525 /* Only the part of the message after the last NL is considered as
12526 * prompt for the command line */
12527 p = vim_strrchr(prompt, '\n');
12528 if (p == NULL)
12529 p = prompt;
12530 else
12531 {
12532 ++p;
12533 c = *p;
12534 *p = NUL;
12535 msg_start();
12536 msg_clr_eos();
12537 msg_puts_attr(prompt, echo_attr);
12538 msg_didout = FALSE;
12539 msg_starthere();
12540 *p = c;
12541 }
12542 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012544 if (argvars[1].v_type != VAR_UNKNOWN)
12545 {
12546 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12547 if (defstr != NULL)
12548 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012550 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012551 {
12552 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012553 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012554 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012555
Bram Moolenaar4463f292005-09-25 22:20:24 +000012556 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012557
Bram Moolenaar4463f292005-09-25 22:20:24 +000012558 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12559 if (xp_name == NULL)
12560 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012561
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012562 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012563
Bram Moolenaar4463f292005-09-25 22:20:24 +000012564 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12565 &xp_arg) == FAIL)
12566 return;
12567 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012568 }
12569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012570 if (defstr != NULL)
12571 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012572 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12573 xp_type, xp_arg);
12574
12575 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012577 /* since the user typed this, no need to wait for return */
12578 need_wait_return = FALSE;
12579 msg_didout = FALSE;
12580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581 cmd_silent = cmd_silent_save;
12582}
12583
12584/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012585 * "input()" function
12586 * Also handles inputsecret() when inputsecret is set.
12587 */
12588 static void
12589f_input(argvars, rettv)
12590 typval_T *argvars;
12591 typval_T *rettv;
12592{
12593 get_user_input(argvars, rettv, FALSE);
12594}
12595
12596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597 * "inputdialog()" function
12598 */
12599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012600f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012601 typval_T *argvars;
12602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012603{
12604#if defined(FEAT_GUI_TEXTDIALOG)
12605 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12606 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12607 {
12608 char_u *message;
12609 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012610 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012612 message = get_tv_string_chk(&argvars[0]);
12613 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012614 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012615 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616 else
12617 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012618 if (message != NULL && defstr != NULL
12619 && do_dialog(VIM_QUESTION, NULL, message,
12620 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012621 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622 else
12623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012624 if (message != NULL && defstr != NULL
12625 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012626 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012627 rettv->vval.v_string = vim_strsave(
12628 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012630 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012632 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633 }
12634 else
12635#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012636 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637}
12638
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012639/*
12640 * "inputlist()" function
12641 */
12642 static void
12643f_inputlist(argvars, rettv)
12644 typval_T *argvars;
12645 typval_T *rettv;
12646{
12647 listitem_T *li;
12648 int selected;
12649 int mouse_used;
12650
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012651#ifdef NO_CONSOLE_INPUT
12652 /* While starting up, there is no place to enter text. */
12653 if (no_console_input())
12654 return;
12655#endif
12656 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12657 {
12658 EMSG2(_(e_listarg), "inputlist()");
12659 return;
12660 }
12661
12662 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012663 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012664 lines_left = Rows; /* avoid more prompt */
12665 msg_scroll = TRUE;
12666 msg_clr_eos();
12667
12668 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12669 {
12670 msg_puts(get_tv_string(&li->li_tv));
12671 msg_putchar('\n');
12672 }
12673
12674 /* Ask for choice. */
12675 selected = prompt_for_number(&mouse_used);
12676 if (mouse_used)
12677 selected -= lines_left;
12678
12679 rettv->vval.v_number = selected;
12680}
12681
12682
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12684
12685/*
12686 * "inputrestore()" function
12687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012689f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012690 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692{
12693 if (ga_userinput.ga_len > 0)
12694 {
12695 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12697 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012698 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699 }
12700 else if (p_verbose > 1)
12701 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012702 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012703 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704 }
12705}
12706
12707/*
12708 * "inputsave()" function
12709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012711f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012712 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012715 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716 if (ga_grow(&ga_userinput, 1) == OK)
12717 {
12718 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12719 + ga_userinput.ga_len);
12720 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012721 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722 }
12723 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012724 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725}
12726
12727/*
12728 * "inputsecret()" function
12729 */
12730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012731f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012732 typval_T *argvars;
12733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734{
12735 ++cmdline_star;
12736 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012737 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738 --cmdline_star;
12739 --inputsecret_flag;
12740}
12741
12742/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012743 * "insert()" function
12744 */
12745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012746f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012747 typval_T *argvars;
12748 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012749{
12750 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012751 listitem_T *item;
12752 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012753 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012754
12755 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012756 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012757 else if ((l = argvars[0].vval.v_list) != NULL
12758 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012759 {
12760 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012761 before = get_tv_number_chk(&argvars[2], &error);
12762 if (error)
12763 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012764
Bram Moolenaar758711c2005-02-02 23:11:38 +000012765 if (before == l->lv_len)
12766 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012767 else
12768 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012769 item = list_find(l, before);
12770 if (item == NULL)
12771 {
12772 EMSGN(_(e_listidx), before);
12773 l = NULL;
12774 }
12775 }
12776 if (l != NULL)
12777 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012778 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012779 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012780 }
12781 }
12782}
12783
12784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785 * "isdirectory()" function
12786 */
12787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012788f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012789 typval_T *argvars;
12790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012792 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793}
12794
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012795/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012796 * "islocked()" function
12797 */
12798 static void
12799f_islocked(argvars, rettv)
12800 typval_T *argvars;
12801 typval_T *rettv;
12802{
12803 lval_T lv;
12804 char_u *end;
12805 dictitem_T *di;
12806
12807 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012808 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12809 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012810 if (end != NULL && lv.ll_name != NULL)
12811 {
12812 if (*end != NUL)
12813 EMSG(_(e_trailing));
12814 else
12815 {
12816 if (lv.ll_tv == NULL)
12817 {
12818 if (check_changedtick(lv.ll_name))
12819 rettv->vval.v_number = 1; /* always locked */
12820 else
12821 {
12822 di = find_var(lv.ll_name, NULL);
12823 if (di != NULL)
12824 {
12825 /* Consider a variable locked when:
12826 * 1. the variable itself is locked
12827 * 2. the value of the variable is locked.
12828 * 3. the List or Dict value is locked.
12829 */
12830 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12831 || tv_islocked(&di->di_tv));
12832 }
12833 }
12834 }
12835 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012836 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012837 else if (lv.ll_newkey != NULL)
12838 EMSG2(_(e_dictkey), lv.ll_newkey);
12839 else if (lv.ll_list != NULL)
12840 /* List item. */
12841 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12842 else
12843 /* Dictionary item. */
12844 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12845 }
12846 }
12847
12848 clear_lval(&lv);
12849}
12850
Bram Moolenaar33570922005-01-25 22:26:29 +000012851static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012852
12853/*
12854 * Turn a dict into a list:
12855 * "what" == 0: list of keys
12856 * "what" == 1: list of values
12857 * "what" == 2: list of items
12858 */
12859 static void
12860dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012861 typval_T *argvars;
12862 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012863 int what;
12864{
Bram Moolenaar33570922005-01-25 22:26:29 +000012865 list_T *l2;
12866 dictitem_T *di;
12867 hashitem_T *hi;
12868 listitem_T *li;
12869 listitem_T *li2;
12870 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012871 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012872
Bram Moolenaar8c711452005-01-14 21:53:12 +000012873 if (argvars[0].v_type != VAR_DICT)
12874 {
12875 EMSG(_(e_dictreq));
12876 return;
12877 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012878 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012879 return;
12880
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012881 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012882 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012883
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012884 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012885 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012886 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012887 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012888 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012889 --todo;
12890 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012891
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012892 li = listitem_alloc();
12893 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012894 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012895 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012896
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012897 if (what == 0)
12898 {
12899 /* keys() */
12900 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012901 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012902 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12903 }
12904 else if (what == 1)
12905 {
12906 /* values() */
12907 copy_tv(&di->di_tv, &li->li_tv);
12908 }
12909 else
12910 {
12911 /* items() */
12912 l2 = list_alloc();
12913 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012914 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012915 li->li_tv.vval.v_list = l2;
12916 if (l2 == NULL)
12917 break;
12918 ++l2->lv_refcount;
12919
12920 li2 = listitem_alloc();
12921 if (li2 == NULL)
12922 break;
12923 list_append(l2, li2);
12924 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012925 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012926 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12927
12928 li2 = listitem_alloc();
12929 if (li2 == NULL)
12930 break;
12931 list_append(l2, li2);
12932 copy_tv(&di->di_tv, &li2->li_tv);
12933 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012934 }
12935 }
12936}
12937
12938/*
12939 * "items(dict)" function
12940 */
12941 static void
12942f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012943 typval_T *argvars;
12944 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012945{
12946 dict_list(argvars, rettv, 2);
12947}
12948
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012950 * "join()" function
12951 */
12952 static void
12953f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012954 typval_T *argvars;
12955 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012956{
12957 garray_T ga;
12958 char_u *sep;
12959
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012960 if (argvars[0].v_type != VAR_LIST)
12961 {
12962 EMSG(_(e_listreq));
12963 return;
12964 }
12965 if (argvars[0].vval.v_list == NULL)
12966 return;
12967 if (argvars[1].v_type == VAR_UNKNOWN)
12968 sep = (char_u *)" ";
12969 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012970 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012971
12972 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012973
12974 if (sep != NULL)
12975 {
12976 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012977 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012978 ga_append(&ga, NUL);
12979 rettv->vval.v_string = (char_u *)ga.ga_data;
12980 }
12981 else
12982 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012983}
12984
12985/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012986 * "keys()" function
12987 */
12988 static void
12989f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012990 typval_T *argvars;
12991 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012992{
12993 dict_list(argvars, rettv, 0);
12994}
12995
12996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997 * "last_buffer_nr()" function.
12998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013000f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013001 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003{
13004 int n = 0;
13005 buf_T *buf;
13006
13007 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13008 if (n < buf->b_fnum)
13009 n = buf->b_fnum;
13010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013011 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013012}
13013
13014/*
13015 * "len()" function
13016 */
13017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013018f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013019 typval_T *argvars;
13020 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013021{
13022 switch (argvars[0].v_type)
13023 {
13024 case VAR_STRING:
13025 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013026 rettv->vval.v_number = (varnumber_T)STRLEN(
13027 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013028 break;
13029 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013030 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013031 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013032 case VAR_DICT:
13033 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13034 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013035 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013036 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013037 break;
13038 }
13039}
13040
Bram Moolenaar33570922005-01-25 22:26:29 +000013041static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013042
13043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013044libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013045 typval_T *argvars;
13046 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013047 int type;
13048{
13049#ifdef FEAT_LIBCALL
13050 char_u *string_in;
13051 char_u **string_result;
13052 int nr_result;
13053#endif
13054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013055 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013056 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013057 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013058
13059 if (check_restricted() || check_secure())
13060 return;
13061
13062#ifdef FEAT_LIBCALL
13063 /* The first two args must be strings, otherwise its meaningless */
13064 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13065 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013066 string_in = NULL;
13067 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013068 string_in = argvars[2].vval.v_string;
13069 if (type == VAR_NUMBER)
13070 string_result = NULL;
13071 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013072 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013073 if (mch_libcall(argvars[0].vval.v_string,
13074 argvars[1].vval.v_string,
13075 string_in,
13076 argvars[2].vval.v_number,
13077 string_result,
13078 &nr_result) == OK
13079 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013080 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013081 }
13082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083}
13084
13085/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013086 * "libcall()" function
13087 */
13088 static void
13089f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013090 typval_T *argvars;
13091 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013092{
13093 libcall_common(argvars, rettv, VAR_STRING);
13094}
13095
13096/*
13097 * "libcallnr()" function
13098 */
13099 static void
13100f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013101 typval_T *argvars;
13102 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013103{
13104 libcall_common(argvars, rettv, VAR_NUMBER);
13105}
13106
13107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108 * "line(string)" function
13109 */
13110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013111f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013112 typval_T *argvars;
13113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114{
13115 linenr_T lnum = 0;
13116 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013117 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013119 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120 if (fp != NULL)
13121 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013122 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123}
13124
13125/*
13126 * "line2byte(lnum)" function
13127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013129f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013130 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132{
13133#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013134 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135#else
13136 linenr_T lnum;
13137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013138 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013140 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013142 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13143 if (rettv->vval.v_number >= 0)
13144 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013145#endif
13146}
13147
13148/*
13149 * "lispindent(lnum)" function
13150 */
13151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013152f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013153 typval_T *argvars;
13154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155{
13156#ifdef FEAT_LISP
13157 pos_T pos;
13158 linenr_T lnum;
13159
13160 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013161 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13163 {
13164 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013165 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 curwin->w_cursor = pos;
13167 }
13168 else
13169#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013170 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171}
13172
13173/*
13174 * "localtime()" function
13175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013177f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013178 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013180{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013181 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182}
13183
Bram Moolenaar33570922005-01-25 22:26:29 +000013184static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185
13186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013187get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013188 typval_T *argvars;
13189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190 int exact;
13191{
13192 char_u *keys;
13193 char_u *which;
13194 char_u buf[NUMBUFLEN];
13195 char_u *keys_buf = NULL;
13196 char_u *rhs;
13197 int mode;
13198 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013199 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013200
13201 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013202 rettv->v_type = VAR_STRING;
13203 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013205 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206 if (*keys == NUL)
13207 return;
13208
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013209 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013211 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013212 if (argvars[2].v_type != VAR_UNKNOWN)
13213 abbr = get_tv_number(&argvars[2]);
13214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215 else
13216 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013217 if (which == NULL)
13218 return;
13219
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220 mode = get_map_mode(&which, 0);
13221
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013222 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013223 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224 vim_free(keys_buf);
13225 if (rhs != NULL)
13226 {
13227 ga_init(&ga);
13228 ga.ga_itemsize = 1;
13229 ga.ga_growsize = 40;
13230
13231 while (*rhs != NUL)
13232 ga_concat(&ga, str2special(&rhs, FALSE));
13233
13234 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013235 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236 }
13237}
13238
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013239#ifdef FEAT_FLOAT
13240/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013241 * "log()" function
13242 */
13243 static void
13244f_log(argvars, rettv)
13245 typval_T *argvars;
13246 typval_T *rettv;
13247{
13248 float_T f;
13249
13250 rettv->v_type = VAR_FLOAT;
13251 if (get_float_arg(argvars, &f) == OK)
13252 rettv->vval.v_float = log(f);
13253 else
13254 rettv->vval.v_float = 0.0;
13255}
13256
13257/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013258 * "log10()" function
13259 */
13260 static void
13261f_log10(argvars, rettv)
13262 typval_T *argvars;
13263 typval_T *rettv;
13264{
13265 float_T f;
13266
13267 rettv->v_type = VAR_FLOAT;
13268 if (get_float_arg(argvars, &f) == OK)
13269 rettv->vval.v_float = log10(f);
13270 else
13271 rettv->vval.v_float = 0.0;
13272}
13273#endif
13274
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013276 * "map()" function
13277 */
13278 static void
13279f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013280 typval_T *argvars;
13281 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013282{
13283 filter_map(argvars, rettv, TRUE);
13284}
13285
13286/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013287 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288 */
13289 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013290f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013291 typval_T *argvars;
13292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013294 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295}
13296
13297/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013298 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299 */
13300 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013301f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013302 typval_T *argvars;
13303 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013305 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306}
13307
Bram Moolenaar33570922005-01-25 22:26:29 +000013308static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309
13310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013311find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013312 typval_T *argvars;
13313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314 int type;
13315{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013316 char_u *str = NULL;
13317 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318 char_u *pat;
13319 regmatch_T regmatch;
13320 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013321 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322 char_u *save_cpo;
13323 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013324 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013325 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013326 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013327 list_T *l = NULL;
13328 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013329 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013330 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331
13332 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13333 save_cpo = p_cpo;
13334 p_cpo = (char_u *)"";
13335
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013336 rettv->vval.v_number = -1;
13337 if (type == 3)
13338 {
13339 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013340 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013341 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013342 }
13343 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013345 rettv->v_type = VAR_STRING;
13346 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013349 if (argvars[0].v_type == VAR_LIST)
13350 {
13351 if ((l = argvars[0].vval.v_list) == NULL)
13352 goto theend;
13353 li = l->lv_first;
13354 }
13355 else
13356 expr = str = get_tv_string(&argvars[0]);
13357
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013358 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13359 if (pat == NULL)
13360 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013361
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013362 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013364 int error = FALSE;
13365
13366 start = get_tv_number_chk(&argvars[2], &error);
13367 if (error)
13368 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013369 if (l != NULL)
13370 {
13371 li = list_find(l, start);
13372 if (li == NULL)
13373 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013374 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013375 }
13376 else
13377 {
13378 if (start < 0)
13379 start = 0;
13380 if (start > (long)STRLEN(str))
13381 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013382 /* When "count" argument is there ignore matches before "start",
13383 * otherwise skip part of the string. Differs when pattern is "^"
13384 * or "\<". */
13385 if (argvars[3].v_type != VAR_UNKNOWN)
13386 startcol = start;
13387 else
13388 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013389 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013390
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013391 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013392 nth = get_tv_number_chk(&argvars[3], &error);
13393 if (error)
13394 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395 }
13396
13397 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13398 if (regmatch.regprog != NULL)
13399 {
13400 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013401
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013402 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013403 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013404 if (l != NULL)
13405 {
13406 if (li == NULL)
13407 {
13408 match = FALSE;
13409 break;
13410 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013411 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013412 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013413 if (str == NULL)
13414 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013415 }
13416
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013417 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013418
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013419 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013420 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013421 if (l == NULL && !match)
13422 break;
13423
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013424 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013425 if (l != NULL)
13426 {
13427 li = li->li_next;
13428 ++idx;
13429 }
13430 else
13431 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013432#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013433 startcol = (colnr_T)(regmatch.startp[0]
13434 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013435#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013436 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013437#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013438 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013439 }
13440
13441 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013443 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013444 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013445 int i;
13446
13447 /* return list with matched string and submatches */
13448 for (i = 0; i < NSUBEXP; ++i)
13449 {
13450 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013451 {
13452 if (list_append_string(rettv->vval.v_list,
13453 (char_u *)"", 0) == FAIL)
13454 break;
13455 }
13456 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013457 regmatch.startp[i],
13458 (int)(regmatch.endp[i] - regmatch.startp[i]))
13459 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013460 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013461 }
13462 }
13463 else if (type == 2)
13464 {
13465 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013466 if (l != NULL)
13467 copy_tv(&li->li_tv, rettv);
13468 else
13469 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013471 }
13472 else if (l != NULL)
13473 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474 else
13475 {
13476 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013477 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 (varnumber_T)(regmatch.startp[0] - str);
13479 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013480 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013482 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483 }
13484 }
13485 vim_free(regmatch.regprog);
13486 }
13487
13488theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013489 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490 p_cpo = save_cpo;
13491}
13492
13493/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013494 * "match()" function
13495 */
13496 static void
13497f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013498 typval_T *argvars;
13499 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013500{
13501 find_some_match(argvars, rettv, 1);
13502}
13503
13504/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013505 * "matchadd()" function
13506 */
13507 static void
13508f_matchadd(argvars, rettv)
13509 typval_T *argvars;
13510 typval_T *rettv;
13511{
13512#ifdef FEAT_SEARCH_EXTRA
13513 char_u buf[NUMBUFLEN];
13514 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13515 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13516 int prio = 10; /* default priority */
13517 int id = -1;
13518 int error = FALSE;
13519
13520 rettv->vval.v_number = -1;
13521
13522 if (grp == NULL || pat == NULL)
13523 return;
13524 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013525 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013526 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013527 if (argvars[3].v_type != VAR_UNKNOWN)
13528 id = get_tv_number_chk(&argvars[3], &error);
13529 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013530 if (error == TRUE)
13531 return;
13532 if (id >= 1 && id <= 3)
13533 {
13534 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13535 return;
13536 }
13537
13538 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13539#endif
13540}
13541
13542/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013543 * "matcharg()" function
13544 */
13545 static void
13546f_matcharg(argvars, rettv)
13547 typval_T *argvars;
13548 typval_T *rettv;
13549{
13550 if (rettv_list_alloc(rettv) == OK)
13551 {
13552#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013553 int id = get_tv_number(&argvars[0]);
13554 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013555
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013556 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013557 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013558 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13559 {
13560 list_append_string(rettv->vval.v_list,
13561 syn_id2name(m->hlg_id), -1);
13562 list_append_string(rettv->vval.v_list, m->pattern, -1);
13563 }
13564 else
13565 {
13566 list_append_string(rettv->vval.v_list, NUL, -1);
13567 list_append_string(rettv->vval.v_list, NUL, -1);
13568 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013569 }
13570#endif
13571 }
13572}
13573
13574/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013575 * "matchdelete()" function
13576 */
13577 static void
13578f_matchdelete(argvars, rettv)
13579 typval_T *argvars;
13580 typval_T *rettv;
13581{
13582#ifdef FEAT_SEARCH_EXTRA
13583 rettv->vval.v_number = match_delete(curwin,
13584 (int)get_tv_number(&argvars[0]), TRUE);
13585#endif
13586}
13587
13588/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013589 * "matchend()" function
13590 */
13591 static void
13592f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013593 typval_T *argvars;
13594 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013595{
13596 find_some_match(argvars, rettv, 0);
13597}
13598
13599/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013600 * "matchlist()" function
13601 */
13602 static void
13603f_matchlist(argvars, rettv)
13604 typval_T *argvars;
13605 typval_T *rettv;
13606{
13607 find_some_match(argvars, rettv, 3);
13608}
13609
13610/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013611 * "matchstr()" function
13612 */
13613 static void
13614f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013615 typval_T *argvars;
13616 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013617{
13618 find_some_match(argvars, rettv, 2);
13619}
13620
Bram Moolenaar33570922005-01-25 22:26:29 +000013621static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013622
13623 static void
13624max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013625 typval_T *argvars;
13626 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013627 int domax;
13628{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013629 long n = 0;
13630 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013631 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013632
13633 if (argvars[0].v_type == VAR_LIST)
13634 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013635 list_T *l;
13636 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013637
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013638 l = argvars[0].vval.v_list;
13639 if (l != NULL)
13640 {
13641 li = l->lv_first;
13642 if (li != NULL)
13643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013644 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013645 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013646 {
13647 li = li->li_next;
13648 if (li == NULL)
13649 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013650 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013651 if (domax ? i > n : i < n)
13652 n = i;
13653 }
13654 }
13655 }
13656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013657 else if (argvars[0].v_type == VAR_DICT)
13658 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013659 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013660 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013661 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013662 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013663
13664 d = argvars[0].vval.v_dict;
13665 if (d != NULL)
13666 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013667 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013668 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013669 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013670 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013671 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013672 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013673 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013674 if (first)
13675 {
13676 n = i;
13677 first = FALSE;
13678 }
13679 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013680 n = i;
13681 }
13682 }
13683 }
13684 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013685 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013686 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013687 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013688}
13689
13690/*
13691 * "max()" function
13692 */
13693 static void
13694f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013695 typval_T *argvars;
13696 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013697{
13698 max_min(argvars, rettv, TRUE);
13699}
13700
13701/*
13702 * "min()" function
13703 */
13704 static void
13705f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013706 typval_T *argvars;
13707 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013708{
13709 max_min(argvars, rettv, FALSE);
13710}
13711
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013712static int mkdir_recurse __ARGS((char_u *dir, int prot));
13713
13714/*
13715 * Create the directory in which "dir" is located, and higher levels when
13716 * needed.
13717 */
13718 static int
13719mkdir_recurse(dir, prot)
13720 char_u *dir;
13721 int prot;
13722{
13723 char_u *p;
13724 char_u *updir;
13725 int r = FAIL;
13726
13727 /* Get end of directory name in "dir".
13728 * We're done when it's "/" or "c:/". */
13729 p = gettail_sep(dir);
13730 if (p <= get_past_head(dir))
13731 return OK;
13732
13733 /* If the directory exists we're done. Otherwise: create it.*/
13734 updir = vim_strnsave(dir, (int)(p - dir));
13735 if (updir == NULL)
13736 return FAIL;
13737 if (mch_isdir(updir))
13738 r = OK;
13739 else if (mkdir_recurse(updir, prot) == OK)
13740 r = vim_mkdir_emsg(updir, prot);
13741 vim_free(updir);
13742 return r;
13743}
13744
13745#ifdef vim_mkdir
13746/*
13747 * "mkdir()" function
13748 */
13749 static void
13750f_mkdir(argvars, rettv)
13751 typval_T *argvars;
13752 typval_T *rettv;
13753{
13754 char_u *dir;
13755 char_u buf[NUMBUFLEN];
13756 int prot = 0755;
13757
13758 rettv->vval.v_number = FAIL;
13759 if (check_restricted() || check_secure())
13760 return;
13761
13762 dir = get_tv_string_buf(&argvars[0], buf);
13763 if (argvars[1].v_type != VAR_UNKNOWN)
13764 {
13765 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013766 prot = get_tv_number_chk(&argvars[2], NULL);
13767 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013768 mkdir_recurse(dir, prot);
13769 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013770 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013771}
13772#endif
13773
Bram Moolenaar0d660222005-01-07 21:51:51 +000013774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775 * "mode()" function
13776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013778f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013779 typval_T *argvars;
13780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013782 char_u buf[3];
13783
13784 buf[1] = NUL;
13785 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786
13787#ifdef FEAT_VISUAL
13788 if (VIsual_active)
13789 {
13790 if (VIsual_select)
13791 buf[0] = VIsual_mode + 's' - 'v';
13792 else
13793 buf[0] = VIsual_mode;
13794 }
13795 else
13796#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013797 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13798 || State == CONFIRM)
13799 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013801 if (State == ASKMORE)
13802 buf[1] = 'm';
13803 else if (State == CONFIRM)
13804 buf[1] = '?';
13805 }
13806 else if (State == EXTERNCMD)
13807 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808 else if (State & INSERT)
13809 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013810#ifdef FEAT_VREPLACE
13811 if (State & VREPLACE_FLAG)
13812 {
13813 buf[0] = 'R';
13814 buf[1] = 'v';
13815 }
13816 else
13817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818 if (State & REPLACE_FLAG)
13819 buf[0] = 'R';
13820 else
13821 buf[0] = 'i';
13822 }
13823 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013824 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013826 if (exmode_active)
13827 buf[1] = 'v';
13828 }
13829 else if (exmode_active)
13830 {
13831 buf[0] = 'c';
13832 buf[1] = 'e';
13833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013837 if (finish_op)
13838 buf[1] = 'o';
13839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013841 /* Clear out the minor mode when the argument is not a non-zero number or
13842 * non-empty string. */
13843 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013844 buf[1] = NUL;
13845
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013846 rettv->vval.v_string = vim_strsave(buf);
13847 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848}
13849
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013850#ifdef FEAT_MZSCHEME
13851/*
13852 * "mzeval()" function
13853 */
13854 static void
13855f_mzeval(argvars, rettv)
13856 typval_T *argvars;
13857 typval_T *rettv;
13858{
13859 char_u *str;
13860 char_u buf[NUMBUFLEN];
13861
13862 str = get_tv_string_buf(&argvars[0], buf);
13863 do_mzeval(str, rettv);
13864}
13865#endif
13866
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013868 * "nextnonblank()" function
13869 */
13870 static void
13871f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013872 typval_T *argvars;
13873 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013874{
13875 linenr_T lnum;
13876
13877 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013879 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013880 {
13881 lnum = 0;
13882 break;
13883 }
13884 if (*skipwhite(ml_get(lnum)) != NUL)
13885 break;
13886 }
13887 rettv->vval.v_number = lnum;
13888}
13889
13890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891 * "nr2char()" function
13892 */
13893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013894f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013895 typval_T *argvars;
13896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897{
13898 char_u buf[NUMBUFLEN];
13899
13900#ifdef FEAT_MBYTE
13901 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013902 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903 else
13904#endif
13905 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013906 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907 buf[1] = NUL;
13908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013909 rettv->v_type = VAR_STRING;
13910 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013911}
13912
13913/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013914 * "pathshorten()" function
13915 */
13916 static void
13917f_pathshorten(argvars, rettv)
13918 typval_T *argvars;
13919 typval_T *rettv;
13920{
13921 char_u *p;
13922
13923 rettv->v_type = VAR_STRING;
13924 p = get_tv_string_chk(&argvars[0]);
13925 if (p == NULL)
13926 rettv->vval.v_string = NULL;
13927 else
13928 {
13929 p = vim_strsave(p);
13930 rettv->vval.v_string = p;
13931 if (p != NULL)
13932 shorten_dir(p);
13933 }
13934}
13935
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013936#ifdef FEAT_FLOAT
13937/*
13938 * "pow()" function
13939 */
13940 static void
13941f_pow(argvars, rettv)
13942 typval_T *argvars;
13943 typval_T *rettv;
13944{
13945 float_T fx, fy;
13946
13947 rettv->v_type = VAR_FLOAT;
13948 if (get_float_arg(argvars, &fx) == OK
13949 && get_float_arg(&argvars[1], &fy) == OK)
13950 rettv->vval.v_float = pow(fx, fy);
13951 else
13952 rettv->vval.v_float = 0.0;
13953}
13954#endif
13955
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013956/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013957 * "prevnonblank()" function
13958 */
13959 static void
13960f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013961 typval_T *argvars;
13962 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013963{
13964 linenr_T lnum;
13965
13966 lnum = get_tv_lnum(argvars);
13967 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13968 lnum = 0;
13969 else
13970 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13971 --lnum;
13972 rettv->vval.v_number = lnum;
13973}
13974
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013975#ifdef HAVE_STDARG_H
13976/* This dummy va_list is here because:
13977 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13978 * - locally in the function results in a "used before set" warning
13979 * - using va_start() to initialize it gives "function with fixed args" error */
13980static va_list ap;
13981#endif
13982
Bram Moolenaar8c711452005-01-14 21:53:12 +000013983/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013984 * "printf()" function
13985 */
13986 static void
13987f_printf(argvars, rettv)
13988 typval_T *argvars;
13989 typval_T *rettv;
13990{
13991 rettv->v_type = VAR_STRING;
13992 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013993#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013994 {
13995 char_u buf[NUMBUFLEN];
13996 int len;
13997 char_u *s;
13998 int saved_did_emsg = did_emsg;
13999 char *fmt;
14000
14001 /* Get the required length, allocate the buffer and do it for real. */
14002 did_emsg = FALSE;
14003 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014004 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014005 if (!did_emsg)
14006 {
14007 s = alloc(len + 1);
14008 if (s != NULL)
14009 {
14010 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014011 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014012 }
14013 }
14014 did_emsg |= saved_did_emsg;
14015 }
14016#endif
14017}
14018
14019/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014020 * "pumvisible()" function
14021 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014022 static void
14023f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014024 typval_T *argvars UNUSED;
14025 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014026{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014027#ifdef FEAT_INS_EXPAND
14028 if (pum_visible())
14029 rettv->vval.v_number = 1;
14030#endif
14031}
14032
14033/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014034 * "range()" function
14035 */
14036 static void
14037f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014038 typval_T *argvars;
14039 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014040{
14041 long start;
14042 long end;
14043 long stride = 1;
14044 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014045 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014047 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014048 if (argvars[1].v_type == VAR_UNKNOWN)
14049 {
14050 end = start - 1;
14051 start = 0;
14052 }
14053 else
14054 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014055 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014056 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014057 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014058 }
14059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014060 if (error)
14061 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014062 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014063 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014064 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014065 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014066 else
14067 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014068 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014069 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014070 if (list_append_number(rettv->vval.v_list,
14071 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014072 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014073 }
14074}
14075
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014076/*
14077 * "readfile()" function
14078 */
14079 static void
14080f_readfile(argvars, rettv)
14081 typval_T *argvars;
14082 typval_T *rettv;
14083{
14084 int binary = FALSE;
14085 char_u *fname;
14086 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014087 listitem_T *li;
14088#define FREAD_SIZE 200 /* optimized for text lines */
14089 char_u buf[FREAD_SIZE];
14090 int readlen; /* size of last fread() */
14091 int buflen; /* nr of valid chars in buf[] */
14092 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14093 int tolist; /* first byte in buf[] still to be put in list */
14094 int chop; /* how many CR to chop off */
14095 char_u *prev = NULL; /* previously read bytes, if any */
14096 int prevlen = 0; /* length of "prev" if not NULL */
14097 char_u *s;
14098 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014099 long maxline = MAXLNUM;
14100 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014101
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014102 if (argvars[1].v_type != VAR_UNKNOWN)
14103 {
14104 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14105 binary = TRUE;
14106 if (argvars[2].v_type != VAR_UNKNOWN)
14107 maxline = get_tv_number(&argvars[2]);
14108 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014109
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014110 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014111 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014112
14113 /* Always open the file in binary mode, library functions have a mind of
14114 * their own about CR-LF conversion. */
14115 fname = get_tv_string(&argvars[0]);
14116 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14117 {
14118 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14119 return;
14120 }
14121
14122 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014123 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014124 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014125 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014126 buflen = filtd + readlen;
14127 tolist = 0;
14128 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14129 {
14130 if (buf[filtd] == '\n' || readlen <= 0)
14131 {
14132 /* Only when in binary mode add an empty list item when the
14133 * last line ends in a '\n'. */
14134 if (!binary && readlen == 0 && filtd == 0)
14135 break;
14136
14137 /* Found end-of-line or end-of-file: add a text line to the
14138 * list. */
14139 chop = 0;
14140 if (!binary)
14141 while (filtd - chop - 1 >= tolist
14142 && buf[filtd - chop - 1] == '\r')
14143 ++chop;
14144 len = filtd - tolist - chop;
14145 if (prev == NULL)
14146 s = vim_strnsave(buf + tolist, len);
14147 else
14148 {
14149 s = alloc((unsigned)(prevlen + len + 1));
14150 if (s != NULL)
14151 {
14152 mch_memmove(s, prev, prevlen);
14153 vim_free(prev);
14154 prev = NULL;
14155 mch_memmove(s + prevlen, buf + tolist, len);
14156 s[prevlen + len] = NUL;
14157 }
14158 }
14159 tolist = filtd + 1;
14160
14161 li = listitem_alloc();
14162 if (li == NULL)
14163 {
14164 vim_free(s);
14165 break;
14166 }
14167 li->li_tv.v_type = VAR_STRING;
14168 li->li_tv.v_lock = 0;
14169 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014170 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014171
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014172 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014173 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014174 if (readlen <= 0)
14175 break;
14176 }
14177 else if (buf[filtd] == NUL)
14178 buf[filtd] = '\n';
14179 }
14180 if (readlen <= 0)
14181 break;
14182
14183 if (tolist == 0)
14184 {
14185 /* "buf" is full, need to move text to an allocated buffer */
14186 if (prev == NULL)
14187 {
14188 prev = vim_strnsave(buf, buflen);
14189 prevlen = buflen;
14190 }
14191 else
14192 {
14193 s = alloc((unsigned)(prevlen + buflen));
14194 if (s != NULL)
14195 {
14196 mch_memmove(s, prev, prevlen);
14197 mch_memmove(s + prevlen, buf, buflen);
14198 vim_free(prev);
14199 prev = s;
14200 prevlen += buflen;
14201 }
14202 }
14203 filtd = 0;
14204 }
14205 else
14206 {
14207 mch_memmove(buf, buf + tolist, buflen - tolist);
14208 filtd -= tolist;
14209 }
14210 }
14211
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014212 /*
14213 * For a negative line count use only the lines at the end of the file,
14214 * free the rest.
14215 */
14216 if (maxline < 0)
14217 while (cnt > -maxline)
14218 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014219 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014220 --cnt;
14221 }
14222
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014223 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014224 fclose(fd);
14225}
14226
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014227#if defined(FEAT_RELTIME)
14228static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14229
14230/*
14231 * Convert a List to proftime_T.
14232 * Return FAIL when there is something wrong.
14233 */
14234 static int
14235list2proftime(arg, tm)
14236 typval_T *arg;
14237 proftime_T *tm;
14238{
14239 long n1, n2;
14240 int error = FALSE;
14241
14242 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14243 || arg->vval.v_list->lv_len != 2)
14244 return FAIL;
14245 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14246 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14247# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014248 tm->HighPart = n1;
14249 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014250# else
14251 tm->tv_sec = n1;
14252 tm->tv_usec = n2;
14253# endif
14254 return error ? FAIL : OK;
14255}
14256#endif /* FEAT_RELTIME */
14257
14258/*
14259 * "reltime()" function
14260 */
14261 static void
14262f_reltime(argvars, rettv)
14263 typval_T *argvars;
14264 typval_T *rettv;
14265{
14266#ifdef FEAT_RELTIME
14267 proftime_T res;
14268 proftime_T start;
14269
14270 if (argvars[0].v_type == VAR_UNKNOWN)
14271 {
14272 /* No arguments: get current time. */
14273 profile_start(&res);
14274 }
14275 else if (argvars[1].v_type == VAR_UNKNOWN)
14276 {
14277 if (list2proftime(&argvars[0], &res) == FAIL)
14278 return;
14279 profile_end(&res);
14280 }
14281 else
14282 {
14283 /* Two arguments: compute the difference. */
14284 if (list2proftime(&argvars[0], &start) == FAIL
14285 || list2proftime(&argvars[1], &res) == FAIL)
14286 return;
14287 profile_sub(&res, &start);
14288 }
14289
14290 if (rettv_list_alloc(rettv) == OK)
14291 {
14292 long n1, n2;
14293
14294# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014295 n1 = res.HighPart;
14296 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014297# else
14298 n1 = res.tv_sec;
14299 n2 = res.tv_usec;
14300# endif
14301 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14302 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14303 }
14304#endif
14305}
14306
14307/*
14308 * "reltimestr()" function
14309 */
14310 static void
14311f_reltimestr(argvars, rettv)
14312 typval_T *argvars;
14313 typval_T *rettv;
14314{
14315#ifdef FEAT_RELTIME
14316 proftime_T tm;
14317#endif
14318
14319 rettv->v_type = VAR_STRING;
14320 rettv->vval.v_string = NULL;
14321#ifdef FEAT_RELTIME
14322 if (list2proftime(&argvars[0], &tm) == OK)
14323 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14324#endif
14325}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014326
Bram Moolenaar0d660222005-01-07 21:51:51 +000014327#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14328static void make_connection __ARGS((void));
14329static int check_connection __ARGS((void));
14330
14331 static void
14332make_connection()
14333{
14334 if (X_DISPLAY == NULL
14335# ifdef FEAT_GUI
14336 && !gui.in_use
14337# endif
14338 )
14339 {
14340 x_force_connect = TRUE;
14341 setup_term_clip();
14342 x_force_connect = FALSE;
14343 }
14344}
14345
14346 static int
14347check_connection()
14348{
14349 make_connection();
14350 if (X_DISPLAY == NULL)
14351 {
14352 EMSG(_("E240: No connection to Vim server"));
14353 return FAIL;
14354 }
14355 return OK;
14356}
14357#endif
14358
14359#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014360static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014361
14362 static void
14363remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014364 typval_T *argvars;
14365 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014366 int expr;
14367{
14368 char_u *server_name;
14369 char_u *keys;
14370 char_u *r = NULL;
14371 char_u buf[NUMBUFLEN];
14372# ifdef WIN32
14373 HWND w;
14374# else
14375 Window w;
14376# endif
14377
14378 if (check_restricted() || check_secure())
14379 return;
14380
14381# ifdef FEAT_X11
14382 if (check_connection() == FAIL)
14383 return;
14384# endif
14385
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014386 server_name = get_tv_string_chk(&argvars[0]);
14387 if (server_name == NULL)
14388 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014389 keys = get_tv_string_buf(&argvars[1], buf);
14390# ifdef WIN32
14391 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14392# else
14393 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14394 < 0)
14395# endif
14396 {
14397 if (r != NULL)
14398 EMSG(r); /* sending worked but evaluation failed */
14399 else
14400 EMSG2(_("E241: Unable to send to %s"), server_name);
14401 return;
14402 }
14403
14404 rettv->vval.v_string = r;
14405
14406 if (argvars[2].v_type != VAR_UNKNOWN)
14407 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014408 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014409 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014410 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014411
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014412 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014413 v.di_tv.v_type = VAR_STRING;
14414 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014415 idvar = get_tv_string_chk(&argvars[2]);
14416 if (idvar != NULL)
14417 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014418 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014419 }
14420}
14421#endif
14422
14423/*
14424 * "remote_expr()" function
14425 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014426 static void
14427f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014428 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014429 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014430{
14431 rettv->v_type = VAR_STRING;
14432 rettv->vval.v_string = NULL;
14433#ifdef FEAT_CLIENTSERVER
14434 remote_common(argvars, rettv, TRUE);
14435#endif
14436}
14437
14438/*
14439 * "remote_foreground()" function
14440 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014441 static void
14442f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014443 typval_T *argvars UNUSED;
14444 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014445{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014446#ifdef FEAT_CLIENTSERVER
14447# ifdef WIN32
14448 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014449 {
14450 char_u *server_name = get_tv_string_chk(&argvars[0]);
14451
14452 if (server_name != NULL)
14453 serverForeground(server_name);
14454 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014455# else
14456 /* Send a foreground() expression to the server. */
14457 argvars[1].v_type = VAR_STRING;
14458 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14459 argvars[2].v_type = VAR_UNKNOWN;
14460 remote_common(argvars, rettv, TRUE);
14461 vim_free(argvars[1].vval.v_string);
14462# endif
14463#endif
14464}
14465
Bram Moolenaar0d660222005-01-07 21:51:51 +000014466 static void
14467f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014468 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014469 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014470{
14471#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014472 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014473 char_u *s = NULL;
14474# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014475 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014476# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014477 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014478
14479 if (check_restricted() || check_secure())
14480 {
14481 rettv->vval.v_number = -1;
14482 return;
14483 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014484 serverid = get_tv_string_chk(&argvars[0]);
14485 if (serverid == NULL)
14486 {
14487 rettv->vval.v_number = -1;
14488 return; /* type error; errmsg already given */
14489 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014490# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014491 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014492 if (n == 0)
14493 rettv->vval.v_number = -1;
14494 else
14495 {
14496 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14497 rettv->vval.v_number = (s != NULL);
14498 }
14499# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014500 if (check_connection() == FAIL)
14501 return;
14502
14503 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014504 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014505# endif
14506
14507 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014509 char_u *retvar;
14510
Bram Moolenaar33570922005-01-25 22:26:29 +000014511 v.di_tv.v_type = VAR_STRING;
14512 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014513 retvar = get_tv_string_chk(&argvars[1]);
14514 if (retvar != NULL)
14515 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014516 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014517 }
14518#else
14519 rettv->vval.v_number = -1;
14520#endif
14521}
14522
Bram Moolenaar0d660222005-01-07 21:51:51 +000014523 static void
14524f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014525 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014526 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014527{
14528 char_u *r = NULL;
14529
14530#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014531 char_u *serverid = get_tv_string_chk(&argvars[0]);
14532
14533 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014534 {
14535# ifdef WIN32
14536 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014537 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014538
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014539 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014540 if (n != 0)
14541 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14542 if (r == NULL)
14543# else
14544 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014545 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014546# endif
14547 EMSG(_("E277: Unable to read a server reply"));
14548 }
14549#endif
14550 rettv->v_type = VAR_STRING;
14551 rettv->vval.v_string = r;
14552}
14553
14554/*
14555 * "remote_send()" function
14556 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014557 static void
14558f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014559 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014560 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014561{
14562 rettv->v_type = VAR_STRING;
14563 rettv->vval.v_string = NULL;
14564#ifdef FEAT_CLIENTSERVER
14565 remote_common(argvars, rettv, FALSE);
14566#endif
14567}
14568
14569/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014570 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014571 */
14572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014573f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014574 typval_T *argvars;
14575 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014576{
Bram Moolenaar33570922005-01-25 22:26:29 +000014577 list_T *l;
14578 listitem_T *item, *item2;
14579 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014580 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014581 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014582 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014583 dict_T *d;
14584 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014585
Bram Moolenaar8c711452005-01-14 21:53:12 +000014586 if (argvars[0].v_type == VAR_DICT)
14587 {
14588 if (argvars[2].v_type != VAR_UNKNOWN)
14589 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014590 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014591 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014592 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014593 key = get_tv_string_chk(&argvars[1]);
14594 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014595 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014596 di = dict_find(d, key, -1);
14597 if (di == NULL)
14598 EMSG2(_(e_dictkey), key);
14599 else
14600 {
14601 *rettv = di->di_tv;
14602 init_tv(&di->di_tv);
14603 dictitem_remove(d, di);
14604 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014605 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014606 }
14607 }
14608 else if (argvars[0].v_type != VAR_LIST)
14609 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014610 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014611 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014613 int error = FALSE;
14614
14615 idx = get_tv_number_chk(&argvars[1], &error);
14616 if (error)
14617 ; /* type error: do nothing, errmsg already given */
14618 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014619 EMSGN(_(e_listidx), idx);
14620 else
14621 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014622 if (argvars[2].v_type == VAR_UNKNOWN)
14623 {
14624 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014625 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014626 *rettv = item->li_tv;
14627 vim_free(item);
14628 }
14629 else
14630 {
14631 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014632 end = get_tv_number_chk(&argvars[2], &error);
14633 if (error)
14634 ; /* type error: do nothing */
14635 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014636 EMSGN(_(e_listidx), end);
14637 else
14638 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014639 int cnt = 0;
14640
14641 for (li = item; li != NULL; li = li->li_next)
14642 {
14643 ++cnt;
14644 if (li == item2)
14645 break;
14646 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014647 if (li == NULL) /* didn't find "item2" after "item" */
14648 EMSG(_(e_invrange));
14649 else
14650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014651 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014652 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014653 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014654 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014655 l->lv_first = item;
14656 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014657 item->li_prev = NULL;
14658 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014659 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014660 }
14661 }
14662 }
14663 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014664 }
14665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666}
14667
14668/*
14669 * "rename({from}, {to})" function
14670 */
14671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014672f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014673 typval_T *argvars;
14674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675{
14676 char_u buf[NUMBUFLEN];
14677
14678 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014679 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014681 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14682 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683}
14684
14685/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014686 * "repeat()" function
14687 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014688 static void
14689f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014690 typval_T *argvars;
14691 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014692{
14693 char_u *p;
14694 int n;
14695 int slen;
14696 int len;
14697 char_u *r;
14698 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014699
14700 n = get_tv_number(&argvars[1]);
14701 if (argvars[0].v_type == VAR_LIST)
14702 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014703 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014704 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014705 if (list_extend(rettv->vval.v_list,
14706 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014707 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014708 }
14709 else
14710 {
14711 p = get_tv_string(&argvars[0]);
14712 rettv->v_type = VAR_STRING;
14713 rettv->vval.v_string = NULL;
14714
14715 slen = (int)STRLEN(p);
14716 len = slen * n;
14717 if (len <= 0)
14718 return;
14719
14720 r = alloc(len + 1);
14721 if (r != NULL)
14722 {
14723 for (i = 0; i < n; i++)
14724 mch_memmove(r + i * slen, p, (size_t)slen);
14725 r[len] = NUL;
14726 }
14727
14728 rettv->vval.v_string = r;
14729 }
14730}
14731
14732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733 * "resolve()" function
14734 */
14735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014736f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014737 typval_T *argvars;
14738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739{
14740 char_u *p;
14741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014742 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743#ifdef FEAT_SHORTCUT
14744 {
14745 char_u *v = NULL;
14746
14747 v = mch_resolve_shortcut(p);
14748 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014749 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014751 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752 }
14753#else
14754# ifdef HAVE_READLINK
14755 {
14756 char_u buf[MAXPATHL + 1];
14757 char_u *cpy;
14758 int len;
14759 char_u *remain = NULL;
14760 char_u *q;
14761 int is_relative_to_current = FALSE;
14762 int has_trailing_pathsep = FALSE;
14763 int limit = 100;
14764
14765 p = vim_strsave(p);
14766
14767 if (p[0] == '.' && (vim_ispathsep(p[1])
14768 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14769 is_relative_to_current = TRUE;
14770
14771 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014772 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773 has_trailing_pathsep = TRUE;
14774
14775 q = getnextcomp(p);
14776 if (*q != NUL)
14777 {
14778 /* Separate the first path component in "p", and keep the
14779 * remainder (beginning with the path separator). */
14780 remain = vim_strsave(q - 1);
14781 q[-1] = NUL;
14782 }
14783
14784 for (;;)
14785 {
14786 for (;;)
14787 {
14788 len = readlink((char *)p, (char *)buf, MAXPATHL);
14789 if (len <= 0)
14790 break;
14791 buf[len] = NUL;
14792
14793 if (limit-- == 0)
14794 {
14795 vim_free(p);
14796 vim_free(remain);
14797 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014798 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014799 goto fail;
14800 }
14801
14802 /* Ensure that the result will have a trailing path separator
14803 * if the argument has one. */
14804 if (remain == NULL && has_trailing_pathsep)
14805 add_pathsep(buf);
14806
14807 /* Separate the first path component in the link value and
14808 * concatenate the remainders. */
14809 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14810 if (*q != NUL)
14811 {
14812 if (remain == NULL)
14813 remain = vim_strsave(q - 1);
14814 else
14815 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014816 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817 if (cpy != NULL)
14818 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014819 vim_free(remain);
14820 remain = cpy;
14821 }
14822 }
14823 q[-1] = NUL;
14824 }
14825
14826 q = gettail(p);
14827 if (q > p && *q == NUL)
14828 {
14829 /* Ignore trailing path separator. */
14830 q[-1] = NUL;
14831 q = gettail(p);
14832 }
14833 if (q > p && !mch_isFullName(buf))
14834 {
14835 /* symlink is relative to directory of argument */
14836 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14837 if (cpy != NULL)
14838 {
14839 STRCPY(cpy, p);
14840 STRCPY(gettail(cpy), buf);
14841 vim_free(p);
14842 p = cpy;
14843 }
14844 }
14845 else
14846 {
14847 vim_free(p);
14848 p = vim_strsave(buf);
14849 }
14850 }
14851
14852 if (remain == NULL)
14853 break;
14854
14855 /* Append the first path component of "remain" to "p". */
14856 q = getnextcomp(remain + 1);
14857 len = q - remain - (*q != NUL);
14858 cpy = vim_strnsave(p, STRLEN(p) + len);
14859 if (cpy != NULL)
14860 {
14861 STRNCAT(cpy, remain, len);
14862 vim_free(p);
14863 p = cpy;
14864 }
14865 /* Shorten "remain". */
14866 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014867 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868 else
14869 {
14870 vim_free(remain);
14871 remain = NULL;
14872 }
14873 }
14874
14875 /* If the result is a relative path name, make it explicitly relative to
14876 * the current directory if and only if the argument had this form. */
14877 if (!vim_ispathsep(*p))
14878 {
14879 if (is_relative_to_current
14880 && *p != NUL
14881 && !(p[0] == '.'
14882 && (p[1] == NUL
14883 || vim_ispathsep(p[1])
14884 || (p[1] == '.'
14885 && (p[2] == NUL
14886 || vim_ispathsep(p[2]))))))
14887 {
14888 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014889 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890 if (cpy != NULL)
14891 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892 vim_free(p);
14893 p = cpy;
14894 }
14895 }
14896 else if (!is_relative_to_current)
14897 {
14898 /* Strip leading "./". */
14899 q = p;
14900 while (q[0] == '.' && vim_ispathsep(q[1]))
14901 q += 2;
14902 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014903 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904 }
14905 }
14906
14907 /* Ensure that the result will have no trailing path separator
14908 * if the argument had none. But keep "/" or "//". */
14909 if (!has_trailing_pathsep)
14910 {
14911 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014912 if (after_pathsep(p, q))
14913 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914 }
14915
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014916 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917 }
14918# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014919 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014920# endif
14921#endif
14922
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014923 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924
14925#ifdef HAVE_READLINK
14926fail:
14927#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014928 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929}
14930
14931/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014932 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933 */
14934 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014935f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014936 typval_T *argvars;
14937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014938{
Bram Moolenaar33570922005-01-25 22:26:29 +000014939 list_T *l;
14940 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941
Bram Moolenaar0d660222005-01-07 21:51:51 +000014942 if (argvars[0].v_type != VAR_LIST)
14943 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014944 else if ((l = argvars[0].vval.v_list) != NULL
14945 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014946 {
14947 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014948 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014949 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014950 while (li != NULL)
14951 {
14952 ni = li->li_prev;
14953 list_append(l, li);
14954 li = ni;
14955 }
14956 rettv->vval.v_list = l;
14957 rettv->v_type = VAR_LIST;
14958 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014959 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961}
14962
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014963#define SP_NOMOVE 0x01 /* don't move cursor */
14964#define SP_REPEAT 0x02 /* repeat to find outer pair */
14965#define SP_RETCOUNT 0x04 /* return matchcount */
14966#define SP_SETPCMARK 0x08 /* set previous context mark */
14967#define SP_START 0x10 /* accept match at start position */
14968#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14969#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014970
Bram Moolenaar33570922005-01-25 22:26:29 +000014971static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014972
14973/*
14974 * Get flags for a search function.
14975 * Possibly sets "p_ws".
14976 * Returns BACKWARD, FORWARD or zero (for an error).
14977 */
14978 static int
14979get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014980 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014981 int *flagsp;
14982{
14983 int dir = FORWARD;
14984 char_u *flags;
14985 char_u nbuf[NUMBUFLEN];
14986 int mask;
14987
14988 if (varp->v_type != VAR_UNKNOWN)
14989 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014990 flags = get_tv_string_buf_chk(varp, nbuf);
14991 if (flags == NULL)
14992 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014993 while (*flags != NUL)
14994 {
14995 switch (*flags)
14996 {
14997 case 'b': dir = BACKWARD; break;
14998 case 'w': p_ws = TRUE; break;
14999 case 'W': p_ws = FALSE; break;
15000 default: mask = 0;
15001 if (flagsp != NULL)
15002 switch (*flags)
15003 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015004 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015005 case 'e': mask = SP_END; break;
15006 case 'm': mask = SP_RETCOUNT; break;
15007 case 'n': mask = SP_NOMOVE; break;
15008 case 'p': mask = SP_SUBPAT; break;
15009 case 'r': mask = SP_REPEAT; break;
15010 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015011 }
15012 if (mask == 0)
15013 {
15014 EMSG2(_(e_invarg2), flags);
15015 dir = 0;
15016 }
15017 else
15018 *flagsp |= mask;
15019 }
15020 if (dir == 0)
15021 break;
15022 ++flags;
15023 }
15024 }
15025 return dir;
15026}
15027
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015029 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015031 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015032search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015033 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015034 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015035 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015036{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015037 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038 char_u *pat;
15039 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015040 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041 int save_p_ws = p_ws;
15042 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015043 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015044 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015045 proftime_T tm;
15046#ifdef FEAT_RELTIME
15047 long time_limit = 0;
15048#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015049 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015050 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015052 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015053 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015054 if (dir == 0)
15055 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015056 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015057 if (flags & SP_START)
15058 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015059 if (flags & SP_END)
15060 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015061
Bram Moolenaar76929292008-01-06 19:07:36 +000015062 /* Optional arguments: line number to stop searching and timeout. */
15063 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015064 {
15065 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15066 if (lnum_stop < 0)
15067 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015068#ifdef FEAT_RELTIME
15069 if (argvars[3].v_type != VAR_UNKNOWN)
15070 {
15071 time_limit = get_tv_number_chk(&argvars[3], NULL);
15072 if (time_limit < 0)
15073 goto theend;
15074 }
15075#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015076 }
15077
Bram Moolenaar76929292008-01-06 19:07:36 +000015078#ifdef FEAT_RELTIME
15079 /* Set the time limit, if there is one. */
15080 profile_setlimit(time_limit, &tm);
15081#endif
15082
Bram Moolenaar231334e2005-07-25 20:46:57 +000015083 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015084 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015085 * Check to make sure only those flags are set.
15086 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15087 * flags cannot be set. Check for that condition also.
15088 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015089 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015090 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015091 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015092 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015093 goto theend;
15094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015095
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015096 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015097 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015098 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015099 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015100 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015101 if (flags & SP_SUBPAT)
15102 retval = subpatnum;
15103 else
15104 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015105 if (flags & SP_SETPCMARK)
15106 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015108 if (match_pos != NULL)
15109 {
15110 /* Store the match cursor position */
15111 match_pos->lnum = pos.lnum;
15112 match_pos->col = pos.col + 1;
15113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015114 /* "/$" will put the cursor after the end of the line, may need to
15115 * correct that here */
15116 check_cursor();
15117 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015118
15119 /* If 'n' flag is used: restore cursor position. */
15120 if (flags & SP_NOMOVE)
15121 curwin->w_cursor = save_cursor;
Bram Moolenaar860cae12010-06-05 23:22:07 +020015122#ifdef FEAT_CONCEAL
15123 else if (curwin->w_p_conceal
15124 && save_cursor.lnum != curwin->w_cursor.lnum)
15125 {
15126 curwin->w_set_curswant = TRUE;
15127 update_single_line(curwin, save_cursor.lnum);
15128 update_single_line(curwin, curwin->w_cursor.lnum);
15129 }
15130#endif
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015131 else
15132 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015133theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015134 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015135
15136 return retval;
15137}
15138
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015139#ifdef FEAT_FLOAT
15140/*
15141 * "round({float})" function
15142 */
15143 static void
15144f_round(argvars, rettv)
15145 typval_T *argvars;
15146 typval_T *rettv;
15147{
15148 float_T f;
15149
15150 rettv->v_type = VAR_FLOAT;
15151 if (get_float_arg(argvars, &f) == OK)
15152 /* round() is not in C90, use ceil() or floor() instead. */
15153 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15154 else
15155 rettv->vval.v_float = 0.0;
15156}
15157#endif
15158
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015159/*
15160 * "search()" function
15161 */
15162 static void
15163f_search(argvars, rettv)
15164 typval_T *argvars;
15165 typval_T *rettv;
15166{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015167 int flags = 0;
15168
15169 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015170}
15171
Bram Moolenaar071d4272004-06-13 20:20:40 +000015172/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015173 * "searchdecl()" function
15174 */
15175 static void
15176f_searchdecl(argvars, rettv)
15177 typval_T *argvars;
15178 typval_T *rettv;
15179{
15180 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015181 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015182 int error = FALSE;
15183 char_u *name;
15184
15185 rettv->vval.v_number = 1; /* default: FAIL */
15186
15187 name = get_tv_string_chk(&argvars[0]);
15188 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015189 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015190 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015191 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15192 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15193 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015194 if (!error && name != NULL)
15195 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015196 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015197}
15198
15199/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015200 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015201 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015202 static int
15203searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015204 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015205 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206{
15207 char_u *spat, *mpat, *epat;
15208 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015210 int dir;
15211 int flags = 0;
15212 char_u nbuf1[NUMBUFLEN];
15213 char_u nbuf2[NUMBUFLEN];
15214 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015215 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015216 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015217 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218
Bram Moolenaar071d4272004-06-13 20:20:40 +000015219 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015220 spat = get_tv_string_chk(&argvars[0]);
15221 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15222 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15223 if (spat == NULL || mpat == NULL || epat == NULL)
15224 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015225
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226 /* Handle the optional fourth argument: flags */
15227 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015228 if (dir == 0)
15229 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015230
15231 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015232 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15233 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015234 if ((flags & (SP_END | SP_SUBPAT)) != 0
15235 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015236 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015237 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015238 goto theend;
15239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015240
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015241 /* Using 'r' implies 'W', otherwise it doesn't work. */
15242 if (flags & SP_REPEAT)
15243 p_ws = FALSE;
15244
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015245 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015246 if (argvars[3].v_type == VAR_UNKNOWN
15247 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015248 skip = (char_u *)"";
15249 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015251 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015252 if (argvars[5].v_type != VAR_UNKNOWN)
15253 {
15254 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15255 if (lnum_stop < 0)
15256 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015257#ifdef FEAT_RELTIME
15258 if (argvars[6].v_type != VAR_UNKNOWN)
15259 {
15260 time_limit = get_tv_number_chk(&argvars[6], NULL);
15261 if (time_limit < 0)
15262 goto theend;
15263 }
15264#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015265 }
15266 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015267 if (skip == NULL)
15268 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015269
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015270 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015271 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015272
15273theend:
15274 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015275
15276 return retval;
15277}
15278
15279/*
15280 * "searchpair()" function
15281 */
15282 static void
15283f_searchpair(argvars, rettv)
15284 typval_T *argvars;
15285 typval_T *rettv;
15286{
15287 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15288}
15289
15290/*
15291 * "searchpairpos()" function
15292 */
15293 static void
15294f_searchpairpos(argvars, rettv)
15295 typval_T *argvars;
15296 typval_T *rettv;
15297{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015298 pos_T match_pos;
15299 int lnum = 0;
15300 int col = 0;
15301
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015302 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015303 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015304
15305 if (searchpair_cmn(argvars, &match_pos) > 0)
15306 {
15307 lnum = match_pos.lnum;
15308 col = match_pos.col;
15309 }
15310
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015311 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15312 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015313}
15314
15315/*
15316 * Search for a start/middle/end thing.
15317 * Used by searchpair(), see its documentation for the details.
15318 * Returns 0 or -1 for no match,
15319 */
15320 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015321do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15322 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015323 char_u *spat; /* start pattern */
15324 char_u *mpat; /* middle pattern */
15325 char_u *epat; /* end pattern */
15326 int dir; /* BACKWARD or FORWARD */
15327 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015328 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015329 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015330 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015331 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015332{
15333 char_u *save_cpo;
15334 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15335 long retval = 0;
15336 pos_T pos;
15337 pos_T firstpos;
15338 pos_T foundpos;
15339 pos_T save_cursor;
15340 pos_T save_pos;
15341 int n;
15342 int r;
15343 int nest = 1;
15344 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015345 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015346 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015347
15348 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15349 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015350 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015351
Bram Moolenaar76929292008-01-06 19:07:36 +000015352#ifdef FEAT_RELTIME
15353 /* Set the time limit, if there is one. */
15354 profile_setlimit(time_limit, &tm);
15355#endif
15356
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015357 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15358 * start/middle/end (pat3, for the top pair). */
15359 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15360 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15361 if (pat2 == NULL || pat3 == NULL)
15362 goto theend;
15363 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15364 if (*mpat == NUL)
15365 STRCPY(pat3, pat2);
15366 else
15367 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15368 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015369 if (flags & SP_START)
15370 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015371
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372 save_cursor = curwin->w_cursor;
15373 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015374 clearpos(&firstpos);
15375 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376 pat = pat3;
15377 for (;;)
15378 {
15379 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015380 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15382 /* didn't find it or found the first match again: FAIL */
15383 break;
15384
15385 if (firstpos.lnum == 0)
15386 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015387 if (equalpos(pos, foundpos))
15388 {
15389 /* Found the same position again. Can happen with a pattern that
15390 * has "\zs" at the end and searching backwards. Advance one
15391 * character and try again. */
15392 if (dir == BACKWARD)
15393 decl(&pos);
15394 else
15395 incl(&pos);
15396 }
15397 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015399 /* clear the start flag to avoid getting stuck here */
15400 options &= ~SEARCH_START;
15401
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402 /* If the skip pattern matches, ignore this match. */
15403 if (*skip != NUL)
15404 {
15405 save_pos = curwin->w_cursor;
15406 curwin->w_cursor = pos;
15407 r = eval_to_bool(skip, &err, NULL, FALSE);
15408 curwin->w_cursor = save_pos;
15409 if (err)
15410 {
15411 /* Evaluating {skip} caused an error, break here. */
15412 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015413 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 break;
15415 }
15416 if (r)
15417 continue;
15418 }
15419
15420 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15421 {
15422 /* Found end when searching backwards or start when searching
15423 * forward: nested pair. */
15424 ++nest;
15425 pat = pat2; /* nested, don't search for middle */
15426 }
15427 else
15428 {
15429 /* Found end when searching forward or start when searching
15430 * backward: end of (nested) pair; or found middle in outer pair. */
15431 if (--nest == 1)
15432 pat = pat3; /* outer level, search for middle */
15433 }
15434
15435 if (nest == 0)
15436 {
15437 /* Found the match: return matchcount or line number. */
15438 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015439 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015441 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015442 if (flags & SP_SETPCMARK)
15443 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444 curwin->w_cursor = pos;
15445 if (!(flags & SP_REPEAT))
15446 break;
15447 nest = 1; /* search for next unmatched */
15448 }
15449 }
15450
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015451 if (match_pos != NULL)
15452 {
15453 /* Store the match cursor position */
15454 match_pos->lnum = curwin->w_cursor.lnum;
15455 match_pos->col = curwin->w_cursor.col + 1;
15456 }
15457
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015459 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 curwin->w_cursor = save_cursor;
15461
15462theend:
15463 vim_free(pat2);
15464 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015465 if (p_cpo == empty_option)
15466 p_cpo = save_cpo;
15467 else
15468 /* Darn, evaluating the {skip} expression changed the value. */
15469 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015470
15471 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472}
15473
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015474/*
15475 * "searchpos()" function
15476 */
15477 static void
15478f_searchpos(argvars, rettv)
15479 typval_T *argvars;
15480 typval_T *rettv;
15481{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015482 pos_T match_pos;
15483 int lnum = 0;
15484 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015485 int n;
15486 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015487
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015488 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015489 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015490
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015491 n = search_cmn(argvars, &match_pos, &flags);
15492 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015493 {
15494 lnum = match_pos.lnum;
15495 col = match_pos.col;
15496 }
15497
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015498 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15499 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015500 if (flags & SP_SUBPAT)
15501 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015502}
15503
15504
Bram Moolenaar0d660222005-01-07 21:51:51 +000015505 static void
15506f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015507 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015510#ifdef FEAT_CLIENTSERVER
15511 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015512 char_u *server = get_tv_string_chk(&argvars[0]);
15513 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514
Bram Moolenaar0d660222005-01-07 21:51:51 +000015515 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015516 if (server == NULL || reply == NULL)
15517 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015518 if (check_restricted() || check_secure())
15519 return;
15520# ifdef FEAT_X11
15521 if (check_connection() == FAIL)
15522 return;
15523# endif
15524
15525 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015527 EMSG(_("E258: Unable to send to client"));
15528 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015530 rettv->vval.v_number = 0;
15531#else
15532 rettv->vval.v_number = -1;
15533#endif
15534}
15535
Bram Moolenaar0d660222005-01-07 21:51:51 +000015536 static void
15537f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015538 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015539 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015540{
15541 char_u *r = NULL;
15542
15543#ifdef FEAT_CLIENTSERVER
15544# ifdef WIN32
15545 r = serverGetVimNames();
15546# else
15547 make_connection();
15548 if (X_DISPLAY != NULL)
15549 r = serverGetVimNames(X_DISPLAY);
15550# endif
15551#endif
15552 rettv->v_type = VAR_STRING;
15553 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554}
15555
15556/*
15557 * "setbufvar()" function
15558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015560f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015561 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015562 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563{
15564 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015567 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568 char_u nbuf[NUMBUFLEN];
15569
15570 if (check_restricted() || check_secure())
15571 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015572 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15573 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015574 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575 varp = &argvars[2];
15576
15577 if (buf != NULL && varname != NULL && varp != NULL)
15578 {
15579 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581
15582 if (*varname == '&')
15583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015584 long numval;
15585 char_u *strval;
15586 int error = FALSE;
15587
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015589 numval = get_tv_number_chk(varp, &error);
15590 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015591 if (!error && strval != NULL)
15592 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 }
15594 else
15595 {
15596 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15597 if (bufvarname != NULL)
15598 {
15599 STRCPY(bufvarname, "b:");
15600 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015601 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602 vim_free(bufvarname);
15603 }
15604 }
15605
15606 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609}
15610
15611/*
15612 * "setcmdpos()" function
15613 */
15614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015615f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015616 typval_T *argvars;
15617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015619 int pos = (int)get_tv_number(&argvars[0]) - 1;
15620
15621 if (pos >= 0)
15622 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623}
15624
15625/*
15626 * "setline()" function
15627 */
15628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015629f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015630 typval_T *argvars;
15631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632{
15633 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015634 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015635 list_T *l = NULL;
15636 listitem_T *li = NULL;
15637 long added = 0;
15638 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015640 lnum = get_tv_lnum(&argvars[0]);
15641 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015643 l = argvars[1].vval.v_list;
15644 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015646 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015647 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015648
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015649 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015650 for (;;)
15651 {
15652 if (l != NULL)
15653 {
15654 /* list argument, get next string */
15655 if (li == NULL)
15656 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015657 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015658 li = li->li_next;
15659 }
15660
15661 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015662 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015663 break;
15664 if (lnum <= curbuf->b_ml.ml_line_count)
15665 {
15666 /* existing line, replace it */
15667 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15668 {
15669 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015670 if (lnum == curwin->w_cursor.lnum)
15671 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015672 rettv->vval.v_number = 0; /* OK */
15673 }
15674 }
15675 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15676 {
15677 /* lnum is one past the last line, append the line */
15678 ++added;
15679 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15680 rettv->vval.v_number = 0; /* OK */
15681 }
15682
15683 if (l == NULL) /* only one string argument */
15684 break;
15685 ++lnum;
15686 }
15687
15688 if (added > 0)
15689 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690}
15691
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015692static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15693
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015695 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015696 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015697 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015698set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015699 win_T *wp UNUSED;
15700 typval_T *list_arg UNUSED;
15701 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015702 typval_T *rettv;
15703{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015704#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015705 char_u *act;
15706 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015707#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015708
Bram Moolenaar2641f772005-03-25 21:58:17 +000015709 rettv->vval.v_number = -1;
15710
15711#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015712 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015713 EMSG(_(e_listreq));
15714 else
15715 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015716 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015717
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015718 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015719 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015720 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015721 if (act == NULL)
15722 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015723 if (*act == 'a' || *act == 'r')
15724 action = *act;
15725 }
15726
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015727 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015728 rettv->vval.v_number = 0;
15729 }
15730#endif
15731}
15732
15733/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015734 * "setloclist()" function
15735 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015736 static void
15737f_setloclist(argvars, rettv)
15738 typval_T *argvars;
15739 typval_T *rettv;
15740{
15741 win_T *win;
15742
15743 rettv->vval.v_number = -1;
15744
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015745 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015746 if (win != NULL)
15747 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15748}
15749
15750/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015751 * "setmatches()" function
15752 */
15753 static void
15754f_setmatches(argvars, rettv)
15755 typval_T *argvars;
15756 typval_T *rettv;
15757{
15758#ifdef FEAT_SEARCH_EXTRA
15759 list_T *l;
15760 listitem_T *li;
15761 dict_T *d;
15762
15763 rettv->vval.v_number = -1;
15764 if (argvars[0].v_type != VAR_LIST)
15765 {
15766 EMSG(_(e_listreq));
15767 return;
15768 }
15769 if ((l = argvars[0].vval.v_list) != NULL)
15770 {
15771
15772 /* To some extent make sure that we are dealing with a list from
15773 * "getmatches()". */
15774 li = l->lv_first;
15775 while (li != NULL)
15776 {
15777 if (li->li_tv.v_type != VAR_DICT
15778 || (d = li->li_tv.vval.v_dict) == NULL)
15779 {
15780 EMSG(_(e_invarg));
15781 return;
15782 }
15783 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15784 && dict_find(d, (char_u *)"pattern", -1) != NULL
15785 && dict_find(d, (char_u *)"priority", -1) != NULL
15786 && dict_find(d, (char_u *)"id", -1) != NULL))
15787 {
15788 EMSG(_(e_invarg));
15789 return;
15790 }
15791 li = li->li_next;
15792 }
15793
15794 clear_matches(curwin);
15795 li = l->lv_first;
15796 while (li != NULL)
15797 {
15798 d = li->li_tv.vval.v_dict;
15799 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15800 get_dict_string(d, (char_u *)"pattern", FALSE),
15801 (int)get_dict_number(d, (char_u *)"priority"),
15802 (int)get_dict_number(d, (char_u *)"id"));
15803 li = li->li_next;
15804 }
15805 rettv->vval.v_number = 0;
15806 }
15807#endif
15808}
15809
15810/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015811 * "setpos()" function
15812 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015813 static void
15814f_setpos(argvars, rettv)
15815 typval_T *argvars;
15816 typval_T *rettv;
15817{
15818 pos_T pos;
15819 int fnum;
15820 char_u *name;
15821
Bram Moolenaar08250432008-02-13 11:42:46 +000015822 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015823 name = get_tv_string_chk(argvars);
15824 if (name != NULL)
15825 {
15826 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15827 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015828 if (--pos.col < 0)
15829 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015830 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015831 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015832 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015833 if (fnum == curbuf->b_fnum)
15834 {
15835 curwin->w_cursor = pos;
15836 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015837 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015838 }
15839 else
15840 EMSG(_(e_invarg));
15841 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015842 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15843 {
15844 /* set mark */
15845 if (setmark_pos(name[1], &pos, fnum) == OK)
15846 rettv->vval.v_number = 0;
15847 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015848 else
15849 EMSG(_(e_invarg));
15850 }
15851 }
15852}
15853
15854/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015855 * "setqflist()" function
15856 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015857 static void
15858f_setqflist(argvars, rettv)
15859 typval_T *argvars;
15860 typval_T *rettv;
15861{
15862 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15863}
15864
15865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866 * "setreg()" function
15867 */
15868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015869f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015870 typval_T *argvars;
15871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872{
15873 int regname;
15874 char_u *strregname;
15875 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015876 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877 int append;
15878 char_u yank_type;
15879 long block_len;
15880
15881 block_len = -1;
15882 yank_type = MAUTO;
15883 append = FALSE;
15884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015885 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015886 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015888 if (strregname == NULL)
15889 return; /* type error; errmsg already given */
15890 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015891 if (regname == 0 || regname == '@')
15892 regname = '"';
15893 else if (regname == '=')
15894 return;
15895
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015896 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015898 stropt = get_tv_string_chk(&argvars[2]);
15899 if (stropt == NULL)
15900 return; /* type error */
15901 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015902 switch (*stropt)
15903 {
15904 case 'a': case 'A': /* append */
15905 append = TRUE;
15906 break;
15907 case 'v': case 'c': /* character-wise selection */
15908 yank_type = MCHAR;
15909 break;
15910 case 'V': case 'l': /* line-wise selection */
15911 yank_type = MLINE;
15912 break;
15913#ifdef FEAT_VISUAL
15914 case 'b': case Ctrl_V: /* block-wise selection */
15915 yank_type = MBLOCK;
15916 if (VIM_ISDIGIT(stropt[1]))
15917 {
15918 ++stropt;
15919 block_len = getdigits(&stropt) - 1;
15920 --stropt;
15921 }
15922 break;
15923#endif
15924 }
15925 }
15926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015927 strval = get_tv_string_chk(&argvars[1]);
15928 if (strval != NULL)
15929 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015931 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015932}
15933
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015934/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020015935 * "settabvar()" function
15936 */
15937 static void
15938f_settabvar(argvars, rettv)
15939 typval_T *argvars;
15940 typval_T *rettv;
15941{
15942 tabpage_T *save_curtab;
15943 char_u *varname, *tabvarname;
15944 typval_T *varp;
15945 tabpage_T *tp;
15946
15947 rettv->vval.v_number = 0;
15948
15949 if (check_restricted() || check_secure())
15950 return;
15951
15952 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15953 varname = get_tv_string_chk(&argvars[1]);
15954 varp = &argvars[2];
15955
15956 if (tp != NULL && varname != NULL && varp != NULL)
15957 {
15958 save_curtab = curtab;
15959 goto_tabpage_tp(tp);
15960
15961 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
15962 if (tabvarname != NULL)
15963 {
15964 STRCPY(tabvarname, "t:");
15965 STRCPY(tabvarname + 2, varname);
15966 set_var(tabvarname, varp, TRUE);
15967 vim_free(tabvarname);
15968 }
15969
15970 /* Restore current tabpage */
15971 if (valid_tabpage(save_curtab))
15972 goto_tabpage_tp(save_curtab);
15973 }
15974}
15975
15976/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015977 * "settabwinvar()" function
15978 */
15979 static void
15980f_settabwinvar(argvars, rettv)
15981 typval_T *argvars;
15982 typval_T *rettv;
15983{
15984 setwinvar(argvars, rettv, 1);
15985}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986
15987/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015988 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015991f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015992 typval_T *argvars;
15993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015995 setwinvar(argvars, rettv, 0);
15996}
15997
15998/*
15999 * "setwinvar()" and "settabwinvar()" functions
16000 */
16001 static void
16002setwinvar(argvars, rettv, off)
16003 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016004 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016005 int off;
16006{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016007 win_T *win;
16008#ifdef FEAT_WINDOWS
16009 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016010 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016011#endif
16012 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016013 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016014 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016015 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016016
16017 if (check_restricted() || check_secure())
16018 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016019
16020#ifdef FEAT_WINDOWS
16021 if (off == 1)
16022 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16023 else
16024 tp = curtab;
16025#endif
16026 win = find_win_by_nr(&argvars[off], tp);
16027 varname = get_tv_string_chk(&argvars[off + 1]);
16028 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029
16030 if (win != NULL && varname != NULL && varp != NULL)
16031 {
16032#ifdef FEAT_WINDOWS
16033 /* set curwin to be our win, temporarily */
16034 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016035 save_curtab = curtab;
16036 goto_tabpage_tp(tp);
16037 if (!win_valid(win))
16038 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039 curwin = win;
16040 curbuf = curwin->w_buffer;
16041#endif
16042
16043 if (*varname == '&')
16044 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016045 long numval;
16046 char_u *strval;
16047 int error = FALSE;
16048
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016050 numval = get_tv_number_chk(varp, &error);
16051 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016052 if (!error && strval != NULL)
16053 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054 }
16055 else
16056 {
16057 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16058 if (winvarname != NULL)
16059 {
16060 STRCPY(winvarname, "w:");
16061 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016062 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063 vim_free(winvarname);
16064 }
16065 }
16066
16067#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016068 /* Restore current tabpage and window, if still valid (autocomands can
16069 * make them invalid). */
16070 if (valid_tabpage(save_curtab))
16071 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072 if (win_valid(save_curwin))
16073 {
16074 curwin = save_curwin;
16075 curbuf = curwin->w_buffer;
16076 }
16077#endif
16078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079}
16080
16081/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016082 * "shellescape({string})" function
16083 */
16084 static void
16085f_shellescape(argvars, rettv)
16086 typval_T *argvars;
16087 typval_T *rettv;
16088{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016089 rettv->vval.v_string = vim_strsave_shellescape(
16090 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016091 rettv->v_type = VAR_STRING;
16092}
16093
16094/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016095 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096 */
16097 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016098f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016099 typval_T *argvars;
16100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016102 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103
Bram Moolenaar0d660222005-01-07 21:51:51 +000016104 p = get_tv_string(&argvars[0]);
16105 rettv->vval.v_string = vim_strsave(p);
16106 simplify_filename(rettv->vval.v_string); /* simplify in place */
16107 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108}
16109
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016110#ifdef FEAT_FLOAT
16111/*
16112 * "sin()" function
16113 */
16114 static void
16115f_sin(argvars, rettv)
16116 typval_T *argvars;
16117 typval_T *rettv;
16118{
16119 float_T f;
16120
16121 rettv->v_type = VAR_FLOAT;
16122 if (get_float_arg(argvars, &f) == OK)
16123 rettv->vval.v_float = sin(f);
16124 else
16125 rettv->vval.v_float = 0.0;
16126}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016127
16128/*
16129 * "sinh()" function
16130 */
16131 static void
16132f_sinh(argvars, rettv)
16133 typval_T *argvars;
16134 typval_T *rettv;
16135{
16136 float_T f;
16137
16138 rettv->v_type = VAR_FLOAT;
16139 if (get_float_arg(argvars, &f) == OK)
16140 rettv->vval.v_float = sinh(f);
16141 else
16142 rettv->vval.v_float = 0.0;
16143}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016144#endif
16145
Bram Moolenaar0d660222005-01-07 21:51:51 +000016146static int
16147#ifdef __BORLANDC__
16148 _RTLENTRYF
16149#endif
16150 item_compare __ARGS((const void *s1, const void *s2));
16151static int
16152#ifdef __BORLANDC__
16153 _RTLENTRYF
16154#endif
16155 item_compare2 __ARGS((const void *s1, const void *s2));
16156
16157static int item_compare_ic;
16158static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016159static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016160#define ITEM_COMPARE_FAIL 999
16161
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016163 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016165 static int
16166#ifdef __BORLANDC__
16167_RTLENTRYF
16168#endif
16169item_compare(s1, s2)
16170 const void *s1;
16171 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016173 char_u *p1, *p2;
16174 char_u *tofree1, *tofree2;
16175 int res;
16176 char_u numbuf1[NUMBUFLEN];
16177 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016179 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16180 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016181 if (p1 == NULL)
16182 p1 = (char_u *)"";
16183 if (p2 == NULL)
16184 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016185 if (item_compare_ic)
16186 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016188 res = STRCMP(p1, p2);
16189 vim_free(tofree1);
16190 vim_free(tofree2);
16191 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192}
16193
16194 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016195#ifdef __BORLANDC__
16196_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016198item_compare2(s1, s2)
16199 const void *s1;
16200 const void *s2;
16201{
16202 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016203 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016204 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016205 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016207 /* shortcut after failure in previous call; compare all items equal */
16208 if (item_compare_func_err)
16209 return 0;
16210
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016211 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16212 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016213 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16214 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016215
16216 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016217 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016218 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016219 clear_tv(&argv[0]);
16220 clear_tv(&argv[1]);
16221
16222 if (res == FAIL)
16223 res = ITEM_COMPARE_FAIL;
16224 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016225 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16226 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016227 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016228 clear_tv(&rettv);
16229 return res;
16230}
16231
16232/*
16233 * "sort({list})" function
16234 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016236f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016237 typval_T *argvars;
16238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239{
Bram Moolenaar33570922005-01-25 22:26:29 +000016240 list_T *l;
16241 listitem_T *li;
16242 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016243 long len;
16244 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016245
Bram Moolenaar0d660222005-01-07 21:51:51 +000016246 if (argvars[0].v_type != VAR_LIST)
16247 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248 else
16249 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016250 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016251 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016252 return;
16253 rettv->vval.v_list = l;
16254 rettv->v_type = VAR_LIST;
16255 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256
Bram Moolenaar0d660222005-01-07 21:51:51 +000016257 len = list_len(l);
16258 if (len <= 1)
16259 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016260
Bram Moolenaar0d660222005-01-07 21:51:51 +000016261 item_compare_ic = FALSE;
16262 item_compare_func = NULL;
16263 if (argvars[1].v_type != VAR_UNKNOWN)
16264 {
16265 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016266 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016267 else
16268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016269 int error = FALSE;
16270
16271 i = get_tv_number_chk(&argvars[1], &error);
16272 if (error)
16273 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016274 if (i == 1)
16275 item_compare_ic = TRUE;
16276 else
16277 item_compare_func = get_tv_string(&argvars[1]);
16278 }
16279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280
Bram Moolenaar0d660222005-01-07 21:51:51 +000016281 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016282 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016283 if (ptrs == NULL)
16284 return;
16285 i = 0;
16286 for (li = l->lv_first; li != NULL; li = li->li_next)
16287 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016289 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016290 /* test the compare function */
16291 if (item_compare_func != NULL
16292 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16293 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016294 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016296 {
16297 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016298 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016299 item_compare_func == NULL ? item_compare : item_compare2);
16300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016301 if (!item_compare_func_err)
16302 {
16303 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016304 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016305 l->lv_len = 0;
16306 for (i = 0; i < len; ++i)
16307 list_append(l, ptrs[i]);
16308 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016309 }
16310
16311 vim_free(ptrs);
16312 }
16313}
16314
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016315/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016316 * "soundfold({word})" function
16317 */
16318 static void
16319f_soundfold(argvars, rettv)
16320 typval_T *argvars;
16321 typval_T *rettv;
16322{
16323 char_u *s;
16324
16325 rettv->v_type = VAR_STRING;
16326 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016327#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016328 rettv->vval.v_string = eval_soundfold(s);
16329#else
16330 rettv->vval.v_string = vim_strsave(s);
16331#endif
16332}
16333
16334/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016335 * "spellbadword()" function
16336 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016337 static void
16338f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016339 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016340 typval_T *rettv;
16341{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016342 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016343 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016344 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016345
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016346 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016347 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016348
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016349#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016350 if (argvars[0].v_type == VAR_UNKNOWN)
16351 {
16352 /* Find the start and length of the badly spelled word. */
16353 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16354 if (len != 0)
16355 word = ml_get_cursor();
16356 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016357 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016358 {
16359 char_u *str = get_tv_string_chk(&argvars[0]);
16360 int capcol = -1;
16361
16362 if (str != NULL)
16363 {
16364 /* Check the argument for spelling. */
16365 while (*str != NUL)
16366 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016367 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016368 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016369 {
16370 word = str;
16371 break;
16372 }
16373 str += len;
16374 }
16375 }
16376 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016377#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016378
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016379 list_append_string(rettv->vval.v_list, word, len);
16380 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016381 attr == HLF_SPB ? "bad" :
16382 attr == HLF_SPR ? "rare" :
16383 attr == HLF_SPL ? "local" :
16384 attr == HLF_SPC ? "caps" :
16385 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016386}
16387
16388/*
16389 * "spellsuggest()" function
16390 */
16391 static void
16392f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016393 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016394 typval_T *rettv;
16395{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016396#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016397 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016398 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016399 int maxcount;
16400 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016401 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016402 listitem_T *li;
16403 int need_capital = FALSE;
16404#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016405
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016406 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016407 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016408
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016409#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016410 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016411 {
16412 str = get_tv_string(&argvars[0]);
16413 if (argvars[1].v_type != VAR_UNKNOWN)
16414 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016415 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016416 if (maxcount <= 0)
16417 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016418 if (argvars[2].v_type != VAR_UNKNOWN)
16419 {
16420 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16421 if (typeerr)
16422 return;
16423 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016424 }
16425 else
16426 maxcount = 25;
16427
Bram Moolenaar4770d092006-01-12 23:22:24 +000016428 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016429
16430 for (i = 0; i < ga.ga_len; ++i)
16431 {
16432 str = ((char_u **)ga.ga_data)[i];
16433
16434 li = listitem_alloc();
16435 if (li == NULL)
16436 vim_free(str);
16437 else
16438 {
16439 li->li_tv.v_type = VAR_STRING;
16440 li->li_tv.v_lock = 0;
16441 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016442 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016443 }
16444 }
16445 ga_clear(&ga);
16446 }
16447#endif
16448}
16449
Bram Moolenaar0d660222005-01-07 21:51:51 +000016450 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016451f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016452 typval_T *argvars;
16453 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016454{
16455 char_u *str;
16456 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016457 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016458 regmatch_T regmatch;
16459 char_u patbuf[NUMBUFLEN];
16460 char_u *save_cpo;
16461 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016462 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016463 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016464 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016465
16466 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16467 save_cpo = p_cpo;
16468 p_cpo = (char_u *)"";
16469
16470 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016471 if (argvars[1].v_type != VAR_UNKNOWN)
16472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016473 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16474 if (pat == NULL)
16475 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016476 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016477 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016478 }
16479 if (pat == NULL || *pat == NUL)
16480 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016481
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016482 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016484 if (typeerr)
16485 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016486
Bram Moolenaar0d660222005-01-07 21:51:51 +000016487 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16488 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016489 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016490 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016491 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016492 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016493 if (*str == NUL)
16494 match = FALSE; /* empty item at the end */
16495 else
16496 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016497 if (match)
16498 end = regmatch.startp[0];
16499 else
16500 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016501 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16502 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016503 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016504 if (list_append_string(rettv->vval.v_list, str,
16505 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016506 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016507 }
16508 if (!match)
16509 break;
16510 /* Advance to just after the match. */
16511 if (regmatch.endp[0] > str)
16512 col = 0;
16513 else
16514 {
16515 /* Don't get stuck at the same match. */
16516#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016517 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016518#else
16519 col = 1;
16520#endif
16521 }
16522 str = regmatch.endp[0];
16523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016524
Bram Moolenaar0d660222005-01-07 21:51:51 +000016525 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016527
Bram Moolenaar0d660222005-01-07 21:51:51 +000016528 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529}
16530
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016531#ifdef FEAT_FLOAT
16532/*
16533 * "sqrt()" function
16534 */
16535 static void
16536f_sqrt(argvars, rettv)
16537 typval_T *argvars;
16538 typval_T *rettv;
16539{
16540 float_T f;
16541
16542 rettv->v_type = VAR_FLOAT;
16543 if (get_float_arg(argvars, &f) == OK)
16544 rettv->vval.v_float = sqrt(f);
16545 else
16546 rettv->vval.v_float = 0.0;
16547}
16548
16549/*
16550 * "str2float()" function
16551 */
16552 static void
16553f_str2float(argvars, rettv)
16554 typval_T *argvars;
16555 typval_T *rettv;
16556{
16557 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16558
16559 if (*p == '+')
16560 p = skipwhite(p + 1);
16561 (void)string2float(p, &rettv->vval.v_float);
16562 rettv->v_type = VAR_FLOAT;
16563}
16564#endif
16565
Bram Moolenaar2c932302006-03-18 21:42:09 +000016566/*
16567 * "str2nr()" function
16568 */
16569 static void
16570f_str2nr(argvars, rettv)
16571 typval_T *argvars;
16572 typval_T *rettv;
16573{
16574 int base = 10;
16575 char_u *p;
16576 long n;
16577
16578 if (argvars[1].v_type != VAR_UNKNOWN)
16579 {
16580 base = get_tv_number(&argvars[1]);
16581 if (base != 8 && base != 10 && base != 16)
16582 {
16583 EMSG(_(e_invarg));
16584 return;
16585 }
16586 }
16587
16588 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016589 if (*p == '+')
16590 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016591 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16592 rettv->vval.v_number = n;
16593}
16594
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595#ifdef HAVE_STRFTIME
16596/*
16597 * "strftime({format}[, {time}])" function
16598 */
16599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016600f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016601 typval_T *argvars;
16602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603{
16604 char_u result_buf[256];
16605 struct tm *curtime;
16606 time_t seconds;
16607 char_u *p;
16608
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016609 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016611 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016612 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016613 seconds = time(NULL);
16614 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016615 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016616 curtime = localtime(&seconds);
16617 /* MSVC returns NULL for an invalid value of seconds. */
16618 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016619 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016620 else
16621 {
16622# ifdef FEAT_MBYTE
16623 vimconv_T conv;
16624 char_u *enc;
16625
16626 conv.vc_type = CONV_NONE;
16627 enc = enc_locale();
16628 convert_setup(&conv, p_enc, enc);
16629 if (conv.vc_type != CONV_NONE)
16630 p = string_convert(&conv, p, NULL);
16631# endif
16632 if (p != NULL)
16633 (void)strftime((char *)result_buf, sizeof(result_buf),
16634 (char *)p, curtime);
16635 else
16636 result_buf[0] = NUL;
16637
16638# ifdef FEAT_MBYTE
16639 if (conv.vc_type != CONV_NONE)
16640 vim_free(p);
16641 convert_setup(&conv, enc, p_enc);
16642 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016643 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 else
16645# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016646 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647
16648# ifdef FEAT_MBYTE
16649 /* Release conversion descriptors */
16650 convert_setup(&conv, NULL, NULL);
16651 vim_free(enc);
16652# endif
16653 }
16654}
16655#endif
16656
16657/*
16658 * "stridx()" function
16659 */
16660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016661f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016662 typval_T *argvars;
16663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664{
16665 char_u buf[NUMBUFLEN];
16666 char_u *needle;
16667 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016668 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016670 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016672 needle = get_tv_string_chk(&argvars[1]);
16673 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016674 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016675 if (needle == NULL || haystack == NULL)
16676 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677
Bram Moolenaar33570922005-01-25 22:26:29 +000016678 if (argvars[2].v_type != VAR_UNKNOWN)
16679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016680 int error = FALSE;
16681
16682 start_idx = get_tv_number_chk(&argvars[2], &error);
16683 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016684 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016685 if (start_idx >= 0)
16686 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016687 }
16688
16689 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16690 if (pos != NULL)
16691 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692}
16693
16694/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016695 * "string()" function
16696 */
16697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016698f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016699 typval_T *argvars;
16700 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016701{
16702 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016703 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016704
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016705 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016706 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016707 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016708 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016709 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710}
16711
16712/*
16713 * "strlen()" function
16714 */
16715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016716f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016717 typval_T *argvars;
16718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016720 rettv->vval.v_number = (varnumber_T)(STRLEN(
16721 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722}
16723
16724/*
16725 * "strpart()" function
16726 */
16727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016728f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016729 typval_T *argvars;
16730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731{
16732 char_u *p;
16733 int n;
16734 int len;
16735 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016736 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016738 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739 slen = (int)STRLEN(p);
16740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016741 n = get_tv_number_chk(&argvars[1], &error);
16742 if (error)
16743 len = 0;
16744 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016745 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746 else
16747 len = slen - n; /* default len: all bytes that are available. */
16748
16749 /*
16750 * Only return the overlap between the specified part and the actual
16751 * string.
16752 */
16753 if (n < 0)
16754 {
16755 len += n;
16756 n = 0;
16757 }
16758 else if (n > slen)
16759 n = slen;
16760 if (len < 0)
16761 len = 0;
16762 else if (n + len > slen)
16763 len = slen - n;
16764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016765 rettv->v_type = VAR_STRING;
16766 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767}
16768
16769/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016770 * "strridx()" function
16771 */
16772 static void
16773f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016774 typval_T *argvars;
16775 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016776{
16777 char_u buf[NUMBUFLEN];
16778 char_u *needle;
16779 char_u *haystack;
16780 char_u *rest;
16781 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016782 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016784 needle = get_tv_string_chk(&argvars[1]);
16785 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016786
16787 rettv->vval.v_number = -1;
16788 if (needle == NULL || haystack == NULL)
16789 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016790
16791 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016792 if (argvars[2].v_type != VAR_UNKNOWN)
16793 {
16794 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016795 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016796 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016797 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016798 }
16799 else
16800 end_idx = haystack_len;
16801
Bram Moolenaar0d660222005-01-07 21:51:51 +000016802 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016803 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016804 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016805 lastmatch = haystack + end_idx;
16806 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016807 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016808 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016809 for (rest = haystack; *rest != '\0'; ++rest)
16810 {
16811 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016812 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016813 break;
16814 lastmatch = rest;
16815 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016816 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016817
16818 if (lastmatch == NULL)
16819 rettv->vval.v_number = -1;
16820 else
16821 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16822}
16823
16824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825 * "strtrans()" function
16826 */
16827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016828f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016829 typval_T *argvars;
16830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016832 rettv->v_type = VAR_STRING;
16833 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834}
16835
16836/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016837 * "submatch()" function
16838 */
16839 static void
16840f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016841 typval_T *argvars;
16842 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016843{
16844 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016845 rettv->vval.v_string =
16846 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016847}
16848
16849/*
16850 * "substitute()" function
16851 */
16852 static void
16853f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016854 typval_T *argvars;
16855 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016856{
16857 char_u patbuf[NUMBUFLEN];
16858 char_u subbuf[NUMBUFLEN];
16859 char_u flagsbuf[NUMBUFLEN];
16860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016861 char_u *str = get_tv_string_chk(&argvars[0]);
16862 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16863 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16864 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16865
Bram Moolenaar0d660222005-01-07 21:51:51 +000016866 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016867 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16868 rettv->vval.v_string = NULL;
16869 else
16870 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016871}
16872
16873/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016874 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016877f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016878 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880{
16881 int id = 0;
16882#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016883 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 long col;
16885 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016886 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016888 lnum = get_tv_lnum(argvars); /* -1 on type error */
16889 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16890 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016892 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016893 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016894 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016895#endif
16896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016897 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898}
16899
16900/*
16901 * "synIDattr(id, what [, mode])" function
16902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016904f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016905 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016906 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907{
16908 char_u *p = NULL;
16909#ifdef FEAT_SYN_HL
16910 int id;
16911 char_u *what;
16912 char_u *mode;
16913 char_u modebuf[NUMBUFLEN];
16914 int modec;
16915
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016916 id = get_tv_number(&argvars[0]);
16917 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016918 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016920 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921 modec = TOLOWER_ASC(mode[0]);
16922 if (modec != 't' && modec != 'c'
16923#ifdef FEAT_GUI
16924 && modec != 'g'
16925#endif
16926 )
16927 modec = 0; /* replace invalid with current */
16928 }
16929 else
16930 {
16931#ifdef FEAT_GUI
16932 if (gui.in_use)
16933 modec = 'g';
16934 else
16935#endif
16936 if (t_colors > 1)
16937 modec = 'c';
16938 else
16939 modec = 't';
16940 }
16941
16942
16943 switch (TOLOWER_ASC(what[0]))
16944 {
16945 case 'b':
16946 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16947 p = highlight_color(id, what, modec);
16948 else /* bold */
16949 p = highlight_has_attr(id, HL_BOLD, modec);
16950 break;
16951
Bram Moolenaar12682fd2010-03-10 13:43:49 +010016952 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953 p = highlight_color(id, what, modec);
16954 break;
16955
16956 case 'i':
16957 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16958 p = highlight_has_attr(id, HL_INVERSE, modec);
16959 else /* italic */
16960 p = highlight_has_attr(id, HL_ITALIC, modec);
16961 break;
16962
16963 case 'n': /* name */
16964 p = get_highlight_name(NULL, id - 1);
16965 break;
16966
16967 case 'r': /* reverse */
16968 p = highlight_has_attr(id, HL_INVERSE, modec);
16969 break;
16970
Bram Moolenaar6f507d62008-11-28 10:16:05 +000016971 case 's':
16972 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
16973 p = highlight_color(id, what, modec);
16974 else /* standout */
16975 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976 break;
16977
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016978 case 'u':
16979 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16980 /* underline */
16981 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16982 else
16983 /* undercurl */
16984 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985 break;
16986 }
16987
16988 if (p != NULL)
16989 p = vim_strsave(p);
16990#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016991 rettv->v_type = VAR_STRING;
16992 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993}
16994
16995/*
16996 * "synIDtrans(id)" function
16997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016999f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017000 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002{
17003 int id;
17004
17005#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017006 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017007
17008 if (id > 0)
17009 id = syn_get_final_id(id);
17010 else
17011#endif
17012 id = 0;
17013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017014 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015}
17016
17017/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017018 * "synstack(lnum, col)" function
17019 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017020 static void
17021f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017022 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017023 typval_T *rettv;
17024{
17025#ifdef FEAT_SYN_HL
17026 long lnum;
17027 long col;
17028 int i;
17029 int id;
17030#endif
17031
17032 rettv->v_type = VAR_LIST;
17033 rettv->vval.v_list = NULL;
17034
17035#ifdef FEAT_SYN_HL
17036 lnum = get_tv_lnum(argvars); /* -1 on type error */
17037 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17038
17039 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar6cad8bd2008-09-10 13:39:10 +000017040 && col >= 0 && (col == 0 || col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017041 && rettv_list_alloc(rettv) != FAIL)
17042 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017043 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017044 for (i = 0; ; ++i)
17045 {
17046 id = syn_get_stack_item(i);
17047 if (id < 0)
17048 break;
17049 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17050 break;
17051 }
17052 }
17053#endif
17054}
17055
17056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057 * "system()" function
17058 */
17059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017060f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017061 typval_T *argvars;
17062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017064 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017065 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017066 char_u *infile = NULL;
17067 char_u buf[NUMBUFLEN];
17068 int err = FALSE;
17069 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017071 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017072 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017073
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017074 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017075 {
17076 /*
17077 * Write the string to a temp file, to be used for input of the shell
17078 * command.
17079 */
17080 if ((infile = vim_tempname('i')) == NULL)
17081 {
17082 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017083 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017084 }
17085
17086 fd = mch_fopen((char *)infile, WRITEBIN);
17087 if (fd == NULL)
17088 {
17089 EMSG2(_(e_notopen), infile);
17090 goto done;
17091 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017092 p = get_tv_string_buf_chk(&argvars[1], buf);
17093 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017094 {
17095 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017096 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017097 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017098 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17099 err = TRUE;
17100 if (fclose(fd) != 0)
17101 err = TRUE;
17102 if (err)
17103 {
17104 EMSG(_("E677: Error writing temp file"));
17105 goto done;
17106 }
17107 }
17108
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017109 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17110 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017111
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112#ifdef USE_CR
17113 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017114 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115 {
17116 char_u *s;
17117
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017118 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119 {
17120 if (*s == CAR)
17121 *s = NL;
17122 }
17123 }
17124#else
17125# ifdef USE_CRNL
17126 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017127 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017128 {
17129 char_u *s, *d;
17130
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017131 d = res;
17132 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017133 {
17134 if (s[0] == CAR && s[1] == NL)
17135 ++s;
17136 *d++ = *s;
17137 }
17138 *d = NUL;
17139 }
17140# endif
17141#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017142
17143done:
17144 if (infile != NULL)
17145 {
17146 mch_remove(infile);
17147 vim_free(infile);
17148 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017149 rettv->v_type = VAR_STRING;
17150 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151}
17152
17153/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017154 * "tabpagebuflist()" function
17155 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017156 static void
17157f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017158 typval_T *argvars UNUSED;
17159 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017160{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017161#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017162 tabpage_T *tp;
17163 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017164
17165 if (argvars[0].v_type == VAR_UNKNOWN)
17166 wp = firstwin;
17167 else
17168 {
17169 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17170 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017171 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017172 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017173 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017174 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017175 for (; wp != NULL; wp = wp->w_next)
17176 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017177 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017178 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017179 }
17180#endif
17181}
17182
17183
17184/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017185 * "tabpagenr()" function
17186 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017187 static void
17188f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017189 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017190 typval_T *rettv;
17191{
17192 int nr = 1;
17193#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017194 char_u *arg;
17195
17196 if (argvars[0].v_type != VAR_UNKNOWN)
17197 {
17198 arg = get_tv_string_chk(&argvars[0]);
17199 nr = 0;
17200 if (arg != NULL)
17201 {
17202 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017203 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017204 else
17205 EMSG2(_(e_invexpr2), arg);
17206 }
17207 }
17208 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017209 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017210#endif
17211 rettv->vval.v_number = nr;
17212}
17213
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017214
17215#ifdef FEAT_WINDOWS
17216static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17217
17218/*
17219 * Common code for tabpagewinnr() and winnr().
17220 */
17221 static int
17222get_winnr(tp, argvar)
17223 tabpage_T *tp;
17224 typval_T *argvar;
17225{
17226 win_T *twin;
17227 int nr = 1;
17228 win_T *wp;
17229 char_u *arg;
17230
17231 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17232 if (argvar->v_type != VAR_UNKNOWN)
17233 {
17234 arg = get_tv_string_chk(argvar);
17235 if (arg == NULL)
17236 nr = 0; /* type error; errmsg already given */
17237 else if (STRCMP(arg, "$") == 0)
17238 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17239 else if (STRCMP(arg, "#") == 0)
17240 {
17241 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17242 if (twin == NULL)
17243 nr = 0;
17244 }
17245 else
17246 {
17247 EMSG2(_(e_invexpr2), arg);
17248 nr = 0;
17249 }
17250 }
17251
17252 if (nr > 0)
17253 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17254 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017255 {
17256 if (wp == NULL)
17257 {
17258 /* didn't find it in this tabpage */
17259 nr = 0;
17260 break;
17261 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017262 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017263 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017264 return nr;
17265}
17266#endif
17267
17268/*
17269 * "tabpagewinnr()" function
17270 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017271 static void
17272f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017273 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017274 typval_T *rettv;
17275{
17276 int nr = 1;
17277#ifdef FEAT_WINDOWS
17278 tabpage_T *tp;
17279
17280 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17281 if (tp == NULL)
17282 nr = 0;
17283 else
17284 nr = get_winnr(tp, &argvars[1]);
17285#endif
17286 rettv->vval.v_number = nr;
17287}
17288
17289
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017290/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017291 * "tagfiles()" function
17292 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017293 static void
17294f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017295 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017296 typval_T *rettv;
17297{
17298 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017299 tagname_T tn;
17300 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017301
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017302 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017303 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017304
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017305 for (first = TRUE; ; first = FALSE)
17306 if (get_tagfname(&tn, first, fname) == FAIL
17307 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017308 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017309 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017310}
17311
17312/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017313 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017314 */
17315 static void
17316f_taglist(argvars, rettv)
17317 typval_T *argvars;
17318 typval_T *rettv;
17319{
17320 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017321
17322 tag_pattern = get_tv_string(&argvars[0]);
17323
17324 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017325 if (*tag_pattern == NUL)
17326 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017327
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017328 if (rettv_list_alloc(rettv) == OK)
17329 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017330}
17331
17332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 * "tempname()" function
17334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017336f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017337 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339{
17340 static int x = 'A';
17341
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017342 rettv->v_type = VAR_STRING;
17343 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344
17345 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17346 * names. Skip 'I' and 'O', they are used for shell redirection. */
17347 do
17348 {
17349 if (x == 'Z')
17350 x = '0';
17351 else if (x == '9')
17352 x = 'A';
17353 else
17354 {
17355#ifdef EBCDIC
17356 if (x == 'I')
17357 x = 'J';
17358 else if (x == 'R')
17359 x = 'S';
17360 else
17361#endif
17362 ++x;
17363 }
17364 } while (x == 'I' || x == 'O');
17365}
17366
17367/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017368 * "test(list)" function: Just checking the walls...
17369 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017370 static void
17371f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017372 typval_T *argvars UNUSED;
17373 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017374{
17375 /* Used for unit testing. Change the code below to your liking. */
17376#if 0
17377 listitem_T *li;
17378 list_T *l;
17379 char_u *bad, *good;
17380
17381 if (argvars[0].v_type != VAR_LIST)
17382 return;
17383 l = argvars[0].vval.v_list;
17384 if (l == NULL)
17385 return;
17386 li = l->lv_first;
17387 if (li == NULL)
17388 return;
17389 bad = get_tv_string(&li->li_tv);
17390 li = li->li_next;
17391 if (li == NULL)
17392 return;
17393 good = get_tv_string(&li->li_tv);
17394 rettv->vval.v_number = test_edit_score(bad, good);
17395#endif
17396}
17397
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017398#ifdef FEAT_FLOAT
17399/*
17400 * "tan()" function
17401 */
17402 static void
17403f_tan(argvars, rettv)
17404 typval_T *argvars;
17405 typval_T *rettv;
17406{
17407 float_T f;
17408
17409 rettv->v_type = VAR_FLOAT;
17410 if (get_float_arg(argvars, &f) == OK)
17411 rettv->vval.v_float = tan(f);
17412 else
17413 rettv->vval.v_float = 0.0;
17414}
17415
17416/*
17417 * "tanh()" function
17418 */
17419 static void
17420f_tanh(argvars, rettv)
17421 typval_T *argvars;
17422 typval_T *rettv;
17423{
17424 float_T f;
17425
17426 rettv->v_type = VAR_FLOAT;
17427 if (get_float_arg(argvars, &f) == OK)
17428 rettv->vval.v_float = tanh(f);
17429 else
17430 rettv->vval.v_float = 0.0;
17431}
17432#endif
17433
Bram Moolenaard52d9742005-08-21 22:20:28 +000017434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435 * "tolower(string)" function
17436 */
17437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017438f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017439 typval_T *argvars;
17440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441{
17442 char_u *p;
17443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017444 p = vim_strsave(get_tv_string(&argvars[0]));
17445 rettv->v_type = VAR_STRING;
17446 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447
17448 if (p != NULL)
17449 while (*p != NUL)
17450 {
17451#ifdef FEAT_MBYTE
17452 int l;
17453
17454 if (enc_utf8)
17455 {
17456 int c, lc;
17457
17458 c = utf_ptr2char(p);
17459 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017460 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017461 /* TODO: reallocate string when byte count changes. */
17462 if (utf_char2len(lc) == l)
17463 utf_char2bytes(lc, p);
17464 p += l;
17465 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017466 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467 p += l; /* skip multi-byte character */
17468 else
17469#endif
17470 {
17471 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17472 ++p;
17473 }
17474 }
17475}
17476
17477/*
17478 * "toupper(string)" function
17479 */
17480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017481f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017482 typval_T *argvars;
17483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017485 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017486 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487}
17488
17489/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017490 * "tr(string, fromstr, tostr)" function
17491 */
17492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017493f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017494 typval_T *argvars;
17495 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017496{
17497 char_u *instr;
17498 char_u *fromstr;
17499 char_u *tostr;
17500 char_u *p;
17501#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017502 int inlen;
17503 int fromlen;
17504 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017505 int idx;
17506 char_u *cpstr;
17507 int cplen;
17508 int first = TRUE;
17509#endif
17510 char_u buf[NUMBUFLEN];
17511 char_u buf2[NUMBUFLEN];
17512 garray_T ga;
17513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017514 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017515 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17516 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017517
17518 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017519 rettv->v_type = VAR_STRING;
17520 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017521 if (fromstr == NULL || tostr == NULL)
17522 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017523 ga_init2(&ga, (int)sizeof(char), 80);
17524
17525#ifdef FEAT_MBYTE
17526 if (!has_mbyte)
17527#endif
17528 /* not multi-byte: fromstr and tostr must be the same length */
17529 if (STRLEN(fromstr) != STRLEN(tostr))
17530 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017531#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017532error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017533#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017534 EMSG2(_(e_invarg2), fromstr);
17535 ga_clear(&ga);
17536 return;
17537 }
17538
17539 /* fromstr and tostr have to contain the same number of chars */
17540 while (*instr != NUL)
17541 {
17542#ifdef FEAT_MBYTE
17543 if (has_mbyte)
17544 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017545 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017546 cpstr = instr;
17547 cplen = inlen;
17548 idx = 0;
17549 for (p = fromstr; *p != NUL; p += fromlen)
17550 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017551 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017552 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17553 {
17554 for (p = tostr; *p != NUL; p += tolen)
17555 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017556 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017557 if (idx-- == 0)
17558 {
17559 cplen = tolen;
17560 cpstr = p;
17561 break;
17562 }
17563 }
17564 if (*p == NUL) /* tostr is shorter than fromstr */
17565 goto error;
17566 break;
17567 }
17568 ++idx;
17569 }
17570
17571 if (first && cpstr == instr)
17572 {
17573 /* Check that fromstr and tostr have the same number of
17574 * (multi-byte) characters. Done only once when a character
17575 * of instr doesn't appear in fromstr. */
17576 first = FALSE;
17577 for (p = tostr; *p != NUL; p += tolen)
17578 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017579 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017580 --idx;
17581 }
17582 if (idx != 0)
17583 goto error;
17584 }
17585
17586 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017587 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017588 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017589
17590 instr += inlen;
17591 }
17592 else
17593#endif
17594 {
17595 /* When not using multi-byte chars we can do it faster. */
17596 p = vim_strchr(fromstr, *instr);
17597 if (p != NULL)
17598 ga_append(&ga, tostr[p - fromstr]);
17599 else
17600 ga_append(&ga, *instr);
17601 ++instr;
17602 }
17603 }
17604
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017605 /* add a terminating NUL */
17606 ga_grow(&ga, 1);
17607 ga_append(&ga, NUL);
17608
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017609 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017610}
17611
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017612#ifdef FEAT_FLOAT
17613/*
17614 * "trunc({float})" function
17615 */
17616 static void
17617f_trunc(argvars, rettv)
17618 typval_T *argvars;
17619 typval_T *rettv;
17620{
17621 float_T f;
17622
17623 rettv->v_type = VAR_FLOAT;
17624 if (get_float_arg(argvars, &f) == OK)
17625 /* trunc() is not in C90, use floor() or ceil() instead. */
17626 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17627 else
17628 rettv->vval.v_float = 0.0;
17629}
17630#endif
17631
Bram Moolenaar8299df92004-07-10 09:47:34 +000017632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633 * "type(expr)" function
17634 */
17635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017636f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017637 typval_T *argvars;
17638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017640 int n;
17641
17642 switch (argvars[0].v_type)
17643 {
17644 case VAR_NUMBER: n = 0; break;
17645 case VAR_STRING: n = 1; break;
17646 case VAR_FUNC: n = 2; break;
17647 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017648 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017649#ifdef FEAT_FLOAT
17650 case VAR_FLOAT: n = 5; break;
17651#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017652 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17653 }
17654 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655}
17656
17657/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017658 * "undofile(name)" function
17659 */
17660 static void
17661f_undofile(argvars, rettv)
17662 typval_T *argvars;
17663 typval_T *rettv;
17664{
17665 rettv->v_type = VAR_STRING;
17666#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017667 {
17668 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17669
17670 if (ffname != NULL)
17671 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17672 vim_free(ffname);
17673 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017674#else
17675 rettv->vval.v_string = NULL;
17676#endif
17677}
17678
17679/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017680 * "values(dict)" function
17681 */
17682 static void
17683f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017684 typval_T *argvars;
17685 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017686{
17687 dict_list(argvars, rettv, 1);
17688}
17689
17690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691 * "virtcol(string)" function
17692 */
17693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017694f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017695 typval_T *argvars;
17696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697{
17698 colnr_T vcol = 0;
17699 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017700 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017702 fp = var2fpos(&argvars[0], FALSE, &fnum);
17703 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17704 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 {
17706 getvvcol(curwin, fp, NULL, NULL, &vcol);
17707 ++vcol;
17708 }
17709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017710 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711}
17712
17713/*
17714 * "visualmode()" function
17715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017717f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017718 typval_T *argvars UNUSED;
17719 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720{
17721#ifdef FEAT_VISUAL
17722 char_u str[2];
17723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017724 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725 str[0] = curbuf->b_visual_mode_eval;
17726 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017727 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728
17729 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017730 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732#endif
17733}
17734
17735/*
17736 * "winbufnr(nr)" function
17737 */
17738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017739f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017740 typval_T *argvars;
17741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742{
17743 win_T *wp;
17744
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017745 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017747 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017749 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750}
17751
17752/*
17753 * "wincol()" function
17754 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017756f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017757 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759{
17760 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017761 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762}
17763
17764/*
17765 * "winheight(nr)" function
17766 */
17767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017768f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017769 typval_T *argvars;
17770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771{
17772 win_T *wp;
17773
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017774 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017775 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017776 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017778 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779}
17780
17781/*
17782 * "winline()" function
17783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017785f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017786 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788{
17789 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017790 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791}
17792
17793/*
17794 * "winnr()" function
17795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017797f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017798 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800{
17801 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017802
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017804 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017806 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807}
17808
17809/*
17810 * "winrestcmd()" function
17811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017813f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017814 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816{
17817#ifdef FEAT_WINDOWS
17818 win_T *wp;
17819 int winnr = 1;
17820 garray_T ga;
17821 char_u buf[50];
17822
17823 ga_init2(&ga, (int)sizeof(char), 70);
17824 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17825 {
17826 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17827 ga_concat(&ga, buf);
17828# ifdef FEAT_VERTSPLIT
17829 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17830 ga_concat(&ga, buf);
17831# endif
17832 ++winnr;
17833 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017834 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017836 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017838 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017840 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841}
17842
17843/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017844 * "winrestview()" function
17845 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017846 static void
17847f_winrestview(argvars, rettv)
17848 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017849 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017850{
17851 dict_T *dict;
17852
17853 if (argvars[0].v_type != VAR_DICT
17854 || (dict = argvars[0].vval.v_dict) == NULL)
17855 EMSG(_(e_invarg));
17856 else
17857 {
17858 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17859 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17860#ifdef FEAT_VIRTUALEDIT
17861 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17862#endif
17863 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017864 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017865
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017866 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017867#ifdef FEAT_DIFF
17868 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17869#endif
17870 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17871 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17872
17873 check_cursor();
17874 changed_cline_bef_curs();
17875 invalidate_botline();
17876 redraw_later(VALID);
17877
17878 if (curwin->w_topline == 0)
17879 curwin->w_topline = 1;
17880 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17881 curwin->w_topline = curbuf->b_ml.ml_line_count;
17882#ifdef FEAT_DIFF
17883 check_topfill(curwin, TRUE);
17884#endif
17885 }
17886}
17887
17888/*
17889 * "winsaveview()" function
17890 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017891 static void
17892f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017893 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017894 typval_T *rettv;
17895{
17896 dict_T *dict;
17897
17898 dict = dict_alloc();
17899 if (dict == NULL)
17900 return;
17901 rettv->v_type = VAR_DICT;
17902 rettv->vval.v_dict = dict;
17903 ++dict->dv_refcount;
17904
17905 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17906 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17907#ifdef FEAT_VIRTUALEDIT
17908 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17909#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017910 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017911 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17912
17913 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17914#ifdef FEAT_DIFF
17915 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17916#endif
17917 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17918 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17919}
17920
17921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922 * "winwidth(nr)" function
17923 */
17924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017925f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017926 typval_T *argvars;
17927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928{
17929 win_T *wp;
17930
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017931 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017933 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934 else
17935#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017936 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017937#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017938 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939#endif
17940}
17941
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017943 * "writefile()" function
17944 */
17945 static void
17946f_writefile(argvars, rettv)
17947 typval_T *argvars;
17948 typval_T *rettv;
17949{
17950 int binary = FALSE;
17951 char_u *fname;
17952 FILE *fd;
17953 listitem_T *li;
17954 char_u *s;
17955 int ret = 0;
17956 int c;
17957
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017958 if (check_restricted() || check_secure())
17959 return;
17960
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017961 if (argvars[0].v_type != VAR_LIST)
17962 {
17963 EMSG2(_(e_listarg), "writefile()");
17964 return;
17965 }
17966 if (argvars[0].vval.v_list == NULL)
17967 return;
17968
17969 if (argvars[2].v_type != VAR_UNKNOWN
17970 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17971 binary = TRUE;
17972
17973 /* Always open the file in binary mode, library functions have a mind of
17974 * their own about CR-LF conversion. */
17975 fname = get_tv_string(&argvars[1]);
17976 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17977 {
17978 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17979 ret = -1;
17980 }
17981 else
17982 {
17983 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17984 li = li->li_next)
17985 {
17986 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17987 {
17988 if (*s == '\n')
17989 c = putc(NUL, fd);
17990 else
17991 c = putc(*s, fd);
17992 if (c == EOF)
17993 {
17994 ret = -1;
17995 break;
17996 }
17997 }
17998 if (!binary || li->li_next != NULL)
17999 if (putc('\n', fd) == EOF)
18000 {
18001 ret = -1;
18002 break;
18003 }
18004 if (ret < 0)
18005 {
18006 EMSG(_(e_write));
18007 break;
18008 }
18009 }
18010 fclose(fd);
18011 }
18012
18013 rettv->vval.v_number = ret;
18014}
18015
18016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018018 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019 */
18020 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018021var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018022 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018023 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018024 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018026 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018028 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029
Bram Moolenaara5525202006-03-02 22:52:09 +000018030 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018031 if (varp->v_type == VAR_LIST)
18032 {
18033 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018034 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018035 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018036 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018037
18038 l = varp->vval.v_list;
18039 if (l == NULL)
18040 return NULL;
18041
18042 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018043 pos.lnum = list_find_nr(l, 0L, &error);
18044 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018045 return NULL; /* invalid line number */
18046
18047 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018048 pos.col = list_find_nr(l, 1L, &error);
18049 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018050 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018051 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018052
18053 /* We accept "$" for the column number: last column. */
18054 li = list_find(l, 1L);
18055 if (li != NULL && li->li_tv.v_type == VAR_STRING
18056 && li->li_tv.vval.v_string != NULL
18057 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18058 pos.col = len + 1;
18059
Bram Moolenaara5525202006-03-02 22:52:09 +000018060 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018061 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018062 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018063 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018064
Bram Moolenaara5525202006-03-02 22:52:09 +000018065#ifdef FEAT_VIRTUALEDIT
18066 /* Get the virtual offset. Defaults to zero. */
18067 pos.coladd = list_find_nr(l, 2L, &error);
18068 if (error)
18069 pos.coladd = 0;
18070#endif
18071
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018072 return &pos;
18073 }
18074
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018075 name = get_tv_string_chk(varp);
18076 if (name == NULL)
18077 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018078 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018080#ifdef FEAT_VISUAL
18081 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18082 {
18083 if (VIsual_active)
18084 return &VIsual;
18085 return &curwin->w_cursor;
18086 }
18087#endif
18088 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018090 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18092 return NULL;
18093 return pp;
18094 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018095
18096#ifdef FEAT_VIRTUALEDIT
18097 pos.coladd = 0;
18098#endif
18099
Bram Moolenaar477933c2007-07-17 14:32:23 +000018100 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018101 {
18102 pos.col = 0;
18103 if (name[1] == '0') /* "w0": first visible line */
18104 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018105 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018106 pos.lnum = curwin->w_topline;
18107 return &pos;
18108 }
18109 else if (name[1] == '$') /* "w$": last visible line */
18110 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018111 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018112 pos.lnum = curwin->w_botline - 1;
18113 return &pos;
18114 }
18115 }
18116 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018118 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119 {
18120 pos.lnum = curbuf->b_ml.ml_line_count;
18121 pos.col = 0;
18122 }
18123 else
18124 {
18125 pos.lnum = curwin->w_cursor.lnum;
18126 pos.col = (colnr_T)STRLEN(ml_get_curline());
18127 }
18128 return &pos;
18129 }
18130 return NULL;
18131}
18132
18133/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018134 * Convert list in "arg" into a position and optional file number.
18135 * When "fnump" is NULL there is no file number, only 3 items.
18136 * Note that the column is passed on as-is, the caller may want to decrement
18137 * it to use 1 for the first column.
18138 * Return FAIL when conversion is not possible, doesn't check the position for
18139 * validity.
18140 */
18141 static int
18142list2fpos(arg, posp, fnump)
18143 typval_T *arg;
18144 pos_T *posp;
18145 int *fnump;
18146{
18147 list_T *l = arg->vval.v_list;
18148 long i = 0;
18149 long n;
18150
Bram Moolenaarbde35262006-07-23 20:12:24 +000018151 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18152 * when "fnump" isn't NULL and "coladd" is optional. */
18153 if (arg->v_type != VAR_LIST
18154 || l == NULL
18155 || l->lv_len < (fnump == NULL ? 2 : 3)
18156 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018157 return FAIL;
18158
18159 if (fnump != NULL)
18160 {
18161 n = list_find_nr(l, i++, NULL); /* fnum */
18162 if (n < 0)
18163 return FAIL;
18164 if (n == 0)
18165 n = curbuf->b_fnum; /* current buffer */
18166 *fnump = n;
18167 }
18168
18169 n = list_find_nr(l, i++, NULL); /* lnum */
18170 if (n < 0)
18171 return FAIL;
18172 posp->lnum = n;
18173
18174 n = list_find_nr(l, i++, NULL); /* col */
18175 if (n < 0)
18176 return FAIL;
18177 posp->col = n;
18178
18179#ifdef FEAT_VIRTUALEDIT
18180 n = list_find_nr(l, i, NULL);
18181 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018182 posp->coladd = 0;
18183 else
18184 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018185#endif
18186
18187 return OK;
18188}
18189
18190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191 * Get the length of an environment variable name.
18192 * Advance "arg" to the first character after the name.
18193 * Return 0 for error.
18194 */
18195 static int
18196get_env_len(arg)
18197 char_u **arg;
18198{
18199 char_u *p;
18200 int len;
18201
18202 for (p = *arg; vim_isIDc(*p); ++p)
18203 ;
18204 if (p == *arg) /* no name found */
18205 return 0;
18206
18207 len = (int)(p - *arg);
18208 *arg = p;
18209 return len;
18210}
18211
18212/*
18213 * Get the length of the name of a function or internal variable.
18214 * "arg" is advanced to the first non-white character after the name.
18215 * Return 0 if something is wrong.
18216 */
18217 static int
18218get_id_len(arg)
18219 char_u **arg;
18220{
18221 char_u *p;
18222 int len;
18223
18224 /* Find the end of the name. */
18225 for (p = *arg; eval_isnamec(*p); ++p)
18226 ;
18227 if (p == *arg) /* no name found */
18228 return 0;
18229
18230 len = (int)(p - *arg);
18231 *arg = skipwhite(p);
18232
18233 return len;
18234}
18235
18236/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018237 * Get the length of the name of a variable or function.
18238 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018240 * Return -1 if curly braces expansion failed.
18241 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242 * If the name contains 'magic' {}'s, expand them and return the
18243 * expanded name in an allocated string via 'alias' - caller must free.
18244 */
18245 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018246get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 char_u **arg;
18248 char_u **alias;
18249 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018250 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251{
18252 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253 char_u *p;
18254 char_u *expr_start;
18255 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256
18257 *alias = NULL; /* default to no alias */
18258
18259 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18260 && (*arg)[2] == (int)KE_SNR)
18261 {
18262 /* hard coded <SNR>, already translated */
18263 *arg += 3;
18264 return get_id_len(arg) + 3;
18265 }
18266 len = eval_fname_script(*arg);
18267 if (len > 0)
18268 {
18269 /* literal "<SID>", "s:" or "<SNR>" */
18270 *arg += len;
18271 }
18272
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018274 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018276 p = find_name_end(*arg, &expr_start, &expr_end,
18277 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278 if (expr_start != NULL)
18279 {
18280 char_u *temp_string;
18281
18282 if (!evaluate)
18283 {
18284 len += (int)(p - *arg);
18285 *arg = skipwhite(p);
18286 return len;
18287 }
18288
18289 /*
18290 * Include any <SID> etc in the expanded string:
18291 * Thus the -len here.
18292 */
18293 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18294 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018295 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296 *alias = temp_string;
18297 *arg = skipwhite(p);
18298 return (int)STRLEN(temp_string);
18299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300
18301 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018302 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303 EMSG2(_(e_invexpr2), *arg);
18304
18305 return len;
18306}
18307
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018308/*
18309 * Find the end of a variable or function name, taking care of magic braces.
18310 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18311 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018312 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018313 * Return a pointer to just after the name. Equal to "arg" if there is no
18314 * valid name.
18315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018317find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318 char_u *arg;
18319 char_u **expr_start;
18320 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018321 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018323 int mb_nest = 0;
18324 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325 char_u *p;
18326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018327 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018329 *expr_start = NULL;
18330 *expr_end = NULL;
18331 }
18332
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018333 /* Quick check for valid starting character. */
18334 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18335 return arg;
18336
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018337 for (p = arg; *p != NUL
18338 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018339 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018340 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018341 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018342 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018343 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018344 if (*p == '\'')
18345 {
18346 /* skip over 'string' to avoid counting [ and ] inside it. */
18347 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18348 ;
18349 if (*p == NUL)
18350 break;
18351 }
18352 else if (*p == '"')
18353 {
18354 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18355 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18356 if (*p == '\\' && p[1] != NUL)
18357 ++p;
18358 if (*p == NUL)
18359 break;
18360 }
18361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018362 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018364 if (*p == '[')
18365 ++br_nest;
18366 else if (*p == ']')
18367 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018368 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018369
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018370 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018372 if (*p == '{')
18373 {
18374 mb_nest++;
18375 if (expr_start != NULL && *expr_start == NULL)
18376 *expr_start = p;
18377 }
18378 else if (*p == '}')
18379 {
18380 mb_nest--;
18381 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18382 *expr_end = p;
18383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385 }
18386
18387 return p;
18388}
18389
18390/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018391 * Expands out the 'magic' {}'s in a variable/function name.
18392 * Note that this can call itself recursively, to deal with
18393 * constructs like foo{bar}{baz}{bam}
18394 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18395 * "in_start" ^
18396 * "expr_start" ^
18397 * "expr_end" ^
18398 * "in_end" ^
18399 *
18400 * Returns a new allocated string, which the caller must free.
18401 * Returns NULL for failure.
18402 */
18403 static char_u *
18404make_expanded_name(in_start, expr_start, expr_end, in_end)
18405 char_u *in_start;
18406 char_u *expr_start;
18407 char_u *expr_end;
18408 char_u *in_end;
18409{
18410 char_u c1;
18411 char_u *retval = NULL;
18412 char_u *temp_result;
18413 char_u *nextcmd = NULL;
18414
18415 if (expr_end == NULL || in_end == NULL)
18416 return NULL;
18417 *expr_start = NUL;
18418 *expr_end = NUL;
18419 c1 = *in_end;
18420 *in_end = NUL;
18421
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018422 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018423 if (temp_result != NULL && nextcmd == NULL)
18424 {
18425 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18426 + (in_end - expr_end) + 1));
18427 if (retval != NULL)
18428 {
18429 STRCPY(retval, in_start);
18430 STRCAT(retval, temp_result);
18431 STRCAT(retval, expr_end + 1);
18432 }
18433 }
18434 vim_free(temp_result);
18435
18436 *in_end = c1; /* put char back for error messages */
18437 *expr_start = '{';
18438 *expr_end = '}';
18439
18440 if (retval != NULL)
18441 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018442 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018443 if (expr_start != NULL)
18444 {
18445 /* Further expansion! */
18446 temp_result = make_expanded_name(retval, expr_start,
18447 expr_end, temp_result);
18448 vim_free(retval);
18449 retval = temp_result;
18450 }
18451 }
18452
18453 return retval;
18454}
18455
18456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018458 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459 */
18460 static int
18461eval_isnamec(c)
18462 int c;
18463{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018464 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18465}
18466
18467/*
18468 * Return TRUE if character "c" can be used as the first character in a
18469 * variable or function name (excluding '{' and '}').
18470 */
18471 static int
18472eval_isnamec1(c)
18473 int c;
18474{
18475 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476}
18477
18478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479 * Set number v: variable to "val".
18480 */
18481 void
18482set_vim_var_nr(idx, val)
18483 int idx;
18484 long val;
18485{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018486 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487}
18488
18489/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018490 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491 */
18492 long
18493get_vim_var_nr(idx)
18494 int idx;
18495{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018496 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018497}
18498
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018499/*
18500 * Get string v: variable value. Uses a static buffer, can only be used once.
18501 */
18502 char_u *
18503get_vim_var_str(idx)
18504 int idx;
18505{
18506 return get_tv_string(&vimvars[idx].vv_tv);
18507}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018508
Bram Moolenaar071d4272004-06-13 20:20:40 +000018509/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018510 * Get List v: variable value. Caller must take care of reference count when
18511 * needed.
18512 */
18513 list_T *
18514get_vim_var_list(idx)
18515 int idx;
18516{
18517 return vimvars[idx].vv_list;
18518}
18519
18520/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018521 * Set v:char to character "c".
18522 */
18523 void
18524set_vim_var_char(c)
18525 int c;
18526{
18527#ifdef FEAT_MBYTE
18528 char_u buf[MB_MAXBYTES];
18529#else
18530 char_u buf[2];
18531#endif
18532
18533#ifdef FEAT_MBYTE
18534 if (has_mbyte)
18535 buf[(*mb_char2bytes)(c, buf)] = NUL;
18536 else
18537#endif
18538 {
18539 buf[0] = c;
18540 buf[1] = NUL;
18541 }
18542 set_vim_var_string(VV_CHAR, buf, -1);
18543}
18544
18545/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018546 * Set v:count to "count" and v:count1 to "count1".
18547 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548 */
18549 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018550set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551 long count;
18552 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018553 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018555 if (set_prevcount)
18556 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018557 vimvars[VV_COUNT].vv_nr = count;
18558 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559}
18560
18561/*
18562 * Set string v: variable to a copy of "val".
18563 */
18564 void
18565set_vim_var_string(idx, val, len)
18566 int idx;
18567 char_u *val;
18568 int len; /* length of "val" to use or -1 (whole string) */
18569{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018570 /* Need to do this (at least) once, since we can't initialize a union.
18571 * Will always be invoked when "v:progname" is set. */
18572 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18573
Bram Moolenaare9a41262005-01-15 22:18:47 +000018574 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018576 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018578 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018580 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581}
18582
18583/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018584 * Set List v: variable to "val".
18585 */
18586 void
18587set_vim_var_list(idx, val)
18588 int idx;
18589 list_T *val;
18590{
18591 list_unref(vimvars[idx].vv_list);
18592 vimvars[idx].vv_list = val;
18593 if (val != NULL)
18594 ++val->lv_refcount;
18595}
18596
18597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 * Set v:register if needed.
18599 */
18600 void
18601set_reg_var(c)
18602 int c;
18603{
18604 char_u regname;
18605
18606 if (c == 0 || c == ' ')
18607 regname = '"';
18608 else
18609 regname = c;
18610 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018611 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612 set_vim_var_string(VV_REG, &regname, 1);
18613}
18614
18615/*
18616 * Get or set v:exception. If "oldval" == NULL, return the current value.
18617 * Otherwise, restore the value to "oldval" and return NULL.
18618 * Must always be called in pairs to save and restore v:exception! Does not
18619 * take care of memory allocations.
18620 */
18621 char_u *
18622v_exception(oldval)
18623 char_u *oldval;
18624{
18625 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018626 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627
Bram Moolenaare9a41262005-01-15 22:18:47 +000018628 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629 return NULL;
18630}
18631
18632/*
18633 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18634 * Otherwise, restore the value to "oldval" and return NULL.
18635 * Must always be called in pairs to save and restore v:throwpoint! Does not
18636 * take care of memory allocations.
18637 */
18638 char_u *
18639v_throwpoint(oldval)
18640 char_u *oldval;
18641{
18642 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018643 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644
Bram Moolenaare9a41262005-01-15 22:18:47 +000018645 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646 return NULL;
18647}
18648
18649#if defined(FEAT_AUTOCMD) || defined(PROTO)
18650/*
18651 * Set v:cmdarg.
18652 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18653 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18654 * Must always be called in pairs!
18655 */
18656 char_u *
18657set_cmdarg(eap, oldarg)
18658 exarg_T *eap;
18659 char_u *oldarg;
18660{
18661 char_u *oldval;
18662 char_u *newval;
18663 unsigned len;
18664
Bram Moolenaare9a41262005-01-15 22:18:47 +000018665 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018666 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018668 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018669 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018670 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671 }
18672
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018673 if (eap->force_bin == FORCE_BIN)
18674 len = 6;
18675 else if (eap->force_bin == FORCE_NOBIN)
18676 len = 8;
18677 else
18678 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018679
18680 if (eap->read_edit)
18681 len += 7;
18682
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018683 if (eap->force_ff != 0)
18684 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18685# ifdef FEAT_MBYTE
18686 if (eap->force_enc != 0)
18687 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018688 if (eap->bad_char != 0)
18689 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018690# endif
18691
18692 newval = alloc(len + 1);
18693 if (newval == NULL)
18694 return NULL;
18695
18696 if (eap->force_bin == FORCE_BIN)
18697 sprintf((char *)newval, " ++bin");
18698 else if (eap->force_bin == FORCE_NOBIN)
18699 sprintf((char *)newval, " ++nobin");
18700 else
18701 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018702
18703 if (eap->read_edit)
18704 STRCAT(newval, " ++edit");
18705
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018706 if (eap->force_ff != 0)
18707 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18708 eap->cmd + eap->force_ff);
18709# ifdef FEAT_MBYTE
18710 if (eap->force_enc != 0)
18711 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18712 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018713 if (eap->bad_char == BAD_KEEP)
18714 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18715 else if (eap->bad_char == BAD_DROP)
18716 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18717 else if (eap->bad_char != 0)
18718 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018719# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018720 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018721 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722}
18723#endif
18724
18725/*
18726 * Get the value of internal variable "name".
18727 * Return OK or FAIL.
18728 */
18729 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018730get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731 char_u *name;
18732 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018733 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018734 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735{
18736 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018737 typval_T *tv = NULL;
18738 typval_T atv;
18739 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741
18742 /* truncate the name, so that we can use strcmp() */
18743 cc = name[len];
18744 name[len] = NUL;
18745
18746 /*
18747 * Check for "b:changedtick".
18748 */
18749 if (STRCMP(name, "b:changedtick") == 0)
18750 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018751 atv.v_type = VAR_NUMBER;
18752 atv.vval.v_number = curbuf->b_changedtick;
18753 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 }
18755
18756 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 * Check for user-defined variables.
18758 */
18759 else
18760 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018761 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018763 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764 }
18765
Bram Moolenaare9a41262005-01-15 22:18:47 +000018766 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018768 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018769 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770 ret = FAIL;
18771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018772 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018773 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774
18775 name[len] = cc;
18776
18777 return ret;
18778}
18779
18780/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018781 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18782 * Also handle function call with Funcref variable: func(expr)
18783 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18784 */
18785 static int
18786handle_subscript(arg, rettv, evaluate, verbose)
18787 char_u **arg;
18788 typval_T *rettv;
18789 int evaluate; /* do more than finding the end */
18790 int verbose; /* give error messages */
18791{
18792 int ret = OK;
18793 dict_T *selfdict = NULL;
18794 char_u *s;
18795 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018796 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018797
18798 while (ret == OK
18799 && (**arg == '['
18800 || (**arg == '.' && rettv->v_type == VAR_DICT)
18801 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18802 && !vim_iswhite(*(*arg - 1)))
18803 {
18804 if (**arg == '(')
18805 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018806 /* need to copy the funcref so that we can clear rettv */
18807 functv = *rettv;
18808 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018809
18810 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018811 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018812 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018813 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18814 &len, evaluate, selfdict);
18815
18816 /* Clear the funcref afterwards, so that deleting it while
18817 * evaluating the arguments is possible (see test55). */
18818 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018819
18820 /* Stop the expression evaluation when immediately aborting on
18821 * error, or when an interrupt occurred or an exception was thrown
18822 * but not caught. */
18823 if (aborting())
18824 {
18825 if (ret == OK)
18826 clear_tv(rettv);
18827 ret = FAIL;
18828 }
18829 dict_unref(selfdict);
18830 selfdict = NULL;
18831 }
18832 else /* **arg == '[' || **arg == '.' */
18833 {
18834 dict_unref(selfdict);
18835 if (rettv->v_type == VAR_DICT)
18836 {
18837 selfdict = rettv->vval.v_dict;
18838 if (selfdict != NULL)
18839 ++selfdict->dv_refcount;
18840 }
18841 else
18842 selfdict = NULL;
18843 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18844 {
18845 clear_tv(rettv);
18846 ret = FAIL;
18847 }
18848 }
18849 }
18850 dict_unref(selfdict);
18851 return ret;
18852}
18853
18854/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018855 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018856 * value).
18857 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018858 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018859alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018860{
Bram Moolenaar33570922005-01-25 22:26:29 +000018861 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018862}
18863
18864/*
18865 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866 * The string "s" must have been allocated, it is consumed.
18867 * Return NULL for out of memory, the variable otherwise.
18868 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018869 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018870alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871 char_u *s;
18872{
Bram Moolenaar33570922005-01-25 22:26:29 +000018873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018875 rettv = alloc_tv();
18876 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018878 rettv->v_type = VAR_STRING;
18879 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880 }
18881 else
18882 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018883 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884}
18885
18886/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018887 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018889 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018890free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018891 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892{
18893 if (varp != NULL)
18894 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018895 switch (varp->v_type)
18896 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018897 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018898 func_unref(varp->vval.v_string);
18899 /*FALLTHROUGH*/
18900 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018901 vim_free(varp->vval.v_string);
18902 break;
18903 case VAR_LIST:
18904 list_unref(varp->vval.v_list);
18905 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018906 case VAR_DICT:
18907 dict_unref(varp->vval.v_dict);
18908 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018909 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018910#ifdef FEAT_FLOAT
18911 case VAR_FLOAT:
18912#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018913 case VAR_UNKNOWN:
18914 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018915 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018916 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018917 break;
18918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018919 vim_free(varp);
18920 }
18921}
18922
18923/*
18924 * Free the memory for a variable value and set the value to NULL or 0.
18925 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018926 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018927clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018928 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929{
18930 if (varp != NULL)
18931 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018932 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018934 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018935 func_unref(varp->vval.v_string);
18936 /*FALLTHROUGH*/
18937 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018938 vim_free(varp->vval.v_string);
18939 varp->vval.v_string = NULL;
18940 break;
18941 case VAR_LIST:
18942 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018943 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018944 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018945 case VAR_DICT:
18946 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018947 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018948 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018949 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018950 varp->vval.v_number = 0;
18951 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018952#ifdef FEAT_FLOAT
18953 case VAR_FLOAT:
18954 varp->vval.v_float = 0.0;
18955 break;
18956#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018957 case VAR_UNKNOWN:
18958 break;
18959 default:
18960 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018962 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963 }
18964}
18965
18966/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018967 * Set the value of a variable to NULL without freeing items.
18968 */
18969 static void
18970init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018971 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018972{
18973 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018974 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018975}
18976
18977/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978 * Get the number value of a variable.
18979 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018980 * For incompatible types, return 0.
18981 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18982 * caller of incompatible types: it sets *denote to TRUE if "denote"
18983 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984 */
18985 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018986get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018987 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018989 int error = FALSE;
18990
18991 return get_tv_number_chk(varp, &error); /* return 0L on error */
18992}
18993
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018994 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018995get_tv_number_chk(varp, denote)
18996 typval_T *varp;
18997 int *denote;
18998{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018999 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019001 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019003 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019004 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019005#ifdef FEAT_FLOAT
19006 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019007 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019008 break;
19009#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019010 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019011 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019012 break;
19013 case VAR_STRING:
19014 if (varp->vval.v_string != NULL)
19015 vim_str2nr(varp->vval.v_string, NULL, NULL,
19016 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019017 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019018 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019019 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019020 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019021 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019022 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019023 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019024 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019025 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019026 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019027 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019028 if (denote == NULL) /* useful for values that must be unsigned */
19029 n = -1;
19030 else
19031 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019032 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033}
19034
19035/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019036 * Get the lnum from the first argument.
19037 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019038 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039 */
19040 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019041get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019042 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043{
Bram Moolenaar33570922005-01-25 22:26:29 +000019044 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045 linenr_T lnum;
19046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019047 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048 if (lnum == 0) /* no valid number, try using line() */
19049 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019050 rettv.v_type = VAR_NUMBER;
19051 f_line(argvars, &rettv);
19052 lnum = rettv.vval.v_number;
19053 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054 }
19055 return lnum;
19056}
19057
19058/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019059 * Get the lnum from the first argument.
19060 * Also accepts "$", then "buf" is used.
19061 * Returns 0 on error.
19062 */
19063 static linenr_T
19064get_tv_lnum_buf(argvars, buf)
19065 typval_T *argvars;
19066 buf_T *buf;
19067{
19068 if (argvars[0].v_type == VAR_STRING
19069 && argvars[0].vval.v_string != NULL
19070 && argvars[0].vval.v_string[0] == '$'
19071 && buf != NULL)
19072 return buf->b_ml.ml_line_count;
19073 return get_tv_number_chk(&argvars[0], NULL);
19074}
19075
19076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077 * Get the string value of a variable.
19078 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019079 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19080 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 * If the String variable has never been set, return an empty string.
19082 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019083 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19084 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 */
19086 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019087get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019088 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089{
19090 static char_u mybuf[NUMBUFLEN];
19091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019092 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093}
19094
19095 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019096get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019097 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019098 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019100 char_u *res = get_tv_string_buf_chk(varp, buf);
19101
19102 return res != NULL ? res : (char_u *)"";
19103}
19104
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019105 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019106get_tv_string_chk(varp)
19107 typval_T *varp;
19108{
19109 static char_u mybuf[NUMBUFLEN];
19110
19111 return get_tv_string_buf_chk(varp, mybuf);
19112}
19113
19114 static char_u *
19115get_tv_string_buf_chk(varp, buf)
19116 typval_T *varp;
19117 char_u *buf;
19118{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019119 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019121 case VAR_NUMBER:
19122 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19123 return buf;
19124 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019125 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019126 break;
19127 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019128 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019129 break;
19130 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019131 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019132 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019133#ifdef FEAT_FLOAT
19134 case VAR_FLOAT:
19135 EMSG(_("E806: using Float as a String"));
19136 break;
19137#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019138 case VAR_STRING:
19139 if (varp->vval.v_string != NULL)
19140 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019141 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019142 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019143 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019144 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019146 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147}
19148
19149/*
19150 * Find variable "name" in the list of variables.
19151 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019152 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019153 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019154 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019156 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019157find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019159 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019162 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163
Bram Moolenaara7043832005-01-21 11:56:39 +000019164 ht = find_var_ht(name, &varname);
19165 if (htp != NULL)
19166 *htp = ht;
19167 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019169 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170}
19171
19172/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019173 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019174 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019176 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019177find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019178 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019179 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019180 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019181{
Bram Moolenaar33570922005-01-25 22:26:29 +000019182 hashitem_T *hi;
19183
19184 if (*varname == NUL)
19185 {
19186 /* Must be something like "s:", otherwise "ht" would be NULL. */
19187 switch (varname[-2])
19188 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019189 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019190 case 'g': return &globvars_var;
19191 case 'v': return &vimvars_var;
19192 case 'b': return &curbuf->b_bufvar;
19193 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019194#ifdef FEAT_WINDOWS
19195 case 't': return &curtab->tp_winvar;
19196#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019197 case 'l': return current_funccal == NULL
19198 ? NULL : &current_funccal->l_vars_var;
19199 case 'a': return current_funccal == NULL
19200 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019201 }
19202 return NULL;
19203 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019204
19205 hi = hash_find(ht, varname);
19206 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019207 {
19208 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019209 * worked find the variable again. Don't auto-load a script if it was
19210 * loaded already, otherwise it would be loaded every time when
19211 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019212 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019213 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019214 hi = hash_find(ht, varname);
19215 if (HASHITEM_EMPTY(hi))
19216 return NULL;
19217 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019218 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019219}
19220
19221/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019222 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019223 * Set "varname" to the start of name without ':'.
19224 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019225 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019226find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227 char_u *name;
19228 char_u **varname;
19229{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019230 hashitem_T *hi;
19231
Bram Moolenaar071d4272004-06-13 20:20:40 +000019232 if (name[1] != ':')
19233 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019234 /* The name must not start with a colon or #. */
19235 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019236 return NULL;
19237 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019238
19239 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019240 hi = hash_find(&compat_hashtab, name);
19241 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019242 return &compat_hashtab;
19243
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019245 return &globvarht; /* global variable */
19246 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247 }
19248 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019249 if (*name == 'g') /* global variable */
19250 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019251 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19252 */
19253 if (vim_strchr(name + 2, ':') != NULL
19254 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019255 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019257 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019258 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019259 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019260#ifdef FEAT_WINDOWS
19261 if (*name == 't') /* tab page variable */
19262 return &curtab->tp_vars.dv_hashtab;
19263#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019264 if (*name == 'v') /* v: variable */
19265 return &vimvarht;
19266 if (*name == 'a' && current_funccal != NULL) /* function argument */
19267 return &current_funccal->l_avars.dv_hashtab;
19268 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19269 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270 if (*name == 's' /* script variable */
19271 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19272 return &SCRIPT_VARS(current_SID);
19273 return NULL;
19274}
19275
19276/*
19277 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019278 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279 * Returns NULL when it doesn't exist.
19280 */
19281 char_u *
19282get_var_value(name)
19283 char_u *name;
19284{
Bram Moolenaar33570922005-01-25 22:26:29 +000019285 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286
Bram Moolenaara7043832005-01-21 11:56:39 +000019287 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288 if (v == NULL)
19289 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019290 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291}
19292
19293/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019294 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295 * sourcing this script and when executing functions defined in the script.
19296 */
19297 void
19298new_script_vars(id)
19299 scid_T id;
19300{
Bram Moolenaara7043832005-01-21 11:56:39 +000019301 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019302 hashtab_T *ht;
19303 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019304
Bram Moolenaar071d4272004-06-13 20:20:40 +000019305 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19306 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019307 /* Re-allocating ga_data means that an ht_array pointing to
19308 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019309 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019310 for (i = 1; i <= ga_scripts.ga_len; ++i)
19311 {
19312 ht = &SCRIPT_VARS(i);
19313 if (ht->ht_mask == HT_INIT_SIZE - 1)
19314 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019315 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019316 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019317 }
19318
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319 while (ga_scripts.ga_len < id)
19320 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019321 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019322 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019323 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019325 }
19326 }
19327}
19328
19329/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019330 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19331 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332 */
19333 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019334init_var_dict(dict, dict_var)
19335 dict_T *dict;
19336 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337{
Bram Moolenaar33570922005-01-25 22:26:29 +000019338 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019339 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019340 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019341 dict_var->di_tv.vval.v_dict = dict;
19342 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019343 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019344 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19345 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346}
19347
19348/*
19349 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019350 * Frees all allocated variables and the value they contain.
19351 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019352 */
19353 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019354vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019355 hashtab_T *ht;
19356{
19357 vars_clear_ext(ht, TRUE);
19358}
19359
19360/*
19361 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19362 */
19363 static void
19364vars_clear_ext(ht, free_val)
19365 hashtab_T *ht;
19366 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367{
Bram Moolenaara7043832005-01-21 11:56:39 +000019368 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019369 hashitem_T *hi;
19370 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019371
Bram Moolenaar33570922005-01-25 22:26:29 +000019372 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019373 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019374 for (hi = ht->ht_array; todo > 0; ++hi)
19375 {
19376 if (!HASHITEM_EMPTY(hi))
19377 {
19378 --todo;
19379
Bram Moolenaar33570922005-01-25 22:26:29 +000019380 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019381 * ht_array might change then. hash_clear() takes care of it
19382 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019383 v = HI2DI(hi);
19384 if (free_val)
19385 clear_tv(&v->di_tv);
19386 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19387 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019388 }
19389 }
19390 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019391 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392}
19393
Bram Moolenaara7043832005-01-21 11:56:39 +000019394/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019395 * Delete a variable from hashtab "ht" at item "hi".
19396 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019399delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019400 hashtab_T *ht;
19401 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402{
Bram Moolenaar33570922005-01-25 22:26:29 +000019403 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019404
19405 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019406 clear_tv(&di->di_tv);
19407 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408}
19409
19410/*
19411 * List the value of one internal variable.
19412 */
19413 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019414list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019415 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019416 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019417 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019418{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019419 char_u *tofree;
19420 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019421 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019422
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019423 current_copyID += COPYID_INC;
19424 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019425 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019426 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019427 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019428}
19429
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019431list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432 char_u *prefix;
19433 char_u *name;
19434 int type;
19435 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019436 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437{
Bram Moolenaar31859182007-08-14 20:41:13 +000019438 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19439 msg_start();
19440 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441 if (name != NULL) /* "a:" vars don't have a name stored */
19442 msg_puts(name);
19443 msg_putchar(' ');
19444 msg_advance(22);
19445 if (type == VAR_NUMBER)
19446 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019447 else if (type == VAR_FUNC)
19448 msg_putchar('*');
19449 else if (type == VAR_LIST)
19450 {
19451 msg_putchar('[');
19452 if (*string == '[')
19453 ++string;
19454 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019455 else if (type == VAR_DICT)
19456 {
19457 msg_putchar('{');
19458 if (*string == '{')
19459 ++string;
19460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461 else
19462 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019463
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019465
19466 if (type == VAR_FUNC)
19467 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019468 if (*first)
19469 {
19470 msg_clr_eos();
19471 *first = FALSE;
19472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473}
19474
19475/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019476 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477 * If the variable already exists, the value is updated.
19478 * Otherwise the variable is created.
19479 */
19480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019481set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019483 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019484 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485{
Bram Moolenaar33570922005-01-25 22:26:29 +000019486 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019488 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019489 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019491 ht = find_var_ht(name, &varname);
19492 if (ht == NULL || *varname == NUL)
19493 {
19494 EMSG2(_(e_illvar), name);
19495 return;
19496 }
19497 v = find_var_in_ht(ht, varname, TRUE);
19498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019499 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019500 {
19501 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19502 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19503 ? name[2] : name[0]))
19504 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019505 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019506 return;
19507 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019508 /* Don't allow hiding a function. When "v" is not NULL we migth be
19509 * assigning another function to the same var, the type is checked
19510 * below. */
19511 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019512 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019513 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019514 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019515 return;
19516 }
19517 }
19518
Bram Moolenaar33570922005-01-25 22:26:29 +000019519 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019521 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019522 if (var_check_ro(v->di_flags, name)
19523 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019524 return;
19525 if (v->di_tv.v_type != tv->v_type
19526 && !((v->di_tv.v_type == VAR_STRING
19527 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019528 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019529 || tv->v_type == VAR_NUMBER))
19530#ifdef FEAT_FLOAT
19531 && !((v->di_tv.v_type == VAR_NUMBER
19532 || v->di_tv.v_type == VAR_FLOAT)
19533 && (tv->v_type == VAR_NUMBER
19534 || tv->v_type == VAR_FLOAT))
19535#endif
19536 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019537 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019538 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019539 return;
19540 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019541
19542 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019543 * Handle setting internal v: variables separately: we don't change
19544 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019545 */
19546 if (ht == &vimvarht)
19547 {
19548 if (v->di_tv.v_type == VAR_STRING)
19549 {
19550 vim_free(v->di_tv.vval.v_string);
19551 if (copy || tv->v_type != VAR_STRING)
19552 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19553 else
19554 {
19555 /* Take over the string to avoid an extra alloc/free. */
19556 v->di_tv.vval.v_string = tv->vval.v_string;
19557 tv->vval.v_string = NULL;
19558 }
19559 }
19560 else if (v->di_tv.v_type != VAR_NUMBER)
19561 EMSG2(_(e_intern2), "set_var()");
19562 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019563 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019564 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019565 if (STRCMP(varname, "searchforward") == 0)
19566 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19567 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019568 return;
19569 }
19570
19571 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572 }
19573 else /* add a new variable */
19574 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019575 /* Can't add "v:" variable. */
19576 if (ht == &vimvarht)
19577 {
19578 EMSG2(_(e_illvar), name);
19579 return;
19580 }
19581
Bram Moolenaar92124a32005-06-17 22:03:40 +000019582 /* Make sure the variable name is valid. */
19583 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019584 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19585 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019586 {
19587 EMSG2(_(e_illvar), varname);
19588 return;
19589 }
19590
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019591 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19592 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019593 if (v == NULL)
19594 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019595 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019596 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019598 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599 return;
19600 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019601 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019603
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019604 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019605 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019606 else
19607 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019608 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019609 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019610 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612}
19613
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019614/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019615 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019616 * Also give an error message.
19617 */
19618 static int
19619var_check_ro(flags, name)
19620 int flags;
19621 char_u *name;
19622{
19623 if (flags & DI_FLAGS_RO)
19624 {
19625 EMSG2(_(e_readonlyvar), name);
19626 return TRUE;
19627 }
19628 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19629 {
19630 EMSG2(_(e_readonlysbx), name);
19631 return TRUE;
19632 }
19633 return FALSE;
19634}
19635
19636/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019637 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19638 * Also give an error message.
19639 */
19640 static int
19641var_check_fixed(flags, name)
19642 int flags;
19643 char_u *name;
19644{
19645 if (flags & DI_FLAGS_FIX)
19646 {
19647 EMSG2(_("E795: Cannot delete variable %s"), name);
19648 return TRUE;
19649 }
19650 return FALSE;
19651}
19652
19653/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019654 * Return TRUE if typeval "tv" is set to be locked (immutable).
19655 * Also give an error message, using "name".
19656 */
19657 static int
19658tv_check_lock(lock, name)
19659 int lock;
19660 char_u *name;
19661{
19662 if (lock & VAR_LOCKED)
19663 {
19664 EMSG2(_("E741: Value is locked: %s"),
19665 name == NULL ? (char_u *)_("Unknown") : name);
19666 return TRUE;
19667 }
19668 if (lock & VAR_FIXED)
19669 {
19670 EMSG2(_("E742: Cannot change value of %s"),
19671 name == NULL ? (char_u *)_("Unknown") : name);
19672 return TRUE;
19673 }
19674 return FALSE;
19675}
19676
19677/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019678 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019679 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019680 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019681 * It is OK for "from" and "to" to point to the same item. This is used to
19682 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019683 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019684 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019685copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019686 typval_T *from;
19687 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019689 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019690 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019691 switch (from->v_type)
19692 {
19693 case VAR_NUMBER:
19694 to->vval.v_number = from->vval.v_number;
19695 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019696#ifdef FEAT_FLOAT
19697 case VAR_FLOAT:
19698 to->vval.v_float = from->vval.v_float;
19699 break;
19700#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019701 case VAR_STRING:
19702 case VAR_FUNC:
19703 if (from->vval.v_string == NULL)
19704 to->vval.v_string = NULL;
19705 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019706 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019707 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019708 if (from->v_type == VAR_FUNC)
19709 func_ref(to->vval.v_string);
19710 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019711 break;
19712 case VAR_LIST:
19713 if (from->vval.v_list == NULL)
19714 to->vval.v_list = NULL;
19715 else
19716 {
19717 to->vval.v_list = from->vval.v_list;
19718 ++to->vval.v_list->lv_refcount;
19719 }
19720 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019721 case VAR_DICT:
19722 if (from->vval.v_dict == NULL)
19723 to->vval.v_dict = NULL;
19724 else
19725 {
19726 to->vval.v_dict = from->vval.v_dict;
19727 ++to->vval.v_dict->dv_refcount;
19728 }
19729 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019730 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019731 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019732 break;
19733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734}
19735
19736/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019737 * Make a copy of an item.
19738 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019739 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19740 * reference to an already copied list/dict can be used.
19741 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019742 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019743 static int
19744item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019745 typval_T *from;
19746 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019747 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019748 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019749{
19750 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019751 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019752
Bram Moolenaar33570922005-01-25 22:26:29 +000019753 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019754 {
19755 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019756 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019757 }
19758 ++recurse;
19759
19760 switch (from->v_type)
19761 {
19762 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019763#ifdef FEAT_FLOAT
19764 case VAR_FLOAT:
19765#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019766 case VAR_STRING:
19767 case VAR_FUNC:
19768 copy_tv(from, to);
19769 break;
19770 case VAR_LIST:
19771 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019772 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019773 if (from->vval.v_list == NULL)
19774 to->vval.v_list = NULL;
19775 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19776 {
19777 /* use the copy made earlier */
19778 to->vval.v_list = from->vval.v_list->lv_copylist;
19779 ++to->vval.v_list->lv_refcount;
19780 }
19781 else
19782 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19783 if (to->vval.v_list == NULL)
19784 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019785 break;
19786 case VAR_DICT:
19787 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019788 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019789 if (from->vval.v_dict == NULL)
19790 to->vval.v_dict = NULL;
19791 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19792 {
19793 /* use the copy made earlier */
19794 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19795 ++to->vval.v_dict->dv_refcount;
19796 }
19797 else
19798 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19799 if (to->vval.v_dict == NULL)
19800 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019801 break;
19802 default:
19803 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019804 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019805 }
19806 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019807 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019808}
19809
19810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811 * ":echo expr1 ..." print each argument separated with a space, add a
19812 * newline at the end.
19813 * ":echon expr1 ..." print each argument plain.
19814 */
19815 void
19816ex_echo(eap)
19817 exarg_T *eap;
19818{
19819 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019820 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019821 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822 char_u *p;
19823 int needclr = TRUE;
19824 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019825 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826
19827 if (eap->skip)
19828 ++emsg_skip;
19829 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19830 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019831 /* If eval1() causes an error message the text from the command may
19832 * still need to be cleared. E.g., "echo 22,44". */
19833 need_clr_eos = needclr;
19834
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019836 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837 {
19838 /*
19839 * Report the invalid expression unless the expression evaluation
19840 * has been cancelled due to an aborting error, an interrupt, or an
19841 * exception.
19842 */
19843 if (!aborting())
19844 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019845 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846 break;
19847 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019848 need_clr_eos = FALSE;
19849
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850 if (!eap->skip)
19851 {
19852 if (atstart)
19853 {
19854 atstart = FALSE;
19855 /* Call msg_start() after eval1(), evaluating the expression
19856 * may cause a message to appear. */
19857 if (eap->cmdidx == CMD_echo)
19858 msg_start();
19859 }
19860 else if (eap->cmdidx == CMD_echo)
19861 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019862 current_copyID += COPYID_INC;
19863 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019864 if (p != NULL)
19865 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019867 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019868 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019869 if (*p != TAB && needclr)
19870 {
19871 /* remove any text still there from the command */
19872 msg_clr_eos();
19873 needclr = FALSE;
19874 }
19875 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876 }
19877 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019878 {
19879#ifdef FEAT_MBYTE
19880 if (has_mbyte)
19881 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019882 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019883
19884 (void)msg_outtrans_len_attr(p, i, echo_attr);
19885 p += i - 1;
19886 }
19887 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019888#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019889 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019892 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019893 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019894 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895 arg = skipwhite(arg);
19896 }
19897 eap->nextcmd = check_nextcmd(arg);
19898
19899 if (eap->skip)
19900 --emsg_skip;
19901 else
19902 {
19903 /* remove text that may still be there from the command */
19904 if (needclr)
19905 msg_clr_eos();
19906 if (eap->cmdidx == CMD_echo)
19907 msg_end();
19908 }
19909}
19910
19911/*
19912 * ":echohl {name}".
19913 */
19914 void
19915ex_echohl(eap)
19916 exarg_T *eap;
19917{
19918 int id;
19919
19920 id = syn_name2id(eap->arg);
19921 if (id == 0)
19922 echo_attr = 0;
19923 else
19924 echo_attr = syn_id2attr(id);
19925}
19926
19927/*
19928 * ":execute expr1 ..." execute the result of an expression.
19929 * ":echomsg expr1 ..." Print a message
19930 * ":echoerr expr1 ..." Print an error
19931 * Each gets spaces around each argument and a newline at the end for
19932 * echo commands
19933 */
19934 void
19935ex_execute(eap)
19936 exarg_T *eap;
19937{
19938 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019939 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940 int ret = OK;
19941 char_u *p;
19942 garray_T ga;
19943 int len;
19944 int save_did_emsg;
19945
19946 ga_init2(&ga, 1, 80);
19947
19948 if (eap->skip)
19949 ++emsg_skip;
19950 while (*arg != NUL && *arg != '|' && *arg != '\n')
19951 {
19952 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019953 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019954 {
19955 /*
19956 * Report the invalid expression unless the expression evaluation
19957 * has been cancelled due to an aborting error, an interrupt, or an
19958 * exception.
19959 */
19960 if (!aborting())
19961 EMSG2(_(e_invexpr2), p);
19962 ret = FAIL;
19963 break;
19964 }
19965
19966 if (!eap->skip)
19967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019968 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969 len = (int)STRLEN(p);
19970 if (ga_grow(&ga, len + 2) == FAIL)
19971 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019972 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973 ret = FAIL;
19974 break;
19975 }
19976 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979 ga.ga_len += len;
19980 }
19981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019982 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983 arg = skipwhite(arg);
19984 }
19985
19986 if (ret != FAIL && ga.ga_data != NULL)
19987 {
19988 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019989 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019991 out_flush();
19992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993 else if (eap->cmdidx == CMD_echoerr)
19994 {
19995 /* We don't want to abort following commands, restore did_emsg. */
19996 save_did_emsg = did_emsg;
19997 EMSG((char_u *)ga.ga_data);
19998 if (!force_abort)
19999 did_emsg = save_did_emsg;
20000 }
20001 else if (eap->cmdidx == CMD_execute)
20002 do_cmdline((char_u *)ga.ga_data,
20003 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20004 }
20005
20006 ga_clear(&ga);
20007
20008 if (eap->skip)
20009 --emsg_skip;
20010
20011 eap->nextcmd = check_nextcmd(arg);
20012}
20013
20014/*
20015 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20016 * "arg" points to the "&" or '+' when called, to "option" when returning.
20017 * Returns NULL when no option name found. Otherwise pointer to the char
20018 * after the option name.
20019 */
20020 static char_u *
20021find_option_end(arg, opt_flags)
20022 char_u **arg;
20023 int *opt_flags;
20024{
20025 char_u *p = *arg;
20026
20027 ++p;
20028 if (*p == 'g' && p[1] == ':')
20029 {
20030 *opt_flags = OPT_GLOBAL;
20031 p += 2;
20032 }
20033 else if (*p == 'l' && p[1] == ':')
20034 {
20035 *opt_flags = OPT_LOCAL;
20036 p += 2;
20037 }
20038 else
20039 *opt_flags = 0;
20040
20041 if (!ASCII_ISALPHA(*p))
20042 return NULL;
20043 *arg = p;
20044
20045 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20046 p += 4; /* termcap option */
20047 else
20048 while (ASCII_ISALPHA(*p))
20049 ++p;
20050 return p;
20051}
20052
20053/*
20054 * ":function"
20055 */
20056 void
20057ex_function(eap)
20058 exarg_T *eap;
20059{
20060 char_u *theline;
20061 int j;
20062 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 char_u *name = NULL;
20065 char_u *p;
20066 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020067 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068 garray_T newargs;
20069 garray_T newlines;
20070 int varargs = FALSE;
20071 int mustend = FALSE;
20072 int flags = 0;
20073 ufunc_T *fp;
20074 int indent;
20075 int nesting;
20076 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020077 dictitem_T *v;
20078 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020079 static int func_nr = 0; /* number for nameless function */
20080 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020081 hashtab_T *ht;
20082 int todo;
20083 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020084 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085
20086 /*
20087 * ":function" without argument: list functions.
20088 */
20089 if (ends_excmd(*eap->arg))
20090 {
20091 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020092 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020093 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020094 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020095 {
20096 if (!HASHITEM_EMPTY(hi))
20097 {
20098 --todo;
20099 fp = HI2UF(hi);
20100 if (!isdigit(*fp->uf_name))
20101 list_func_head(fp, FALSE);
20102 }
20103 }
20104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 eap->nextcmd = check_nextcmd(eap->arg);
20106 return;
20107 }
20108
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020109 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020110 * ":function /pat": list functions matching pattern.
20111 */
20112 if (*eap->arg == '/')
20113 {
20114 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20115 if (!eap->skip)
20116 {
20117 regmatch_T regmatch;
20118
20119 c = *p;
20120 *p = NUL;
20121 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20122 *p = c;
20123 if (regmatch.regprog != NULL)
20124 {
20125 regmatch.rm_ic = p_ic;
20126
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020127 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020128 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20129 {
20130 if (!HASHITEM_EMPTY(hi))
20131 {
20132 --todo;
20133 fp = HI2UF(hi);
20134 if (!isdigit(*fp->uf_name)
20135 && vim_regexec(&regmatch, fp->uf_name, 0))
20136 list_func_head(fp, FALSE);
20137 }
20138 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020139 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020140 }
20141 }
20142 if (*p == '/')
20143 ++p;
20144 eap->nextcmd = check_nextcmd(p);
20145 return;
20146 }
20147
20148 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020149 * Get the function name. There are these situations:
20150 * func normal function name
20151 * "name" == func, "fudi.fd_dict" == NULL
20152 * dict.func new dictionary entry
20153 * "name" == NULL, "fudi.fd_dict" set,
20154 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20155 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020156 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020157 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20158 * dict.func existing dict entry that's not a Funcref
20159 * "name" == NULL, "fudi.fd_dict" set,
20160 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020163 name = trans_function_name(&p, eap->skip, 0, &fudi);
20164 paren = (vim_strchr(p, '(') != NULL);
20165 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166 {
20167 /*
20168 * Return on an invalid expression in braces, unless the expression
20169 * evaluation has been cancelled due to an aborting error, an
20170 * interrupt, or an exception.
20171 */
20172 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020173 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020174 if (!eap->skip && fudi.fd_newkey != NULL)
20175 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020176 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179 else
20180 eap->skip = TRUE;
20181 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020182
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183 /* An error in a function call during evaluation of an expression in magic
20184 * braces should not cause the function not to be defined. */
20185 saved_did_emsg = did_emsg;
20186 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187
20188 /*
20189 * ":function func" with only function name: list function.
20190 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020191 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192 {
20193 if (!ends_excmd(*skipwhite(p)))
20194 {
20195 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020196 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197 }
20198 eap->nextcmd = check_nextcmd(p);
20199 if (eap->nextcmd != NULL)
20200 *p = NUL;
20201 if (!eap->skip && !got_int)
20202 {
20203 fp = find_func(name);
20204 if (fp != NULL)
20205 {
20206 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020207 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020209 if (FUNCLINE(fp, j) == NULL)
20210 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 msg_putchar('\n');
20212 msg_outnum((long)(j + 1));
20213 if (j < 9)
20214 msg_putchar(' ');
20215 if (j < 99)
20216 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020217 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218 out_flush(); /* show a line at a time */
20219 ui_breakcheck();
20220 }
20221 if (!got_int)
20222 {
20223 msg_putchar('\n');
20224 msg_puts((char_u *)" endfunction");
20225 }
20226 }
20227 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020228 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020230 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231 }
20232
20233 /*
20234 * ":function name(arg1, arg2)" Define function.
20235 */
20236 p = skipwhite(p);
20237 if (*p != '(')
20238 {
20239 if (!eap->skip)
20240 {
20241 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020242 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243 }
20244 /* attempt to continue by skipping some text */
20245 if (vim_strchr(p, '(') != NULL)
20246 p = vim_strchr(p, '(');
20247 }
20248 p = skipwhite(p + 1);
20249
20250 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20251 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20252
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020253 if (!eap->skip)
20254 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020255 /* Check the name of the function. Unless it's a dictionary function
20256 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020257 if (name != NULL)
20258 arg = name;
20259 else
20260 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020261 if (arg != NULL && (fudi.fd_di == NULL
20262 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020263 {
20264 if (*arg == K_SPECIAL)
20265 j = 3;
20266 else
20267 j = 0;
20268 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20269 : eval_isnamec(arg[j])))
20270 ++j;
20271 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020272 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020273 }
20274 }
20275
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 /*
20277 * Isolate the arguments: "arg1, arg2, ...)"
20278 */
20279 while (*p != ')')
20280 {
20281 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20282 {
20283 varargs = TRUE;
20284 p += 3;
20285 mustend = TRUE;
20286 }
20287 else
20288 {
20289 arg = p;
20290 while (ASCII_ISALNUM(*p) || *p == '_')
20291 ++p;
20292 if (arg == p || isdigit(*arg)
20293 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20294 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20295 {
20296 if (!eap->skip)
20297 EMSG2(_("E125: Illegal argument: %s"), arg);
20298 break;
20299 }
20300 if (ga_grow(&newargs, 1) == FAIL)
20301 goto erret;
20302 c = *p;
20303 *p = NUL;
20304 arg = vim_strsave(arg);
20305 if (arg == NULL)
20306 goto erret;
20307 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20308 *p = c;
20309 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 if (*p == ',')
20311 ++p;
20312 else
20313 mustend = TRUE;
20314 }
20315 p = skipwhite(p);
20316 if (mustend && *p != ')')
20317 {
20318 if (!eap->skip)
20319 EMSG2(_(e_invarg2), eap->arg);
20320 break;
20321 }
20322 }
20323 ++p; /* skip the ')' */
20324
Bram Moolenaare9a41262005-01-15 22:18:47 +000020325 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326 for (;;)
20327 {
20328 p = skipwhite(p);
20329 if (STRNCMP(p, "range", 5) == 0)
20330 {
20331 flags |= FC_RANGE;
20332 p += 5;
20333 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020334 else if (STRNCMP(p, "dict", 4) == 0)
20335 {
20336 flags |= FC_DICT;
20337 p += 4;
20338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339 else if (STRNCMP(p, "abort", 5) == 0)
20340 {
20341 flags |= FC_ABORT;
20342 p += 5;
20343 }
20344 else
20345 break;
20346 }
20347
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020348 /* When there is a line break use what follows for the function body.
20349 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20350 if (*p == '\n')
20351 line_arg = p + 1;
20352 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353 EMSG(_(e_trailing));
20354
20355 /*
20356 * Read the body of the function, until ":endfunction" is found.
20357 */
20358 if (KeyTyped)
20359 {
20360 /* Check if the function already exists, don't let the user type the
20361 * whole function before telling him it doesn't work! For a script we
20362 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020363 if (!eap->skip && !eap->forceit)
20364 {
20365 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20366 EMSG(_(e_funcdict));
20367 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020368 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020371 if (!eap->skip && did_emsg)
20372 goto erret;
20373
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 msg_putchar('\n'); /* don't overwrite the function name */
20375 cmdline_row = msg_row;
20376 }
20377
20378 indent = 2;
20379 nesting = 0;
20380 for (;;)
20381 {
20382 msg_scroll = TRUE;
20383 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020384 sourcing_lnum_off = sourcing_lnum;
20385
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020386 if (line_arg != NULL)
20387 {
20388 /* Use eap->arg, split up in parts by line breaks. */
20389 theline = line_arg;
20390 p = vim_strchr(theline, '\n');
20391 if (p == NULL)
20392 line_arg += STRLEN(line_arg);
20393 else
20394 {
20395 *p = NUL;
20396 line_arg = p + 1;
20397 }
20398 }
20399 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400 theline = getcmdline(':', 0L, indent);
20401 else
20402 theline = eap->getline(':', eap->cookie, indent);
20403 if (KeyTyped)
20404 lines_left = Rows - 1;
20405 if (theline == NULL)
20406 {
20407 EMSG(_("E126: Missing :endfunction"));
20408 goto erret;
20409 }
20410
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020411 /* Detect line continuation: sourcing_lnum increased more than one. */
20412 if (sourcing_lnum > sourcing_lnum_off + 1)
20413 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20414 else
20415 sourcing_lnum_off = 0;
20416
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417 if (skip_until != NULL)
20418 {
20419 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20420 * don't check for ":endfunc". */
20421 if (STRCMP(theline, skip_until) == 0)
20422 {
20423 vim_free(skip_until);
20424 skip_until = NULL;
20425 }
20426 }
20427 else
20428 {
20429 /* skip ':' and blanks*/
20430 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20431 ;
20432
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020433 /* Check for "endfunction". */
20434 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020436 if (line_arg == NULL)
20437 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020438 break;
20439 }
20440
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020441 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442 * at "end". */
20443 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20444 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020445 else if (STRNCMP(p, "if", 2) == 0
20446 || STRNCMP(p, "wh", 2) == 0
20447 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448 || STRNCMP(p, "try", 3) == 0)
20449 indent += 2;
20450
20451 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020452 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020454 if (*p == '!')
20455 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456 p += eval_fname_script(p);
20457 if (ASCII_ISALPHA(*p))
20458 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020459 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460 if (*skipwhite(p) == '(')
20461 {
20462 ++nesting;
20463 indent += 2;
20464 }
20465 }
20466 }
20467
20468 /* Check for ":append" or ":insert". */
20469 p = skip_range(p, NULL);
20470 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20471 || (p[0] == 'i'
20472 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20473 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20474 skip_until = vim_strsave((char_u *)".");
20475
20476 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20477 arg = skipwhite(skiptowhite(p));
20478 if (arg[0] == '<' && arg[1] =='<'
20479 && ((p[0] == 'p' && p[1] == 'y'
20480 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20481 || (p[0] == 'p' && p[1] == 'e'
20482 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20483 || (p[0] == 't' && p[1] == 'c'
20484 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20485 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20486 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020487 || (p[0] == 'm' && p[1] == 'z'
20488 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489 ))
20490 {
20491 /* ":python <<" continues until a dot, like ":append" */
20492 p = skipwhite(arg + 2);
20493 if (*p == NUL)
20494 skip_until = vim_strsave((char_u *)".");
20495 else
20496 skip_until = vim_strsave(p);
20497 }
20498 }
20499
20500 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020501 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020502 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020503 if (line_arg == NULL)
20504 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020506 }
20507
20508 /* Copy the line to newly allocated memory. get_one_sourceline()
20509 * allocates 250 bytes per line, this saves 80% on average. The cost
20510 * is an extra alloc/free. */
20511 p = vim_strsave(theline);
20512 if (p != NULL)
20513 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020514 if (line_arg == NULL)
20515 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020516 theline = p;
20517 }
20518
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020519 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20520
20521 /* Add NULL lines for continuation lines, so that the line count is
20522 * equal to the index in the growarray. */
20523 while (sourcing_lnum_off-- > 0)
20524 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020525
20526 /* Check for end of eap->arg. */
20527 if (line_arg != NULL && *line_arg == NUL)
20528 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020529 }
20530
20531 /* Don't define the function when skipping commands or when an error was
20532 * detected. */
20533 if (eap->skip || did_emsg)
20534 goto erret;
20535
20536 /*
20537 * If there are no errors, add the function
20538 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020539 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020540 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020541 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020542 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020543 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020544 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020545 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020546 goto erret;
20547 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020548
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020549 fp = find_func(name);
20550 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020552 if (!eap->forceit)
20553 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020554 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020555 goto erret;
20556 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020557 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020558 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020559 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020560 name);
20561 goto erret;
20562 }
20563 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020564 ga_clear_strings(&(fp->uf_args));
20565 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020566 vim_free(name);
20567 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569 }
20570 else
20571 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020572 char numbuf[20];
20573
20574 fp = NULL;
20575 if (fudi.fd_newkey == NULL && !eap->forceit)
20576 {
20577 EMSG(_(e_funcdict));
20578 goto erret;
20579 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020580 if (fudi.fd_di == NULL)
20581 {
20582 /* Can't add a function to a locked dictionary */
20583 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20584 goto erret;
20585 }
20586 /* Can't change an existing function if it is locked */
20587 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20588 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020589
20590 /* Give the function a sequential number. Can only be used with a
20591 * Funcref! */
20592 vim_free(name);
20593 sprintf(numbuf, "%d", ++func_nr);
20594 name = vim_strsave((char_u *)numbuf);
20595 if (name == NULL)
20596 goto erret;
20597 }
20598
20599 if (fp == NULL)
20600 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020601 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020602 {
20603 int slen, plen;
20604 char_u *scriptname;
20605
20606 /* Check that the autoload name matches the script name. */
20607 j = FAIL;
20608 if (sourcing_name != NULL)
20609 {
20610 scriptname = autoload_name(name);
20611 if (scriptname != NULL)
20612 {
20613 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020614 plen = (int)STRLEN(p);
20615 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020616 if (slen > plen && fnamecmp(p,
20617 sourcing_name + slen - plen) == 0)
20618 j = OK;
20619 vim_free(scriptname);
20620 }
20621 }
20622 if (j == FAIL)
20623 {
20624 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20625 goto erret;
20626 }
20627 }
20628
20629 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630 if (fp == NULL)
20631 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020632
20633 if (fudi.fd_dict != NULL)
20634 {
20635 if (fudi.fd_di == NULL)
20636 {
20637 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020638 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020639 if (fudi.fd_di == NULL)
20640 {
20641 vim_free(fp);
20642 goto erret;
20643 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020644 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20645 {
20646 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020647 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020648 goto erret;
20649 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020650 }
20651 else
20652 /* overwrite existing dict entry */
20653 clear_tv(&fudi.fd_di->di_tv);
20654 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020655 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020656 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020657 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020658
20659 /* behave like "dict" was used */
20660 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020661 }
20662
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020664 STRCPY(fp->uf_name, name);
20665 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020666 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020667 fp->uf_args = newargs;
20668 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020669#ifdef FEAT_PROFILE
20670 fp->uf_tml_count = NULL;
20671 fp->uf_tml_total = NULL;
20672 fp->uf_tml_self = NULL;
20673 fp->uf_profiling = FALSE;
20674 if (prof_def_func())
20675 func_do_profile(fp);
20676#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020677 fp->uf_varargs = varargs;
20678 fp->uf_flags = flags;
20679 fp->uf_calls = 0;
20680 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020681 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682
20683erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684 ga_clear_strings(&newargs);
20685 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020686ret_free:
20687 vim_free(skip_until);
20688 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020690 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020691}
20692
20693/*
20694 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020695 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020696 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020697 * flags:
20698 * TFN_INT: internal function name OK
20699 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020700 * Advances "pp" to just after the function name (if no error).
20701 */
20702 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020703trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704 char_u **pp;
20705 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020706 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020707 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020709 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710 char_u *start;
20711 char_u *end;
20712 int lead;
20713 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020714 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020715 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020716
20717 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020718 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020719 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020720
20721 /* Check for hard coded <SNR>: already translated function ID (from a user
20722 * command). */
20723 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20724 && (*pp)[2] == (int)KE_SNR)
20725 {
20726 *pp += 3;
20727 len = get_id_len(pp) + 3;
20728 return vim_strnsave(start, len);
20729 }
20730
20731 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20732 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020733 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020734 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020736
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020737 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20738 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020739 if (end == start)
20740 {
20741 if (!skip)
20742 EMSG(_("E129: Function name required"));
20743 goto theend;
20744 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020745 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020746 {
20747 /*
20748 * Report an invalid expression in braces, unless the expression
20749 * evaluation has been cancelled due to an aborting error, an
20750 * interrupt, or an exception.
20751 */
20752 if (!aborting())
20753 {
20754 if (end != NULL)
20755 EMSG2(_(e_invarg2), start);
20756 }
20757 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020758 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020759 goto theend;
20760 }
20761
20762 if (lv.ll_tv != NULL)
20763 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020764 if (fdp != NULL)
20765 {
20766 fdp->fd_dict = lv.ll_dict;
20767 fdp->fd_newkey = lv.ll_newkey;
20768 lv.ll_newkey = NULL;
20769 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020770 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020771 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20772 {
20773 name = vim_strsave(lv.ll_tv->vval.v_string);
20774 *pp = end;
20775 }
20776 else
20777 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020778 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20779 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020780 EMSG(_(e_funcref));
20781 else
20782 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020783 name = NULL;
20784 }
20785 goto theend;
20786 }
20787
20788 if (lv.ll_name == NULL)
20789 {
20790 /* Error found, but continue after the function name. */
20791 *pp = end;
20792 goto theend;
20793 }
20794
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020795 /* Check if the name is a Funcref. If so, use the value. */
20796 if (lv.ll_exp_name != NULL)
20797 {
20798 len = (int)STRLEN(lv.ll_exp_name);
20799 name = deref_func_name(lv.ll_exp_name, &len);
20800 if (name == lv.ll_exp_name)
20801 name = NULL;
20802 }
20803 else
20804 {
20805 len = (int)(end - *pp);
20806 name = deref_func_name(*pp, &len);
20807 if (name == *pp)
20808 name = NULL;
20809 }
20810 if (name != NULL)
20811 {
20812 name = vim_strsave(name);
20813 *pp = end;
20814 goto theend;
20815 }
20816
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020817 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020818 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020819 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020820 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20821 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20822 {
20823 /* When there was "s:" already or the name expanded to get a
20824 * leading "s:" then remove it. */
20825 lv.ll_name += 2;
20826 len -= 2;
20827 lead = 2;
20828 }
20829 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020830 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020831 {
20832 if (lead == 2) /* skip over "s:" */
20833 lv.ll_name += 2;
20834 len = (int)(end - lv.ll_name);
20835 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020836
20837 /*
20838 * Copy the function name to allocated memory.
20839 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20840 * Accept <SNR>123_name() outside a script.
20841 */
20842 if (skip)
20843 lead = 0; /* do nothing */
20844 else if (lead > 0)
20845 {
20846 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020847 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20848 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020849 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020850 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020851 if (current_SID <= 0)
20852 {
20853 EMSG(_(e_usingsid));
20854 goto theend;
20855 }
20856 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20857 lead += (int)STRLEN(sid_buf);
20858 }
20859 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020860 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020861 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020862 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020863 goto theend;
20864 }
20865 name = alloc((unsigned)(len + lead + 1));
20866 if (name != NULL)
20867 {
20868 if (lead > 0)
20869 {
20870 name[0] = K_SPECIAL;
20871 name[1] = KS_EXTRA;
20872 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020873 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020874 STRCPY(name + 3, sid_buf);
20875 }
20876 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20877 name[len + lead] = NUL;
20878 }
20879 *pp = end;
20880
20881theend:
20882 clear_lval(&lv);
20883 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020884}
20885
20886/*
20887 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20888 * Return 2 if "p" starts with "s:".
20889 * Return 0 otherwise.
20890 */
20891 static int
20892eval_fname_script(p)
20893 char_u *p;
20894{
20895 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20896 || STRNICMP(p + 1, "SNR>", 4) == 0))
20897 return 5;
20898 if (p[0] == 's' && p[1] == ':')
20899 return 2;
20900 return 0;
20901}
20902
20903/*
20904 * Return TRUE if "p" starts with "<SID>" or "s:".
20905 * Only works if eval_fname_script() returned non-zero for "p"!
20906 */
20907 static int
20908eval_fname_sid(p)
20909 char_u *p;
20910{
20911 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20912}
20913
20914/*
20915 * List the head of the function: "name(arg1, arg2)".
20916 */
20917 static void
20918list_func_head(fp, indent)
20919 ufunc_T *fp;
20920 int indent;
20921{
20922 int j;
20923
20924 msg_start();
20925 if (indent)
20926 MSG_PUTS(" ");
20927 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020928 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929 {
20930 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020931 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932 }
20933 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020934 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020936 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937 {
20938 if (j)
20939 MSG_PUTS(", ");
20940 msg_puts(FUNCARG(fp, j));
20941 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020942 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943 {
20944 if (j)
20945 MSG_PUTS(", ");
20946 MSG_PUTS("...");
20947 }
20948 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020949 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020950 if (p_verbose > 0)
20951 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952}
20953
20954/*
20955 * Find a function by name, return pointer to it in ufuncs.
20956 * Return NULL for unknown function.
20957 */
20958 static ufunc_T *
20959find_func(name)
20960 char_u *name;
20961{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020962 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020964 hi = hash_find(&func_hashtab, name);
20965 if (!HASHITEM_EMPTY(hi))
20966 return HI2UF(hi);
20967 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968}
20969
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020970#if defined(EXITFREE) || defined(PROTO)
20971 void
20972free_all_functions()
20973{
20974 hashitem_T *hi;
20975
20976 /* Need to start all over every time, because func_free() may change the
20977 * hash table. */
20978 while (func_hashtab.ht_used > 0)
20979 for (hi = func_hashtab.ht_array; ; ++hi)
20980 if (!HASHITEM_EMPTY(hi))
20981 {
20982 func_free(HI2UF(hi));
20983 break;
20984 }
20985}
20986#endif
20987
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020988/*
20989 * Return TRUE if a function "name" exists.
20990 */
20991 static int
20992function_exists(name)
20993 char_u *name;
20994{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020995 char_u *nm = name;
20996 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020997 int n = FALSE;
20998
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020999 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021000 nm = skipwhite(nm);
21001
21002 /* Only accept "funcname", "funcname ", "funcname (..." and
21003 * "funcname(...", not "funcname!...". */
21004 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021005 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021006 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021007 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021008 else
21009 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021010 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021011 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021012 return n;
21013}
21014
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021015/*
21016 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021017 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021018 */
21019 static int
21020builtin_function(name)
21021 char_u *name;
21022{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021023 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21024 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021025}
21026
Bram Moolenaar05159a02005-02-26 23:04:13 +000021027#if defined(FEAT_PROFILE) || defined(PROTO)
21028/*
21029 * Start profiling function "fp".
21030 */
21031 static void
21032func_do_profile(fp)
21033 ufunc_T *fp;
21034{
21035 fp->uf_tm_count = 0;
21036 profile_zero(&fp->uf_tm_self);
21037 profile_zero(&fp->uf_tm_total);
21038 if (fp->uf_tml_count == NULL)
21039 fp->uf_tml_count = (int *)alloc_clear((unsigned)
21040 (sizeof(int) * fp->uf_lines.ga_len));
21041 if (fp->uf_tml_total == NULL)
21042 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
21043 (sizeof(proftime_T) * fp->uf_lines.ga_len));
21044 if (fp->uf_tml_self == NULL)
21045 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
21046 (sizeof(proftime_T) * fp->uf_lines.ga_len));
21047 fp->uf_tml_idx = -1;
21048 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21049 || fp->uf_tml_self == NULL)
21050 return; /* out of memory */
21051
21052 fp->uf_profiling = TRUE;
21053}
21054
21055/*
21056 * Dump the profiling results for all functions in file "fd".
21057 */
21058 void
21059func_dump_profile(fd)
21060 FILE *fd;
21061{
21062 hashitem_T *hi;
21063 int todo;
21064 ufunc_T *fp;
21065 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021066 ufunc_T **sorttab;
21067 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021068
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021069 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021070 if (todo == 0)
21071 return; /* nothing to dump */
21072
Bram Moolenaar73830342005-02-28 22:48:19 +000021073 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21074
Bram Moolenaar05159a02005-02-26 23:04:13 +000021075 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21076 {
21077 if (!HASHITEM_EMPTY(hi))
21078 {
21079 --todo;
21080 fp = HI2UF(hi);
21081 if (fp->uf_profiling)
21082 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021083 if (sorttab != NULL)
21084 sorttab[st_len++] = fp;
21085
Bram Moolenaar05159a02005-02-26 23:04:13 +000021086 if (fp->uf_name[0] == K_SPECIAL)
21087 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21088 else
21089 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21090 if (fp->uf_tm_count == 1)
21091 fprintf(fd, "Called 1 time\n");
21092 else
21093 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21094 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21095 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21096 fprintf(fd, "\n");
21097 fprintf(fd, "count total (s) self (s)\n");
21098
21099 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21100 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021101 if (FUNCLINE(fp, i) == NULL)
21102 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021103 prof_func_line(fd, fp->uf_tml_count[i],
21104 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021105 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21106 }
21107 fprintf(fd, "\n");
21108 }
21109 }
21110 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021111
21112 if (sorttab != NULL && st_len > 0)
21113 {
21114 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21115 prof_total_cmp);
21116 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21117 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21118 prof_self_cmp);
21119 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21120 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021121
21122 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021123}
Bram Moolenaar73830342005-02-28 22:48:19 +000021124
21125 static void
21126prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21127 FILE *fd;
21128 ufunc_T **sorttab;
21129 int st_len;
21130 char *title;
21131 int prefer_self; /* when equal print only self time */
21132{
21133 int i;
21134 ufunc_T *fp;
21135
21136 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21137 fprintf(fd, "count total (s) self (s) function\n");
21138 for (i = 0; i < 20 && i < st_len; ++i)
21139 {
21140 fp = sorttab[i];
21141 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21142 prefer_self);
21143 if (fp->uf_name[0] == K_SPECIAL)
21144 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21145 else
21146 fprintf(fd, " %s()\n", fp->uf_name);
21147 }
21148 fprintf(fd, "\n");
21149}
21150
21151/*
21152 * Print the count and times for one function or function line.
21153 */
21154 static void
21155prof_func_line(fd, count, total, self, prefer_self)
21156 FILE *fd;
21157 int count;
21158 proftime_T *total;
21159 proftime_T *self;
21160 int prefer_self; /* when equal print only self time */
21161{
21162 if (count > 0)
21163 {
21164 fprintf(fd, "%5d ", count);
21165 if (prefer_self && profile_equal(total, self))
21166 fprintf(fd, " ");
21167 else
21168 fprintf(fd, "%s ", profile_msg(total));
21169 if (!prefer_self && profile_equal(total, self))
21170 fprintf(fd, " ");
21171 else
21172 fprintf(fd, "%s ", profile_msg(self));
21173 }
21174 else
21175 fprintf(fd, " ");
21176}
21177
21178/*
21179 * Compare function for total time sorting.
21180 */
21181 static int
21182#ifdef __BORLANDC__
21183_RTLENTRYF
21184#endif
21185prof_total_cmp(s1, s2)
21186 const void *s1;
21187 const void *s2;
21188{
21189 ufunc_T *p1, *p2;
21190
21191 p1 = *(ufunc_T **)s1;
21192 p2 = *(ufunc_T **)s2;
21193 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21194}
21195
21196/*
21197 * Compare function for self time sorting.
21198 */
21199 static int
21200#ifdef __BORLANDC__
21201_RTLENTRYF
21202#endif
21203prof_self_cmp(s1, s2)
21204 const void *s1;
21205 const void *s2;
21206{
21207 ufunc_T *p1, *p2;
21208
21209 p1 = *(ufunc_T **)s1;
21210 p2 = *(ufunc_T **)s2;
21211 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21212}
21213
Bram Moolenaar05159a02005-02-26 23:04:13 +000021214#endif
21215
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021216/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021217 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021218 * Return TRUE if a package was loaded.
21219 */
21220 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021221script_autoload(name, reload)
21222 char_u *name;
21223 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021224{
21225 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021226 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021227 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021228 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021229
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021230 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021231 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021232 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021233 return FALSE;
21234
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021235 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021236
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021237 /* Find the name in the list of previously loaded package names. Skip
21238 * "autoload/", it's always the same. */
21239 for (i = 0; i < ga_loaded.ga_len; ++i)
21240 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21241 break;
21242 if (!reload && i < ga_loaded.ga_len)
21243 ret = FALSE; /* was loaded already */
21244 else
21245 {
21246 /* Remember the name if it wasn't loaded already. */
21247 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21248 {
21249 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21250 tofree = NULL;
21251 }
21252
21253 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021254 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021255 ret = TRUE;
21256 }
21257
21258 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021259 return ret;
21260}
21261
21262/*
21263 * Return the autoload script name for a function or variable name.
21264 * Returns NULL when out of memory.
21265 */
21266 static char_u *
21267autoload_name(name)
21268 char_u *name;
21269{
21270 char_u *p;
21271 char_u *scriptname;
21272
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021273 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021274 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21275 if (scriptname == NULL)
21276 return FALSE;
21277 STRCPY(scriptname, "autoload/");
21278 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021279 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021280 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021281 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021282 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021283 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021284}
21285
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21287
21288/*
21289 * Function given to ExpandGeneric() to obtain the list of user defined
21290 * function names.
21291 */
21292 char_u *
21293get_user_func_name(xp, idx)
21294 expand_T *xp;
21295 int idx;
21296{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021297 static long_u done;
21298 static hashitem_T *hi;
21299 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021300
21301 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021303 done = 0;
21304 hi = func_hashtab.ht_array;
21305 }
21306 if (done < func_hashtab.ht_used)
21307 {
21308 if (done++ > 0)
21309 ++hi;
21310 while (HASHITEM_EMPTY(hi))
21311 ++hi;
21312 fp = HI2UF(hi);
21313
21314 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21315 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316
21317 cat_func_name(IObuff, fp);
21318 if (xp->xp_context != EXPAND_USER_FUNC)
21319 {
21320 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021321 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322 STRCAT(IObuff, ")");
21323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324 return IObuff;
21325 }
21326 return NULL;
21327}
21328
21329#endif /* FEAT_CMDL_COMPL */
21330
21331/*
21332 * Copy the function name of "fp" to buffer "buf".
21333 * "buf" must be able to hold the function name plus three bytes.
21334 * Takes care of script-local function names.
21335 */
21336 static void
21337cat_func_name(buf, fp)
21338 char_u *buf;
21339 ufunc_T *fp;
21340{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021341 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342 {
21343 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021344 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345 }
21346 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021347 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348}
21349
21350/*
21351 * ":delfunction {name}"
21352 */
21353 void
21354ex_delfunction(eap)
21355 exarg_T *eap;
21356{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021357 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 char_u *p;
21359 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021360 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361
21362 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021363 name = trans_function_name(&p, eap->skip, 0, &fudi);
21364 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021366 {
21367 if (fudi.fd_dict != NULL && !eap->skip)
21368 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371 if (!ends_excmd(*skipwhite(p)))
21372 {
21373 vim_free(name);
21374 EMSG(_(e_trailing));
21375 return;
21376 }
21377 eap->nextcmd = check_nextcmd(p);
21378 if (eap->nextcmd != NULL)
21379 *p = NUL;
21380
21381 if (!eap->skip)
21382 fp = find_func(name);
21383 vim_free(name);
21384
21385 if (!eap->skip)
21386 {
21387 if (fp == NULL)
21388 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021389 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390 return;
21391 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021392 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393 {
21394 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21395 return;
21396 }
21397
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021398 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021400 /* Delete the dict item that refers to the function, it will
21401 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021402 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021404 else
21405 func_free(fp);
21406 }
21407}
21408
21409/*
21410 * Free a function and remove it from the list of functions.
21411 */
21412 static void
21413func_free(fp)
21414 ufunc_T *fp;
21415{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021416 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021417
21418 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021419 ga_clear_strings(&(fp->uf_args));
21420 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021421#ifdef FEAT_PROFILE
21422 vim_free(fp->uf_tml_count);
21423 vim_free(fp->uf_tml_total);
21424 vim_free(fp->uf_tml_self);
21425#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021426
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021427 /* remove the function from the function hashtable */
21428 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21429 if (HASHITEM_EMPTY(hi))
21430 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021431 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021432 hash_remove(&func_hashtab, hi);
21433
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021434 vim_free(fp);
21435}
21436
21437/*
21438 * Unreference a Function: decrement the reference count and free it when it
21439 * becomes zero. Only for numbered functions.
21440 */
21441 static void
21442func_unref(name)
21443 char_u *name;
21444{
21445 ufunc_T *fp;
21446
21447 if (name != NULL && isdigit(*name))
21448 {
21449 fp = find_func(name);
21450 if (fp == NULL)
21451 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021452 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021453 {
21454 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021455 * when "uf_calls" becomes zero. */
21456 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021457 func_free(fp);
21458 }
21459 }
21460}
21461
21462/*
21463 * Count a reference to a Function.
21464 */
21465 static void
21466func_ref(name)
21467 char_u *name;
21468{
21469 ufunc_T *fp;
21470
21471 if (name != NULL && isdigit(*name))
21472 {
21473 fp = find_func(name);
21474 if (fp == NULL)
21475 EMSG2(_(e_intern2), "func_ref()");
21476 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021477 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478 }
21479}
21480
21481/*
21482 * Call a user function.
21483 */
21484 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021485call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 ufunc_T *fp; /* pointer to function */
21487 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021488 typval_T *argvars; /* arguments */
21489 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490 linenr_T firstline; /* first line of range */
21491 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021492 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021493{
Bram Moolenaar33570922005-01-25 22:26:29 +000021494 char_u *save_sourcing_name;
21495 linenr_T save_sourcing_lnum;
21496 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021497 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021498 int save_did_emsg;
21499 static int depth = 0;
21500 dictitem_T *v;
21501 int fixvar_idx = 0; /* index in fixvar[] */
21502 int i;
21503 int ai;
21504 char_u numbuf[NUMBUFLEN];
21505 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021506#ifdef FEAT_PROFILE
21507 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021508 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021510
21511 /* If depth of calling is getting too high, don't execute the function */
21512 if (depth >= p_mfd)
21513 {
21514 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021515 rettv->v_type = VAR_NUMBER;
21516 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517 return;
21518 }
21519 ++depth;
21520
21521 line_breakcheck(); /* check for CTRL-C hit */
21522
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021523 fc = (funccall_T *)alloc(sizeof(funccall_T));
21524 fc->caller = current_funccal;
21525 current_funccal = fc;
21526 fc->func = fp;
21527 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021528 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021529 fc->linenr = 0;
21530 fc->returned = FALSE;
21531 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021533 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21534 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535
Bram Moolenaar33570922005-01-25 22:26:29 +000021536 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021537 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021538 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21539 * each argument variable and saves a lot of time.
21540 */
21541 /*
21542 * Init l: variables.
21543 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021544 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021545 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021546 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021547 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21548 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021549 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021550 name = v->di_key;
21551 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021552 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021553 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021554 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021555 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021556 v->di_tv.vval.v_dict = selfdict;
21557 ++selfdict->dv_refcount;
21558 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021559
Bram Moolenaar33570922005-01-25 22:26:29 +000021560 /*
21561 * Init a: variables.
21562 * Set a:0 to "argcount".
21563 * Set a:000 to a list with room for the "..." arguments.
21564 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021565 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21566 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021567 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021568 /* Use "name" to avoid a warning from some compiler that checks the
21569 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021570 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021571 name = v->di_key;
21572 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021573 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021574 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021575 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021576 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021577 v->di_tv.vval.v_list = &fc->l_varlist;
21578 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21579 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21580 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021581
21582 /*
21583 * Set a:firstline to "firstline" and a:lastline to "lastline".
21584 * Set a:name to named arguments.
21585 * Set a:N to the "..." arguments.
21586 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021587 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021588 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021589 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021590 (varnumber_T)lastline);
21591 for (i = 0; i < argcount; ++i)
21592 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021593 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021594 if (ai < 0)
21595 /* named argument a:name */
21596 name = FUNCARG(fp, i);
21597 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021598 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021599 /* "..." argument a:1, a:2, etc. */
21600 sprintf((char *)numbuf, "%d", ai + 1);
21601 name = numbuf;
21602 }
21603 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21604 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021605 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021606 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21607 }
21608 else
21609 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021610 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21611 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021612 if (v == NULL)
21613 break;
21614 v->di_flags = DI_FLAGS_RO;
21615 }
21616 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021617 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021618
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021619 /* Note: the values are copied directly to avoid alloc/free.
21620 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021621 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021622 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021623
21624 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21625 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021626 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21627 fc->l_listitems[ai].li_tv = argvars[i];
21628 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021629 }
21630 }
21631
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632 /* Don't redraw while executing the function. */
21633 ++RedrawingDisabled;
21634 save_sourcing_name = sourcing_name;
21635 save_sourcing_lnum = sourcing_lnum;
21636 sourcing_lnum = 1;
21637 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021638 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639 if (sourcing_name != NULL)
21640 {
21641 if (save_sourcing_name != NULL
21642 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21643 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21644 else
21645 STRCPY(sourcing_name, "function ");
21646 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21647
21648 if (p_verbose >= 12)
21649 {
21650 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021651 verbose_enter_scroll();
21652
Bram Moolenaar555b2802005-05-19 21:08:39 +000021653 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 if (p_verbose >= 14)
21655 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021657 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021658 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021659 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660
21661 msg_puts((char_u *)"(");
21662 for (i = 0; i < argcount; ++i)
21663 {
21664 if (i > 0)
21665 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021666 if (argvars[i].v_type == VAR_NUMBER)
21667 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668 else
21669 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021670 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21671 if (s != NULL)
21672 {
21673 trunc_string(s, buf, MSG_BUF_CLEN);
21674 msg_puts(buf);
21675 vim_free(tofree);
21676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677 }
21678 }
21679 msg_puts((char_u *)")");
21680 }
21681 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021682
21683 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021684 --no_wait_return;
21685 }
21686 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021687#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021688 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021689 {
21690 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21691 func_do_profile(fp);
21692 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021693 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021694 {
21695 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021696 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021697 profile_zero(&fp->uf_tm_children);
21698 }
21699 script_prof_save(&wait_start);
21700 }
21701#endif
21702
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021704 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021705 save_did_emsg = did_emsg;
21706 did_emsg = FALSE;
21707
21708 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021709 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21711
21712 --RedrawingDisabled;
21713
21714 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021715 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021717 clear_tv(rettv);
21718 rettv->v_type = VAR_NUMBER;
21719 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021720 }
21721
Bram Moolenaar05159a02005-02-26 23:04:13 +000021722#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021723 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021724 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021725 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021726 profile_end(&call_start);
21727 profile_sub_wait(&wait_start, &call_start);
21728 profile_add(&fp->uf_tm_total, &call_start);
21729 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021730 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021731 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021732 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21733 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021734 }
21735 }
21736#endif
21737
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738 /* when being verbose, mention the return value */
21739 if (p_verbose >= 12)
21740 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021741 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021742 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021745 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021746 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021747 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021748 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021749 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021751 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021752 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021753 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021754 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021755
Bram Moolenaar555b2802005-05-19 21:08:39 +000021756 /* The value may be very long. Skip the middle part, so that we
21757 * have some idea how it starts and ends. smsg() would always
21758 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021759 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021760 if (s != NULL)
21761 {
21762 trunc_string(s, buf, MSG_BUF_CLEN);
21763 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21764 vim_free(tofree);
21765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021766 }
21767 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021768
21769 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021770 --no_wait_return;
21771 }
21772
21773 vim_free(sourcing_name);
21774 sourcing_name = save_sourcing_name;
21775 sourcing_lnum = save_sourcing_lnum;
21776 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021777#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021778 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021779 script_prof_restore(&wait_start);
21780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781
21782 if (p_verbose >= 12 && sourcing_name != NULL)
21783 {
21784 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021785 verbose_enter_scroll();
21786
Bram Moolenaar555b2802005-05-19 21:08:39 +000021787 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021788 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021789
21790 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791 --no_wait_return;
21792 }
21793
21794 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021795 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021796 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021797
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021798 /* If the a:000 list and the l: and a: dicts are not referenced we can
21799 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021800 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21801 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21802 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21803 {
21804 free_funccal(fc, FALSE);
21805 }
21806 else
21807 {
21808 hashitem_T *hi;
21809 listitem_T *li;
21810 int todo;
21811
21812 /* "fc" is still in use. This can happen when returning "a:000" or
21813 * assigning "l:" to a global variable.
21814 * Link "fc" in the list for garbage collection later. */
21815 fc->caller = previous_funccal;
21816 previous_funccal = fc;
21817
21818 /* Make a copy of the a: variables, since we didn't do that above. */
21819 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21820 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21821 {
21822 if (!HASHITEM_EMPTY(hi))
21823 {
21824 --todo;
21825 v = HI2DI(hi);
21826 copy_tv(&v->di_tv, &v->di_tv);
21827 }
21828 }
21829
21830 /* Make a copy of the a:000 items, since we didn't do that above. */
21831 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21832 copy_tv(&li->li_tv, &li->li_tv);
21833 }
21834}
21835
21836/*
21837 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021838 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021839 */
21840 static int
21841can_free_funccal(fc, copyID)
21842 funccall_T *fc;
21843 int copyID;
21844{
21845 return (fc->l_varlist.lv_copyID != copyID
21846 && fc->l_vars.dv_copyID != copyID
21847 && fc->l_avars.dv_copyID != copyID);
21848}
21849
21850/*
21851 * Free "fc" and what it contains.
21852 */
21853 static void
21854free_funccal(fc, free_val)
21855 funccall_T *fc;
21856 int free_val; /* a: vars were allocated */
21857{
21858 listitem_T *li;
21859
21860 /* The a: variables typevals may not have been allocated, only free the
21861 * allocated variables. */
21862 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21863
21864 /* free all l: variables */
21865 vars_clear(&fc->l_vars.dv_hashtab);
21866
21867 /* Free the a:000 variables if they were allocated. */
21868 if (free_val)
21869 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21870 clear_tv(&li->li_tv);
21871
21872 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873}
21874
21875/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021876 * Add a number variable "name" to dict "dp" with value "nr".
21877 */
21878 static void
21879add_nr_var(dp, v, name, nr)
21880 dict_T *dp;
21881 dictitem_T *v;
21882 char *name;
21883 varnumber_T nr;
21884{
21885 STRCPY(v->di_key, name);
21886 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21887 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21888 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021889 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021890 v->di_tv.vval.v_number = nr;
21891}
21892
21893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894 * ":return [expr]"
21895 */
21896 void
21897ex_return(eap)
21898 exarg_T *eap;
21899{
21900 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021901 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902 int returning = FALSE;
21903
21904 if (current_funccal == NULL)
21905 {
21906 EMSG(_("E133: :return not inside a function"));
21907 return;
21908 }
21909
21910 if (eap->skip)
21911 ++emsg_skip;
21912
21913 eap->nextcmd = NULL;
21914 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021915 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916 {
21917 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021918 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021920 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 }
21922 /* It's safer to return also on error. */
21923 else if (!eap->skip)
21924 {
21925 /*
21926 * Return unless the expression evaluation has been cancelled due to an
21927 * aborting error, an interrupt, or an exception.
21928 */
21929 if (!aborting())
21930 returning = do_return(eap, FALSE, TRUE, NULL);
21931 }
21932
21933 /* When skipping or the return gets pending, advance to the next command
21934 * in this line (!returning). Otherwise, ignore the rest of the line.
21935 * Following lines will be ignored by get_func_line(). */
21936 if (returning)
21937 eap->nextcmd = NULL;
21938 else if (eap->nextcmd == NULL) /* no argument */
21939 eap->nextcmd = check_nextcmd(arg);
21940
21941 if (eap->skip)
21942 --emsg_skip;
21943}
21944
21945/*
21946 * Return from a function. Possibly makes the return pending. Also called
21947 * for a pending return at the ":endtry" or after returning from an extra
21948 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021949 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021950 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951 * FALSE when the return gets pending.
21952 */
21953 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021954do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955 exarg_T *eap;
21956 int reanimate;
21957 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021958 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959{
21960 int idx;
21961 struct condstack *cstack = eap->cstack;
21962
21963 if (reanimate)
21964 /* Undo the return. */
21965 current_funccal->returned = FALSE;
21966
21967 /*
21968 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21969 * not in its finally clause (which then is to be executed next) is found.
21970 * In this case, make the ":return" pending for execution at the ":endtry".
21971 * Otherwise, return normally.
21972 */
21973 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21974 if (idx >= 0)
21975 {
21976 cstack->cs_pending[idx] = CSTP_RETURN;
21977
21978 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021979 /* A pending return again gets pending. "rettv" points to an
21980 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021982 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983 else
21984 {
21985 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021986 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021988 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021990 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 {
21992 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021993 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021994 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995 else
21996 EMSG(_(e_outofmem));
21997 }
21998 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021999 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000
22001 if (reanimate)
22002 {
22003 /* The pending return value could be overwritten by a ":return"
22004 * without argument in a finally clause; reset the default
22005 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022006 current_funccal->rettv->v_type = VAR_NUMBER;
22007 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008 }
22009 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022010 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 }
22012 else
22013 {
22014 current_funccal->returned = TRUE;
22015
22016 /* If the return is carried out now, store the return value. For
22017 * a return immediately after reanimation, the value is already
22018 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022019 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022021 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022022 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022024 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025 }
22026 }
22027
22028 return idx < 0;
22029}
22030
22031/*
22032 * Free the variable with a pending return value.
22033 */
22034 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022035discard_pending_return(rettv)
22036 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037{
Bram Moolenaar33570922005-01-25 22:26:29 +000022038 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039}
22040
22041/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022042 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 * is an allocated string. Used by report_pending() for verbose messages.
22044 */
22045 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022046get_return_cmd(rettv)
22047 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022049 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022050 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022051 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022053 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022054 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022055 if (s == NULL)
22056 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022057
22058 STRCPY(IObuff, ":return ");
22059 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22060 if (STRLEN(s) + 8 >= IOSIZE)
22061 STRCPY(IObuff + IOSIZE - 4, "...");
22062 vim_free(tofree);
22063 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064}
22065
22066/*
22067 * Get next function line.
22068 * Called by do_cmdline() to get the next line.
22069 * Returns allocated string, or NULL for end of function.
22070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 char_u *
22072get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022073 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022075 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076{
Bram Moolenaar33570922005-01-25 22:26:29 +000022077 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022078 ufunc_T *fp = fcp->func;
22079 char_u *retval;
22080 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022081
22082 /* If breakpoints have been added/deleted need to check for it. */
22083 if (fcp->dbg_tick != debug_tick)
22084 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022085 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086 sourcing_lnum);
22087 fcp->dbg_tick = debug_tick;
22088 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022089#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022090 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022091 func_line_end(cookie);
22092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093
Bram Moolenaar05159a02005-02-26 23:04:13 +000022094 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022095 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22096 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097 retval = NULL;
22098 else
22099 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022100 /* Skip NULL lines (continuation lines). */
22101 while (fcp->linenr < gap->ga_len
22102 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22103 ++fcp->linenr;
22104 if (fcp->linenr >= gap->ga_len)
22105 retval = NULL;
22106 else
22107 {
22108 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22109 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022110#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022111 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022112 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022113#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022115 }
22116
22117 /* Did we encounter a breakpoint? */
22118 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22119 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022120 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022121 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022122 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123 sourcing_lnum);
22124 fcp->dbg_tick = debug_tick;
22125 }
22126
22127 return retval;
22128}
22129
Bram Moolenaar05159a02005-02-26 23:04:13 +000022130#if defined(FEAT_PROFILE) || defined(PROTO)
22131/*
22132 * Called when starting to read a function line.
22133 * "sourcing_lnum" must be correct!
22134 * When skipping lines it may not actually be executed, but we won't find out
22135 * until later and we need to store the time now.
22136 */
22137 void
22138func_line_start(cookie)
22139 void *cookie;
22140{
22141 funccall_T *fcp = (funccall_T *)cookie;
22142 ufunc_T *fp = fcp->func;
22143
22144 if (fp->uf_profiling && sourcing_lnum >= 1
22145 && sourcing_lnum <= fp->uf_lines.ga_len)
22146 {
22147 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022148 /* Skip continuation lines. */
22149 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22150 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022151 fp->uf_tml_execed = FALSE;
22152 profile_start(&fp->uf_tml_start);
22153 profile_zero(&fp->uf_tml_children);
22154 profile_get_wait(&fp->uf_tml_wait);
22155 }
22156}
22157
22158/*
22159 * Called when actually executing a function line.
22160 */
22161 void
22162func_line_exec(cookie)
22163 void *cookie;
22164{
22165 funccall_T *fcp = (funccall_T *)cookie;
22166 ufunc_T *fp = fcp->func;
22167
22168 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22169 fp->uf_tml_execed = TRUE;
22170}
22171
22172/*
22173 * Called when done with a function line.
22174 */
22175 void
22176func_line_end(cookie)
22177 void *cookie;
22178{
22179 funccall_T *fcp = (funccall_T *)cookie;
22180 ufunc_T *fp = fcp->func;
22181
22182 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22183 {
22184 if (fp->uf_tml_execed)
22185 {
22186 ++fp->uf_tml_count[fp->uf_tml_idx];
22187 profile_end(&fp->uf_tml_start);
22188 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022189 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022190 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22191 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022192 }
22193 fp->uf_tml_idx = -1;
22194 }
22195}
22196#endif
22197
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198/*
22199 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022200 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022201 */
22202 int
22203func_has_ended(cookie)
22204 void *cookie;
22205{
Bram Moolenaar33570922005-01-25 22:26:29 +000022206 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207
22208 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22209 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022210 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022211 || fcp->returned);
22212}
22213
22214/*
22215 * return TRUE if cookie indicates a function which "abort"s on errors.
22216 */
22217 int
22218func_has_abort(cookie)
22219 void *cookie;
22220{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022221 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222}
22223
22224#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22225typedef enum
22226{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022227 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22228 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22229 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022230} var_flavour_T;
22231
22232static var_flavour_T var_flavour __ARGS((char_u *varname));
22233
22234 static var_flavour_T
22235var_flavour(varname)
22236 char_u *varname;
22237{
22238 char_u *p = varname;
22239
22240 if (ASCII_ISUPPER(*p))
22241 {
22242 while (*(++p))
22243 if (ASCII_ISLOWER(*p))
22244 return VAR_FLAVOUR_SESSION;
22245 return VAR_FLAVOUR_VIMINFO;
22246 }
22247 else
22248 return VAR_FLAVOUR_DEFAULT;
22249}
22250#endif
22251
22252#if defined(FEAT_VIMINFO) || defined(PROTO)
22253/*
22254 * Restore global vars that start with a capital from the viminfo file
22255 */
22256 int
22257read_viminfo_varlist(virp, writing)
22258 vir_T *virp;
22259 int writing;
22260{
22261 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022262 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022263 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264
22265 if (!writing && (find_viminfo_parameter('!') != NULL))
22266 {
22267 tab = vim_strchr(virp->vir_line + 1, '\t');
22268 if (tab != NULL)
22269 {
22270 *tab++ = '\0'; /* isolate the variable name */
22271 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022272 type = VAR_STRING;
22273#ifdef FEAT_FLOAT
22274 else if (*tab == 'F')
22275 type = VAR_FLOAT;
22276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277
22278 tab = vim_strchr(tab, '\t');
22279 if (tab != NULL)
22280 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022281 tv.v_type = type;
22282 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022283 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022285#ifdef FEAT_FLOAT
22286 else if (type == VAR_FLOAT)
22287 (void)string2float(tab + 1, &tv.vval.v_float);
22288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022290 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022291 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022292 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022293 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 }
22295 }
22296 }
22297
22298 return viminfo_readline(virp);
22299}
22300
22301/*
22302 * Write global vars that start with a capital to the viminfo file
22303 */
22304 void
22305write_viminfo_varlist(fp)
22306 FILE *fp;
22307{
Bram Moolenaar33570922005-01-25 22:26:29 +000022308 hashitem_T *hi;
22309 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022310 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022311 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022312 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022313 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022314 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315
22316 if (find_viminfo_parameter('!') == NULL)
22317 return;
22318
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022319 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022320
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022321 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022322 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022323 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022324 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022325 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022326 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022327 this_var = HI2DI(hi);
22328 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022329 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022330 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022331 {
22332 case VAR_STRING: s = "STR"; break;
22333 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022334#ifdef FEAT_FLOAT
22335 case VAR_FLOAT: s = "FLO"; break;
22336#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022337 default: continue;
22338 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022339 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022340 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022341 if (p != NULL)
22342 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022343 vim_free(tofree);
22344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345 }
22346 }
22347}
22348#endif
22349
22350#if defined(FEAT_SESSION) || defined(PROTO)
22351 int
22352store_session_globals(fd)
22353 FILE *fd;
22354{
Bram Moolenaar33570922005-01-25 22:26:29 +000022355 hashitem_T *hi;
22356 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022357 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022358 char_u *p, *t;
22359
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022360 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022361 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022363 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022365 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022366 this_var = HI2DI(hi);
22367 if ((this_var->di_tv.v_type == VAR_NUMBER
22368 || this_var->di_tv.v_type == VAR_STRING)
22369 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022370 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022371 /* Escape special characters with a backslash. Turn a LF and
22372 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022373 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022374 (char_u *)"\\\"\n\r");
22375 if (p == NULL) /* out of memory */
22376 break;
22377 for (t = p; *t != NUL; ++t)
22378 if (*t == '\n')
22379 *t = 'n';
22380 else if (*t == '\r')
22381 *t = 'r';
22382 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022383 this_var->di_key,
22384 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22385 : ' ',
22386 p,
22387 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22388 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022389 || put_eol(fd) == FAIL)
22390 {
22391 vim_free(p);
22392 return FAIL;
22393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394 vim_free(p);
22395 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022396#ifdef FEAT_FLOAT
22397 else if (this_var->di_tv.v_type == VAR_FLOAT
22398 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22399 {
22400 float_T f = this_var->di_tv.vval.v_float;
22401 int sign = ' ';
22402
22403 if (f < 0)
22404 {
22405 f = -f;
22406 sign = '-';
22407 }
22408 if ((fprintf(fd, "let %s = %c&%f",
22409 this_var->di_key, sign, f) < 0)
22410 || put_eol(fd) == FAIL)
22411 return FAIL;
22412 }
22413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 }
22415 }
22416 return OK;
22417}
22418#endif
22419
Bram Moolenaar661b1822005-07-28 22:36:45 +000022420/*
22421 * Display script name where an item was last set.
22422 * Should only be invoked when 'verbose' is non-zero.
22423 */
22424 void
22425last_set_msg(scriptID)
22426 scid_T scriptID;
22427{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022428 char_u *p;
22429
Bram Moolenaar661b1822005-07-28 22:36:45 +000022430 if (scriptID != 0)
22431 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022432 p = home_replace_save(NULL, get_scriptname(scriptID));
22433 if (p != NULL)
22434 {
22435 verbose_enter();
22436 MSG_PUTS(_("\n\tLast set from "));
22437 MSG_PUTS(p);
22438 vim_free(p);
22439 verbose_leave();
22440 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022441 }
22442}
22443
Bram Moolenaard812df62008-11-09 12:46:09 +000022444/*
22445 * List v:oldfiles in a nice way.
22446 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022447 void
22448ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022449 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022450{
22451 list_T *l = vimvars[VV_OLDFILES].vv_list;
22452 listitem_T *li;
22453 int nr = 0;
22454
22455 if (l == NULL)
22456 msg((char_u *)_("No old files"));
22457 else
22458 {
22459 msg_start();
22460 msg_scroll = TRUE;
22461 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22462 {
22463 msg_outnum((long)++nr);
22464 MSG_PUTS(": ");
22465 msg_outtrans(get_tv_string(&li->li_tv));
22466 msg_putchar('\n');
22467 out_flush(); /* output one line at a time */
22468 ui_breakcheck();
22469 }
22470 /* Assume "got_int" was set to truncate the listing. */
22471 got_int = FALSE;
22472
22473#ifdef FEAT_BROWSE_CMD
22474 if (cmdmod.browse)
22475 {
22476 quit_more = FALSE;
22477 nr = prompt_for_number(FALSE);
22478 msg_starthere();
22479 if (nr > 0)
22480 {
22481 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22482 (long)nr);
22483
22484 if (p != NULL)
22485 {
22486 p = expand_env_save(p);
22487 eap->arg = p;
22488 eap->cmdidx = CMD_edit;
22489 cmdmod.browse = FALSE;
22490 do_exedit(eap, NULL);
22491 vim_free(p);
22492 }
22493 }
22494 }
22495#endif
22496 }
22497}
22498
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499#endif /* FEAT_EVAL */
22500
Bram Moolenaar071d4272004-06-13 20:20:40 +000022501
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022502#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022503
22504#ifdef WIN3264
22505/*
22506 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22507 */
22508static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22509static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22510static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22511
22512/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022513 * Get the short path (8.3) for the filename in "fnamep".
22514 * Only works for a valid file name.
22515 * When the path gets longer "fnamep" is changed and the allocated buffer
22516 * is put in "bufp".
22517 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22518 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022519 */
22520 static int
22521get_short_pathname(fnamep, bufp, fnamelen)
22522 char_u **fnamep;
22523 char_u **bufp;
22524 int *fnamelen;
22525{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022526 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527 char_u *newbuf;
22528
22529 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530 l = GetShortPathName(*fnamep, *fnamep, len);
22531 if (l > len - 1)
22532 {
22533 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022534 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 newbuf = vim_strnsave(*fnamep, l);
22536 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022537 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538
22539 vim_free(*bufp);
22540 *fnamep = *bufp = newbuf;
22541
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022542 /* Really should always succeed, as the buffer is big enough. */
22543 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544 }
22545
22546 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022547 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548}
22549
22550/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022551 * Get the short path (8.3) for the filename in "fname". The converted
22552 * path is returned in "bufp".
22553 *
22554 * Some of the directories specified in "fname" may not exist. This function
22555 * will shorten the existing directories at the beginning of the path and then
22556 * append the remaining non-existing path.
22557 *
22558 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022559 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022560 * bufp - Pointer to an allocated buffer for the filename.
22561 * fnamelen - Length of the filename pointed to by fname
22562 *
22563 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022564 */
22565 static int
22566shortpath_for_invalid_fname(fname, bufp, fnamelen)
22567 char_u **fname;
22568 char_u **bufp;
22569 int *fnamelen;
22570{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022571 char_u *short_fname, *save_fname, *pbuf_unused;
22572 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022574 int old_len, len;
22575 int new_len, sfx_len;
22576 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577
22578 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022579 old_len = *fnamelen;
22580 save_fname = vim_strnsave(*fname, old_len);
22581 pbuf_unused = NULL;
22582 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022583
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022584 endp = save_fname + old_len - 1; /* Find the end of the copy */
22585 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022587 /*
22588 * Try shortening the supplied path till it succeeds by removing one
22589 * directory at a time from the tail of the path.
22590 */
22591 len = 0;
22592 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022594 /* go back one path-separator */
22595 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22596 --endp;
22597 if (endp <= save_fname)
22598 break; /* processed the complete path */
22599
22600 /*
22601 * Replace the path separator with a NUL and try to shorten the
22602 * resulting path.
22603 */
22604 ch = *endp;
22605 *endp = 0;
22606 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022607 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022608 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22609 {
22610 retval = FAIL;
22611 goto theend;
22612 }
22613 *endp = ch; /* preserve the string */
22614
22615 if (len > 0)
22616 break; /* successfully shortened the path */
22617
22618 /* failed to shorten the path. Skip the path separator */
22619 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 }
22621
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022622 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022623 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022624 /*
22625 * Succeeded in shortening the path. Now concatenate the shortened
22626 * path with the remaining path at the tail.
22627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022629 /* Compute the length of the new path. */
22630 sfx_len = (int)(save_endp - endp) + 1;
22631 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022633 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022635 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022637 /* There is not enough space in the currently allocated string,
22638 * copy it to a buffer big enough. */
22639 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022641 {
22642 retval = FAIL;
22643 goto theend;
22644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 }
22646 else
22647 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022648 /* Transfer short_fname to the main buffer (it's big enough),
22649 * unless get_short_pathname() did its work in-place. */
22650 *fname = *bufp = save_fname;
22651 if (short_fname != save_fname)
22652 vim_strncpy(save_fname, short_fname, len);
22653 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022655
22656 /* concat the not-shortened part of the path */
22657 vim_strncpy(*fname + len, endp, sfx_len);
22658 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022660
22661theend:
22662 vim_free(pbuf_unused);
22663 vim_free(save_fname);
22664
22665 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022666}
22667
22668/*
22669 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022670 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671 */
22672 static int
22673shortpath_for_partial(fnamep, bufp, fnamelen)
22674 char_u **fnamep;
22675 char_u **bufp;
22676 int *fnamelen;
22677{
22678 int sepcount, len, tflen;
22679 char_u *p;
22680 char_u *pbuf, *tfname;
22681 int hasTilde;
22682
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022683 /* Count up the path separators from the RHS.. so we know which part
22684 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022686 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687 if (vim_ispathsep(*p))
22688 ++sepcount;
22689
22690 /* Need full path first (use expand_env() to remove a "~/") */
22691 hasTilde = (**fnamep == '~');
22692 if (hasTilde)
22693 pbuf = tfname = expand_env_save(*fnamep);
22694 else
22695 pbuf = tfname = FullName_save(*fnamep, FALSE);
22696
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022697 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022698
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022699 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22700 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701
22702 if (len == 0)
22703 {
22704 /* Don't have a valid filename, so shorten the rest of the
22705 * path if we can. This CAN give us invalid 8.3 filenames, but
22706 * there's not a lot of point in guessing what it might be.
22707 */
22708 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022709 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22710 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711 }
22712
22713 /* Count the paths backward to find the beginning of the desired string. */
22714 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022715 {
22716#ifdef FEAT_MBYTE
22717 if (has_mbyte)
22718 p -= mb_head_off(tfname, p);
22719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022720 if (vim_ispathsep(*p))
22721 {
22722 if (sepcount == 0 || (hasTilde && sepcount == 1))
22723 break;
22724 else
22725 sepcount --;
22726 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022728 if (hasTilde)
22729 {
22730 --p;
22731 if (p >= tfname)
22732 *p = '~';
22733 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022734 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735 }
22736 else
22737 ++p;
22738
22739 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22740 vim_free(*bufp);
22741 *fnamelen = (int)STRLEN(p);
22742 *bufp = pbuf;
22743 *fnamep = p;
22744
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022745 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746}
22747#endif /* WIN3264 */
22748
22749/*
22750 * Adjust a filename, according to a string of modifiers.
22751 * *fnamep must be NUL terminated when called. When returning, the length is
22752 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022753 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754 * When there is an error, *fnamep is set to NULL.
22755 */
22756 int
22757modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22758 char_u *src; /* string with modifiers */
22759 int *usedlen; /* characters after src that are used */
22760 char_u **fnamep; /* file name so far */
22761 char_u **bufp; /* buffer for allocated file name or NULL */
22762 int *fnamelen; /* length of fnamep */
22763{
22764 int valid = 0;
22765 char_u *tail;
22766 char_u *s, *p, *pbuf;
22767 char_u dirname[MAXPATHL];
22768 int c;
22769 int has_fullname = 0;
22770#ifdef WIN3264
22771 int has_shortname = 0;
22772#endif
22773
22774repeat:
22775 /* ":p" - full path/file_name */
22776 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22777 {
22778 has_fullname = 1;
22779
22780 valid |= VALID_PATH;
22781 *usedlen += 2;
22782
22783 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22784 if ((*fnamep)[0] == '~'
22785#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22786 && ((*fnamep)[1] == '/'
22787# ifdef BACKSLASH_IN_FILENAME
22788 || (*fnamep)[1] == '\\'
22789# endif
22790 || (*fnamep)[1] == NUL)
22791
22792#endif
22793 )
22794 {
22795 *fnamep = expand_env_save(*fnamep);
22796 vim_free(*bufp); /* free any allocated file name */
22797 *bufp = *fnamep;
22798 if (*fnamep == NULL)
22799 return -1;
22800 }
22801
22802 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022803 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804 {
22805 if (vim_ispathsep(*p)
22806 && p[1] == '.'
22807 && (p[2] == NUL
22808 || vim_ispathsep(p[2])
22809 || (p[2] == '.'
22810 && (p[3] == NUL || vim_ispathsep(p[3])))))
22811 break;
22812 }
22813
22814 /* FullName_save() is slow, don't use it when not needed. */
22815 if (*p != NUL || !vim_isAbsName(*fnamep))
22816 {
22817 *fnamep = FullName_save(*fnamep, *p != NUL);
22818 vim_free(*bufp); /* free any allocated file name */
22819 *bufp = *fnamep;
22820 if (*fnamep == NULL)
22821 return -1;
22822 }
22823
22824 /* Append a path separator to a directory. */
22825 if (mch_isdir(*fnamep))
22826 {
22827 /* Make room for one or two extra characters. */
22828 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22829 vim_free(*bufp); /* free any allocated file name */
22830 *bufp = *fnamep;
22831 if (*fnamep == NULL)
22832 return -1;
22833 add_pathsep(*fnamep);
22834 }
22835 }
22836
22837 /* ":." - path relative to the current directory */
22838 /* ":~" - path relative to the home directory */
22839 /* ":8" - shortname path - postponed till after */
22840 while (src[*usedlen] == ':'
22841 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22842 {
22843 *usedlen += 2;
22844 if (c == '8')
22845 {
22846#ifdef WIN3264
22847 has_shortname = 1; /* Postpone this. */
22848#endif
22849 continue;
22850 }
22851 pbuf = NULL;
22852 /* Need full path first (use expand_env() to remove a "~/") */
22853 if (!has_fullname)
22854 {
22855 if (c == '.' && **fnamep == '~')
22856 p = pbuf = expand_env_save(*fnamep);
22857 else
22858 p = pbuf = FullName_save(*fnamep, FALSE);
22859 }
22860 else
22861 p = *fnamep;
22862
22863 has_fullname = 0;
22864
22865 if (p != NULL)
22866 {
22867 if (c == '.')
22868 {
22869 mch_dirname(dirname, MAXPATHL);
22870 s = shorten_fname(p, dirname);
22871 if (s != NULL)
22872 {
22873 *fnamep = s;
22874 if (pbuf != NULL)
22875 {
22876 vim_free(*bufp); /* free any allocated file name */
22877 *bufp = pbuf;
22878 pbuf = NULL;
22879 }
22880 }
22881 }
22882 else
22883 {
22884 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22885 /* Only replace it when it starts with '~' */
22886 if (*dirname == '~')
22887 {
22888 s = vim_strsave(dirname);
22889 if (s != NULL)
22890 {
22891 *fnamep = s;
22892 vim_free(*bufp);
22893 *bufp = s;
22894 }
22895 }
22896 }
22897 vim_free(pbuf);
22898 }
22899 }
22900
22901 tail = gettail(*fnamep);
22902 *fnamelen = (int)STRLEN(*fnamep);
22903
22904 /* ":h" - head, remove "/file_name", can be repeated */
22905 /* Don't remove the first "/" or "c:\" */
22906 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22907 {
22908 valid |= VALID_HEAD;
22909 *usedlen += 2;
22910 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022911 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022912 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913 *fnamelen = (int)(tail - *fnamep);
22914#ifdef VMS
22915 if (*fnamelen > 0)
22916 *fnamelen += 1; /* the path separator is part of the path */
22917#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022918 if (*fnamelen == 0)
22919 {
22920 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22921 p = vim_strsave((char_u *)".");
22922 if (p == NULL)
22923 return -1;
22924 vim_free(*bufp);
22925 *bufp = *fnamep = tail = p;
22926 *fnamelen = 1;
22927 }
22928 else
22929 {
22930 while (tail > s && !after_pathsep(s, tail))
22931 mb_ptr_back(*fnamep, tail);
22932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 }
22934
22935 /* ":8" - shortname */
22936 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22937 {
22938 *usedlen += 2;
22939#ifdef WIN3264
22940 has_shortname = 1;
22941#endif
22942 }
22943
22944#ifdef WIN3264
22945 /* Check shortname after we have done 'heads' and before we do 'tails'
22946 */
22947 if (has_shortname)
22948 {
22949 pbuf = NULL;
22950 /* Copy the string if it is shortened by :h */
22951 if (*fnamelen < (int)STRLEN(*fnamep))
22952 {
22953 p = vim_strnsave(*fnamep, *fnamelen);
22954 if (p == 0)
22955 return -1;
22956 vim_free(*bufp);
22957 *bufp = *fnamep = p;
22958 }
22959
22960 /* Split into two implementations - makes it easier. First is where
22961 * there isn't a full name already, second is where there is.
22962 */
22963 if (!has_fullname && !vim_isAbsName(*fnamep))
22964 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022965 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966 return -1;
22967 }
22968 else
22969 {
22970 int l;
22971
22972 /* Simple case, already have the full-name
22973 * Nearly always shorter, so try first time. */
22974 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022975 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 return -1;
22977
22978 if (l == 0)
22979 {
22980 /* Couldn't find the filename.. search the paths.
22981 */
22982 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022983 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 return -1;
22985 }
22986 *fnamelen = l;
22987 }
22988 }
22989#endif /* WIN3264 */
22990
22991 /* ":t" - tail, just the basename */
22992 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22993 {
22994 *usedlen += 2;
22995 *fnamelen -= (int)(tail - *fnamep);
22996 *fnamep = tail;
22997 }
22998
22999 /* ":e" - extension, can be repeated */
23000 /* ":r" - root, without extension, can be repeated */
23001 while (src[*usedlen] == ':'
23002 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23003 {
23004 /* find a '.' in the tail:
23005 * - for second :e: before the current fname
23006 * - otherwise: The last '.'
23007 */
23008 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23009 s = *fnamep - 2;
23010 else
23011 s = *fnamep + *fnamelen - 1;
23012 for ( ; s > tail; --s)
23013 if (s[0] == '.')
23014 break;
23015 if (src[*usedlen + 1] == 'e') /* :e */
23016 {
23017 if (s > tail)
23018 {
23019 *fnamelen += (int)(*fnamep - (s + 1));
23020 *fnamep = s + 1;
23021#ifdef VMS
23022 /* cut version from the extension */
23023 s = *fnamep + *fnamelen - 1;
23024 for ( ; s > *fnamep; --s)
23025 if (s[0] == ';')
23026 break;
23027 if (s > *fnamep)
23028 *fnamelen = s - *fnamep;
23029#endif
23030 }
23031 else if (*fnamep <= tail)
23032 *fnamelen = 0;
23033 }
23034 else /* :r */
23035 {
23036 if (s > tail) /* remove one extension */
23037 *fnamelen = (int)(s - *fnamep);
23038 }
23039 *usedlen += 2;
23040 }
23041
23042 /* ":s?pat?foo?" - substitute */
23043 /* ":gs?pat?foo?" - global substitute */
23044 if (src[*usedlen] == ':'
23045 && (src[*usedlen + 1] == 's'
23046 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23047 {
23048 char_u *str;
23049 char_u *pat;
23050 char_u *sub;
23051 int sep;
23052 char_u *flags;
23053 int didit = FALSE;
23054
23055 flags = (char_u *)"";
23056 s = src + *usedlen + 2;
23057 if (src[*usedlen + 1] == 'g')
23058 {
23059 flags = (char_u *)"g";
23060 ++s;
23061 }
23062
23063 sep = *s++;
23064 if (sep)
23065 {
23066 /* find end of pattern */
23067 p = vim_strchr(s, sep);
23068 if (p != NULL)
23069 {
23070 pat = vim_strnsave(s, (int)(p - s));
23071 if (pat != NULL)
23072 {
23073 s = p + 1;
23074 /* find end of substitution */
23075 p = vim_strchr(s, sep);
23076 if (p != NULL)
23077 {
23078 sub = vim_strnsave(s, (int)(p - s));
23079 str = vim_strnsave(*fnamep, *fnamelen);
23080 if (sub != NULL && str != NULL)
23081 {
23082 *usedlen = (int)(p + 1 - src);
23083 s = do_string_sub(str, pat, sub, flags);
23084 if (s != NULL)
23085 {
23086 *fnamep = s;
23087 *fnamelen = (int)STRLEN(s);
23088 vim_free(*bufp);
23089 *bufp = s;
23090 didit = TRUE;
23091 }
23092 }
23093 vim_free(sub);
23094 vim_free(str);
23095 }
23096 vim_free(pat);
23097 }
23098 }
23099 /* after using ":s", repeat all the modifiers */
23100 if (didit)
23101 goto repeat;
23102 }
23103 }
23104
23105 return valid;
23106}
23107
23108/*
23109 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23110 * "flags" can be "g" to do a global substitute.
23111 * Returns an allocated string, NULL for error.
23112 */
23113 char_u *
23114do_string_sub(str, pat, sub, flags)
23115 char_u *str;
23116 char_u *pat;
23117 char_u *sub;
23118 char_u *flags;
23119{
23120 int sublen;
23121 regmatch_T regmatch;
23122 int i;
23123 int do_all;
23124 char_u *tail;
23125 garray_T ga;
23126 char_u *ret;
23127 char_u *save_cpo;
23128
23129 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23130 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023131 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132
23133 ga_init2(&ga, 1, 200);
23134
23135 do_all = (flags[0] == 'g');
23136
23137 regmatch.rm_ic = p_ic;
23138 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23139 if (regmatch.regprog != NULL)
23140 {
23141 tail = str;
23142 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23143 {
23144 /*
23145 * Get some space for a temporary buffer to do the substitution
23146 * into. It will contain:
23147 * - The text up to where the match is.
23148 * - The substituted text.
23149 * - The text after the match.
23150 */
23151 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23152 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23153 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23154 {
23155 ga_clear(&ga);
23156 break;
23157 }
23158
23159 /* copy the text up to where the match is */
23160 i = (int)(regmatch.startp[0] - tail);
23161 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23162 /* add the substituted text */
23163 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23164 + ga.ga_len + i, TRUE, TRUE, FALSE);
23165 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166 /* avoid getting stuck on a match with an empty string */
23167 if (tail == regmatch.endp[0])
23168 {
23169 if (*tail == NUL)
23170 break;
23171 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23172 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173 }
23174 else
23175 {
23176 tail = regmatch.endp[0];
23177 if (*tail == NUL)
23178 break;
23179 }
23180 if (!do_all)
23181 break;
23182 }
23183
23184 if (ga.ga_data != NULL)
23185 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23186
23187 vim_free(regmatch.regprog);
23188 }
23189
23190 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23191 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023192 if (p_cpo == empty_option)
23193 p_cpo = save_cpo;
23194 else
23195 /* Darn, evaluating {sub} expression changed the value. */
23196 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197
23198 return ret;
23199}
23200
23201#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */