blob: 446df8ed3ff203490bb3d5ffc120426841edea3f [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));
467static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +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));
734static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000743static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000746static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000748static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000749static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000750static int get_env_len __ARGS((char_u **arg));
751static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000752static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000753static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
754#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
755#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
756 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000757static 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 +0000758static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000759static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000760static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
761static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static typval_T *alloc_tv __ARGS((void));
763static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000764static void init_tv __ARGS((typval_T *varp));
765static long get_tv_number __ARGS((typval_T *varp));
766static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000767static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000768static char_u *get_tv_string __ARGS((typval_T *varp));
769static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000770static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000771static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000772static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
774static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
775static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000776static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
777static 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 +0000778static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
779static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000780static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000781static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000782static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
784static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
785static int eval_fname_script __ARGS((char_u *p));
786static int eval_fname_sid __ARGS((char_u *p));
787static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static ufunc_T *find_func __ARGS((char_u *name));
789static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000790static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000791#ifdef FEAT_PROFILE
792static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000793static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
794static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
795static int
796# ifdef __BORLANDC__
797 _RTLENTRYF
798# endif
799 prof_total_cmp __ARGS((const void *s1, const void *s2));
800static int
801# ifdef __BORLANDC__
802 _RTLENTRYF
803# endif
804 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000806static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000807static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000808static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000809static void func_free __ARGS((ufunc_T *fp));
810static void func_unref __ARGS((char_u *name));
811static void func_ref __ARGS((char_u *name));
812static 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 +0000813static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
814static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000815static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000816static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
817static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000818static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000819static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000820static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000821
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000822/* Character used as separated in autoload function/variable names. */
823#define AUTOLOAD_CHAR '#'
824
Bram Moolenaar33570922005-01-25 22:26:29 +0000825/*
826 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000827 */
828 void
829eval_init()
830{
Bram Moolenaar33570922005-01-25 22:26:29 +0000831 int i;
832 struct vimvar *p;
833
834 init_var_dict(&globvardict, &globvars_var);
835 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000836 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000837 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000838
839 for (i = 0; i < VV_LEN; ++i)
840 {
841 p = &vimvars[i];
842 STRCPY(p->vv_di.di_key, p->vv_name);
843 if (p->vv_flags & VV_RO)
844 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
845 else if (p->vv_flags & VV_RO_SBX)
846 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
847 else
848 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000849
850 /* add to v: scope dict, unless the value is not always available */
851 if (p->vv_type != VAR_UNKNOWN)
852 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000853 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000854 /* add to compat scope dict */
855 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000856 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000857 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaara7043832005-01-21 11:56:39 +0000858}
859
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000860#if defined(EXITFREE) || defined(PROTO)
861 void
862eval_clear()
863{
864 int i;
865 struct vimvar *p;
866
867 for (i = 0; i < VV_LEN; ++i)
868 {
869 p = &vimvars[i];
870 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000871 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000872 vim_free(p->vv_str);
873 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000874 }
875 else if (p->vv_di.di_tv.v_type == VAR_LIST)
876 {
877 list_unref(p->vv_list);
878 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000879 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000880 }
881 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000882 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000883 hash_clear(&compat_hashtab);
884
Bram Moolenaard9fba312005-06-26 22:34:35 +0000885 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000886
887 /* global variables */
888 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000889
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000890 /* autoloaded script names */
891 ga_clear_strings(&ga_loaded);
892
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200893 /* script-local variables */
894 for (i = 1; i <= ga_scripts.ga_len; ++i)
895 {
896 vars_clear(&SCRIPT_VARS(i));
897 vim_free(SCRIPT_SV(i));
898 }
899 ga_clear(&ga_scripts);
900
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000901 /* unreferenced lists and dicts */
902 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000903
904 /* functions */
905 free_all_functions();
906 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000907}
908#endif
909
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 * Return the name of the executed function.
912 */
913 char_u *
914func_name(cookie)
915 void *cookie;
916{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000917 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918}
919
920/*
921 * Return the address holding the next breakpoint line for a funccall cookie.
922 */
923 linenr_T *
924func_breakpoint(cookie)
925 void *cookie;
926{
Bram Moolenaar33570922005-01-25 22:26:29 +0000927 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928}
929
930/*
931 * Return the address holding the debug tick for a funccall cookie.
932 */
933 int *
934func_dbg_tick(cookie)
935 void *cookie;
936{
Bram Moolenaar33570922005-01-25 22:26:29 +0000937 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938}
939
940/*
941 * Return the nesting level for a funccall cookie.
942 */
943 int
944func_level(cookie)
945 void *cookie;
946{
Bram Moolenaar33570922005-01-25 22:26:29 +0000947 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948}
949
950/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000951funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000953/* pointer to list of previously used funccal, still around because some
954 * item in it is still being used. */
955funccall_T *previous_funccal = NULL;
956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957/*
958 * Return TRUE when a function was ended by a ":return" command.
959 */
960 int
961current_func_returned()
962{
963 return current_funccal->returned;
964}
965
966
967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 * Set an internal variable to a string value. Creates the variable if it does
969 * not already exist.
970 */
971 void
972set_internal_string_var(name, value)
973 char_u *name;
974 char_u *value;
975{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000976 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000977 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
979 val = vim_strsave(value);
980 if (val != NULL)
981 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000982 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000983 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000985 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000986 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 }
988 }
989}
990
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000991static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000992static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000993static char_u *redir_endp = NULL;
994static char_u *redir_varname = NULL;
995
996/*
997 * Start recording command output to a variable
998 * Returns OK if successfully completed the setup. FAIL otherwise.
999 */
1000 int
1001var_redir_start(name, append)
1002 char_u *name;
1003 int append; /* append to an existing variable */
1004{
1005 int save_emsg;
1006 int err;
1007 typval_T tv;
1008
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001009 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001010 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001011 {
1012 EMSG(_(e_invarg));
1013 return FAIL;
1014 }
1015
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001016 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001017 redir_varname = vim_strsave(name);
1018 if (redir_varname == NULL)
1019 return FAIL;
1020
1021 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1022 if (redir_lval == NULL)
1023 {
1024 var_redir_stop();
1025 return FAIL;
1026 }
1027
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001028 /* The output is stored in growarray "redir_ga" until redirection ends. */
1029 ga_init2(&redir_ga, (int)sizeof(char), 500);
1030
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001032 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1033 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001034 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1035 {
1036 if (redir_endp != NULL && *redir_endp != NUL)
1037 /* Trailing characters are present after the variable name */
1038 EMSG(_(e_trailing));
1039 else
1040 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001042 var_redir_stop();
1043 return FAIL;
1044 }
1045
1046 /* check if we can write to the variable: set it to or append an empty
1047 * string */
1048 save_emsg = did_emsg;
1049 did_emsg = FALSE;
1050 tv.v_type = VAR_STRING;
1051 tv.vval.v_string = (char_u *)"";
1052 if (append)
1053 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1054 else
1055 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1056 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001057 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001058 if (err)
1059 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001060 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 var_redir_stop();
1062 return FAIL;
1063 }
1064 if (redir_lval->ll_newkey != NULL)
1065 {
1066 /* Dictionary item was created, don't do it again. */
1067 vim_free(redir_lval->ll_newkey);
1068 redir_lval->ll_newkey = NULL;
1069 }
1070
1071 return OK;
1072}
1073
1074/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001075 * Append "value[value_len]" to the variable set by var_redir_start().
1076 * The actual appending is postponed until redirection ends, because the value
1077 * appended may in fact be the string we write to, changing it may cause freed
1078 * memory to be used:
1079 * :redir => foo
1080 * :let foo
1081 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 */
1083 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001084var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001086 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001088 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089
1090 if (redir_lval == NULL)
1091 return;
1092
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001093 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001094 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001096 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001098 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001099 {
1100 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001101 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001102 }
1103 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105}
1106
1107/*
1108 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001109 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 */
1111 void
1112var_redir_stop()
1113{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 typval_T tv;
1115
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 if (redir_lval != NULL)
1117 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001118 /* If there was no error: assign the text to the variable. */
1119 if (redir_endp != NULL)
1120 {
1121 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1122 tv.v_type = VAR_STRING;
1123 tv.vval.v_string = redir_ga.ga_data;
1124 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1125 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001126
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001127 /* free the collected output */
1128 vim_free(redir_ga.ga_data);
1129 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 clear_lval(redir_lval);
1132 vim_free(redir_lval);
1133 redir_lval = NULL;
1134 }
1135 vim_free(redir_varname);
1136 redir_varname = NULL;
1137}
1138
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139# if defined(FEAT_MBYTE) || defined(PROTO)
1140 int
1141eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1142 char_u *enc_from;
1143 char_u *enc_to;
1144 char_u *fname_from;
1145 char_u *fname_to;
1146{
1147 int err = FALSE;
1148
1149 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1150 set_vim_var_string(VV_CC_TO, enc_to, -1);
1151 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1152 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1153 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1154 err = TRUE;
1155 set_vim_var_string(VV_CC_FROM, NULL, -1);
1156 set_vim_var_string(VV_CC_TO, NULL, -1);
1157 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1158 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1159
1160 if (err)
1161 return FAIL;
1162 return OK;
1163}
1164# endif
1165
1166# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1167 int
1168eval_printexpr(fname, args)
1169 char_u *fname;
1170 char_u *args;
1171{
1172 int err = FALSE;
1173
1174 set_vim_var_string(VV_FNAME_IN, fname, -1);
1175 set_vim_var_string(VV_CMDARG, args, -1);
1176 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1177 err = TRUE;
1178 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1179 set_vim_var_string(VV_CMDARG, NULL, -1);
1180
1181 if (err)
1182 {
1183 mch_remove(fname);
1184 return FAIL;
1185 }
1186 return OK;
1187}
1188# endif
1189
1190# if defined(FEAT_DIFF) || defined(PROTO)
1191 void
1192eval_diff(origfile, newfile, outfile)
1193 char_u *origfile;
1194 char_u *newfile;
1195 char_u *outfile;
1196{
1197 int err = FALSE;
1198
1199 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1200 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1201 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1202 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1203 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1204 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1205 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1206}
1207
1208 void
1209eval_patch(origfile, difffile, outfile)
1210 char_u *origfile;
1211 char_u *difffile;
1212 char_u *outfile;
1213{
1214 int err;
1215
1216 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1217 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1218 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1219 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1220 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1221 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1222 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1223}
1224# endif
1225
1226/*
1227 * Top level evaluation function, returning a boolean.
1228 * Sets "error" to TRUE if there was an error.
1229 * Return TRUE or FALSE.
1230 */
1231 int
1232eval_to_bool(arg, error, nextcmd, skip)
1233 char_u *arg;
1234 int *error;
1235 char_u **nextcmd;
1236 int skip; /* only parse, don't execute */
1237{
Bram Moolenaar33570922005-01-25 22:26:29 +00001238 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 int retval = FALSE;
1240
1241 if (skip)
1242 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001243 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 else
1246 {
1247 *error = FALSE;
1248 if (!skip)
1249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001250 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001251 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 }
1253 }
1254 if (skip)
1255 --emsg_skip;
1256
1257 return retval;
1258}
1259
1260/*
1261 * Top level evaluation function, returning a string. If "skip" is TRUE,
1262 * only parsing to "nextcmd" is done, without reporting errors. Return
1263 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1264 */
1265 char_u *
1266eval_to_string_skip(arg, nextcmd, skip)
1267 char_u *arg;
1268 char_u **nextcmd;
1269 int skip; /* only parse, don't execute */
1270{
Bram Moolenaar33570922005-01-25 22:26:29 +00001271 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 char_u *retval;
1273
1274 if (skip)
1275 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001276 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 retval = NULL;
1278 else
1279 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001280 retval = vim_strsave(get_tv_string(&tv));
1281 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 if (skip)
1284 --emsg_skip;
1285
1286 return retval;
1287}
1288
1289/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001290 * Skip over an expression at "*pp".
1291 * Return FAIL for an error, OK otherwise.
1292 */
1293 int
1294skip_expr(pp)
1295 char_u **pp;
1296{
Bram Moolenaar33570922005-01-25 22:26:29 +00001297 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001298
1299 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001300 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001301}
1302
1303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001305 * When "convert" is TRUE convert a List into a sequence of lines and convert
1306 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 * Return pointer to allocated memory, or NULL for failure.
1308 */
1309 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001310eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 char_u *arg;
1312 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001313 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
Bram Moolenaar33570922005-01-25 22:26:29 +00001315 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001317 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001318#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001319 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001322 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 retval = NULL;
1324 else
1325 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001326 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001327 {
1328 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001329 if (tv.vval.v_list != NULL)
1330 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001331 ga_append(&ga, NUL);
1332 retval = (char_u *)ga.ga_data;
1333 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334#ifdef FEAT_FLOAT
1335 else if (convert && tv.v_type == VAR_FLOAT)
1336 {
1337 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1338 retval = vim_strsave(numbuf);
1339 }
1340#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001341 else
1342 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001343 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 }
1345
1346 return retval;
1347}
1348
1349/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001350 * Call eval_to_string() without using current local variables and using
1351 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 */
1353 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001354eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 char_u *arg;
1356 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001357 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358{
1359 char_u *retval;
1360 void *save_funccalp;
1361
1362 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001363 if (use_sandbox)
1364 ++sandbox;
1365 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001367 if (use_sandbox)
1368 --sandbox;
1369 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 restore_funccal(save_funccalp);
1371 return retval;
1372}
1373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374/*
1375 * Top level evaluation function, returning a number.
1376 * Evaluates "expr" silently.
1377 * Returns -1 for an error.
1378 */
1379 int
1380eval_to_number(expr)
1381 char_u *expr;
1382{
Bram Moolenaar33570922005-01-25 22:26:29 +00001383 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001385 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
1387 ++emsg_off;
1388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001389 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 retval = -1;
1391 else
1392 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001393 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001394 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396 --emsg_off;
1397
1398 return retval;
1399}
1400
Bram Moolenaara40058a2005-07-11 22:42:07 +00001401/*
1402 * Prepare v: variable "idx" to be used.
1403 * Save the current typeval in "save_tv".
1404 * When not used yet add the variable to the v: hashtable.
1405 */
1406 static void
1407prepare_vimvar(idx, save_tv)
1408 int idx;
1409 typval_T *save_tv;
1410{
1411 *save_tv = vimvars[idx].vv_tv;
1412 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1413 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1414}
1415
1416/*
1417 * Restore v: variable "idx" to typeval "save_tv".
1418 * When no longer defined, remove the variable from the v: hashtable.
1419 */
1420 static void
1421restore_vimvar(idx, save_tv)
1422 int idx;
1423 typval_T *save_tv;
1424{
1425 hashitem_T *hi;
1426
Bram Moolenaara40058a2005-07-11 22:42:07 +00001427 vimvars[idx].vv_tv = *save_tv;
1428 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1429 {
1430 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1431 if (HASHITEM_EMPTY(hi))
1432 EMSG2(_(e_intern2), "restore_vimvar()");
1433 else
1434 hash_remove(&vimvarht, hi);
1435 }
1436}
1437
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001438#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001439/*
1440 * Evaluate an expression to a list with suggestions.
1441 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001442 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001443 */
1444 list_T *
1445eval_spell_expr(badword, expr)
1446 char_u *badword;
1447 char_u *expr;
1448{
1449 typval_T save_val;
1450 typval_T rettv;
1451 list_T *list = NULL;
1452 char_u *p = skipwhite(expr);
1453
1454 /* Set "v:val" to the bad word. */
1455 prepare_vimvar(VV_VAL, &save_val);
1456 vimvars[VV_VAL].vv_type = VAR_STRING;
1457 vimvars[VV_VAL].vv_str = badword;
1458 if (p_verbose == 0)
1459 ++emsg_off;
1460
1461 if (eval1(&p, &rettv, TRUE) == OK)
1462 {
1463 if (rettv.v_type != VAR_LIST)
1464 clear_tv(&rettv);
1465 else
1466 list = rettv.vval.v_list;
1467 }
1468
1469 if (p_verbose == 0)
1470 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001471 restore_vimvar(VV_VAL, &save_val);
1472
1473 return list;
1474}
1475
1476/*
1477 * "list" is supposed to contain two items: a word and a number. Return the
1478 * word in "pp" and the number as the return value.
1479 * Return -1 if anything isn't right.
1480 * Used to get the good word and score from the eval_spell_expr() result.
1481 */
1482 int
1483get_spellword(list, pp)
1484 list_T *list;
1485 char_u **pp;
1486{
1487 listitem_T *li;
1488
1489 li = list->lv_first;
1490 if (li == NULL)
1491 return -1;
1492 *pp = get_tv_string(&li->li_tv);
1493
1494 li = li->li_next;
1495 if (li == NULL)
1496 return -1;
1497 return get_tv_number(&li->li_tv);
1498}
1499#endif
1500
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001501/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001502 * Top level evaluation function.
1503 * Returns an allocated typval_T with the result.
1504 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001505 */
1506 typval_T *
1507eval_expr(arg, nextcmd)
1508 char_u *arg;
1509 char_u **nextcmd;
1510{
1511 typval_T *tv;
1512
1513 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001514 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001515 {
1516 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001517 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001518 }
1519
1520 return tv;
1521}
1522
1523
Bram Moolenaar4f688582007-07-24 12:34:30 +00001524#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1525 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001527 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001528 * Uses argv[argc] for the function arguments. Only Number and String
1529 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001530 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001532 static int
1533call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 char_u *func;
1535 int argc;
1536 char_u **argv;
1537 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539{
Bram Moolenaar33570922005-01-25 22:26:29 +00001540 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 long n;
1542 int len;
1543 int i;
1544 int doesrange;
1545 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001546 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001548 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001550 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
1552 for (i = 0; i < argc; i++)
1553 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001554 /* Pass a NULL or empty argument as an empty string */
1555 if (argv[i] == NULL || *argv[i] == NUL)
1556 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001557 argvars[i].v_type = VAR_STRING;
1558 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001559 continue;
1560 }
1561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 /* Recognize a number argument, the others must be strings. */
1563 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1564 if (len != 0 && len == (int)STRLEN(argv[i]))
1565 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001566 argvars[i].v_type = VAR_NUMBER;
1567 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 }
1569 else
1570 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001571 argvars[i].v_type = VAR_STRING;
1572 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 }
1574 }
1575
1576 if (safe)
1577 {
1578 save_funccalp = save_funccal();
1579 ++sandbox;
1580 }
1581
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001582 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1583 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001585 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 if (safe)
1587 {
1588 --sandbox;
1589 restore_funccal(save_funccalp);
1590 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001591 vim_free(argvars);
1592
1593 if (ret == FAIL)
1594 clear_tv(rettv);
1595
1596 return ret;
1597}
1598
Bram Moolenaar4f688582007-07-24 12:34:30 +00001599# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001600/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001601 * Call vimL function "func" and return the result as a string.
1602 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001603 * Uses argv[argc] for the function arguments.
1604 */
1605 void *
1606call_func_retstr(func, argc, argv, safe)
1607 char_u *func;
1608 int argc;
1609 char_u **argv;
1610 int safe; /* use the sandbox */
1611{
1612 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001613 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614
1615 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1616 return NULL;
1617
1618 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 return retval;
1621}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001622# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623
Bram Moolenaar4f688582007-07-24 12:34:30 +00001624# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001625/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001626 * Call vimL function "func" and return the result as a number.
1627 * Returns -1 when calling the function fails.
1628 * Uses argv[argc] for the function arguments.
1629 */
1630 long
1631call_func_retnr(func, argc, argv, safe)
1632 char_u *func;
1633 int argc;
1634 char_u **argv;
1635 int safe; /* use the sandbox */
1636{
1637 typval_T rettv;
1638 long retval;
1639
1640 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1641 return -1;
1642
1643 retval = get_tv_number_chk(&rettv, NULL);
1644 clear_tv(&rettv);
1645 return retval;
1646}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001647# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001648
1649/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001650 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001651 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001652 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653 */
1654 void *
1655call_func_retlist(func, argc, argv, safe)
1656 char_u *func;
1657 int argc;
1658 char_u **argv;
1659 int safe; /* use the sandbox */
1660{
1661 typval_T rettv;
1662
1663 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1664 return NULL;
1665
1666 if (rettv.v_type != VAR_LIST)
1667 {
1668 clear_tv(&rettv);
1669 return NULL;
1670 }
1671
1672 return rettv.vval.v_list;
1673}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674#endif
1675
Bram Moolenaar4f688582007-07-24 12:34:30 +00001676
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677/*
1678 * Save the current function call pointer, and set it to NULL.
1679 * Used when executing autocommands and for ":source".
1680 */
1681 void *
1682save_funccal()
1683{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001684 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 current_funccal = NULL;
1687 return (void *)fc;
1688}
1689
1690 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001691restore_funccal(vfc)
1692 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001694 funccall_T *fc = (funccall_T *)vfc;
1695
1696 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697}
1698
Bram Moolenaar05159a02005-02-26 23:04:13 +00001699#if defined(FEAT_PROFILE) || defined(PROTO)
1700/*
1701 * Prepare profiling for entering a child or something else that is not
1702 * counted for the script/function itself.
1703 * Should always be called in pair with prof_child_exit().
1704 */
1705 void
1706prof_child_enter(tm)
1707 proftime_T *tm; /* place to store waittime */
1708{
1709 funccall_T *fc = current_funccal;
1710
1711 if (fc != NULL && fc->func->uf_profiling)
1712 profile_start(&fc->prof_child);
1713 script_prof_save(tm);
1714}
1715
1716/*
1717 * Take care of time spent in a child.
1718 * Should always be called after prof_child_enter().
1719 */
1720 void
1721prof_child_exit(tm)
1722 proftime_T *tm; /* where waittime was stored */
1723{
1724 funccall_T *fc = current_funccal;
1725
1726 if (fc != NULL && fc->func->uf_profiling)
1727 {
1728 profile_end(&fc->prof_child);
1729 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1730 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1731 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1732 }
1733 script_prof_restore(tm);
1734}
1735#endif
1736
1737
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738#ifdef FEAT_FOLDING
1739/*
1740 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1741 * it in "*cp". Doesn't give error messages.
1742 */
1743 int
1744eval_foldexpr(arg, cp)
1745 char_u *arg;
1746 int *cp;
1747{
Bram Moolenaar33570922005-01-25 22:26:29 +00001748 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 int retval;
1750 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001751 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1752 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
1754 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001755 if (use_sandbox)
1756 ++sandbox;
1757 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 retval = 0;
1761 else
1762 {
1763 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001764 if (tv.v_type == VAR_NUMBER)
1765 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001766 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 retval = 0;
1768 else
1769 {
1770 /* If the result is a string, check if there is a non-digit before
1771 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001772 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 if (!VIM_ISDIGIT(*s) && *s != '-')
1774 *cp = *s++;
1775 retval = atol((char *)s);
1776 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001777 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 }
1779 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001780 if (use_sandbox)
1781 --sandbox;
1782 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
1784 return retval;
1785}
1786#endif
1787
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001789 * ":let" list all variable values
1790 * ":let var1 var2" list variable values
1791 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001792 * ":let var += expr" assignment command.
1793 * ":let var -= expr" assignment command.
1794 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 */
1797 void
1798ex_let(eap)
1799 exarg_T *eap;
1800{
1801 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001803 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 int var_count = 0;
1806 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001807 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001808 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001809 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaardb552d602006-03-23 22:59:57 +00001811 argend = skip_var_list(arg, &var_count, &semicolon);
1812 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001814 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1815 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001816 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001817 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001819 /*
1820 * ":let" without "=": list variables
1821 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001822 if (*arg == '[')
1823 EMSG(_(e_invarg));
1824 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001826 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001828 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001830 list_glob_vars(&first);
1831 list_buf_vars(&first);
1832 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001833#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001834 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001835#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001836 list_script_vars(&first);
1837 list_func_vars(&first);
1838 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 eap->nextcmd = check_nextcmd(arg);
1841 }
1842 else
1843 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001844 op[0] = '=';
1845 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001846 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001847 {
1848 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1849 op[0] = expr[-1]; /* +=, -= or .= */
1850 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (eap->skip)
1854 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 if (eap->skip)
1857 {
1858 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 --emsg_skip;
1861 }
1862 else if (i != FAIL)
1863 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001865 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868 }
1869}
1870
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871/*
1872 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1873 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001874 * When "nextchars" is not NULL it points to a string with characters that
1875 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1876 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001877 * Returns OK or FAIL;
1878 */
1879 static int
1880ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1881 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001882 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883 int copy; /* copy values from "tv", don't move */
1884 int semicolon; /* from skip_var_list() */
1885 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887{
1888 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001889 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001891 listitem_T *item;
1892 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893
1894 if (*arg != '[')
1895 {
1896 /*
1897 * ":let var = expr" or ":for var in list"
1898 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001899 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900 return FAIL;
1901 return OK;
1902 }
1903
1904 /*
1905 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1906 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001907 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001908 {
1909 EMSG(_(e_listreq));
1910 return FAIL;
1911 }
1912
1913 i = list_len(l);
1914 if (semicolon == 0 && var_count < i)
1915 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001916 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917 return FAIL;
1918 }
1919 if (var_count - semicolon > i)
1920 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001921 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 return FAIL;
1923 }
1924
1925 item = l->lv_first;
1926 while (*arg != ']')
1927 {
1928 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001929 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 item = item->li_next;
1931 if (arg == NULL)
1932 return FAIL;
1933
1934 arg = skipwhite(arg);
1935 if (*arg == ';')
1936 {
1937 /* Put the rest of the list (may be empty) in the var after ';'.
1938 * Create a new list for this. */
1939 l = list_alloc();
1940 if (l == NULL)
1941 return FAIL;
1942 while (item != NULL)
1943 {
1944 list_append_tv(l, &item->li_tv);
1945 item = item->li_next;
1946 }
1947
1948 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001949 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 ltv.vval.v_list = l;
1951 l->lv_refcount = 1;
1952
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001953 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1954 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 clear_tv(&ltv);
1956 if (arg == NULL)
1957 return FAIL;
1958 break;
1959 }
1960 else if (*arg != ',' && *arg != ']')
1961 {
1962 EMSG2(_(e_intern2), "ex_let_vars()");
1963 return FAIL;
1964 }
1965 }
1966
1967 return OK;
1968}
1969
1970/*
1971 * Skip over assignable variable "var" or list of variables "[var, var]".
1972 * Used for ":let varvar = expr" and ":for varvar in expr".
1973 * For "[var, var]" increment "*var_count" for each variable.
1974 * for "[var, var; var]" set "semicolon".
1975 * Return NULL for an error.
1976 */
1977 static char_u *
1978skip_var_list(arg, var_count, semicolon)
1979 char_u *arg;
1980 int *var_count;
1981 int *semicolon;
1982{
1983 char_u *p, *s;
1984
1985 if (*arg == '[')
1986 {
1987 /* "[var, var]": find the matching ']'. */
1988 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001989 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 {
1991 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1992 s = skip_var_one(p);
1993 if (s == p)
1994 {
1995 EMSG2(_(e_invarg2), p);
1996 return NULL;
1997 }
1998 ++*var_count;
1999
2000 p = skipwhite(s);
2001 if (*p == ']')
2002 break;
2003 else if (*p == ';')
2004 {
2005 if (*semicolon == 1)
2006 {
2007 EMSG(_("Double ; in list of variables"));
2008 return NULL;
2009 }
2010 *semicolon = 1;
2011 }
2012 else if (*p != ',')
2013 {
2014 EMSG2(_(e_invarg2), p);
2015 return NULL;
2016 }
2017 }
2018 return p + 1;
2019 }
2020 else
2021 return skip_var_one(arg);
2022}
2023
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002024/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002025 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002026 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002027 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002028 static char_u *
2029skip_var_one(arg)
2030 char_u *arg;
2031{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002032 if (*arg == '@' && arg[1] != NUL)
2033 return arg + 2;
2034 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2035 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002036}
2037
Bram Moolenaara7043832005-01-21 11:56:39 +00002038/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002039 * List variables for hashtab "ht" with prefix "prefix".
2040 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002041 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002043list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002044 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002045 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002046 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002047 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002048{
Bram Moolenaar33570922005-01-25 22:26:29 +00002049 hashitem_T *hi;
2050 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002051 int todo;
2052
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002053 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002054 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2055 {
2056 if (!HASHITEM_EMPTY(hi))
2057 {
2058 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002059 di = HI2DI(hi);
2060 if (empty || di->di_tv.v_type != VAR_STRING
2061 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002062 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002063 }
2064 }
2065}
2066
2067/*
2068 * List global variables.
2069 */
2070 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002071list_glob_vars(first)
2072 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002073{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002074 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002075}
2076
2077/*
2078 * List buffer variables.
2079 */
2080 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002081list_buf_vars(first)
2082 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002083{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002084 char_u numbuf[NUMBUFLEN];
2085
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002086 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2087 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002088
2089 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002090 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2091 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002092}
2093
2094/*
2095 * List window variables.
2096 */
2097 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002098list_win_vars(first)
2099 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002100{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2102 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002103}
2104
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002105#ifdef FEAT_WINDOWS
2106/*
2107 * List tab page variables.
2108 */
2109 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110list_tab_vars(first)
2111 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002112{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2114 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002115}
2116#endif
2117
Bram Moolenaara7043832005-01-21 11:56:39 +00002118/*
2119 * List Vim variables.
2120 */
2121 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122list_vim_vars(first)
2123 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002124{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002126}
2127
2128/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002129 * List script-local variables, if there is a script.
2130 */
2131 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132list_script_vars(first)
2133 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002134{
2135 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2137 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002138}
2139
2140/*
2141 * List function variables, if there is a function.
2142 */
2143 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144list_func_vars(first)
2145 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002146{
2147 if (current_funccal != NULL)
2148 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002150}
2151
2152/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153 * List variables in "arg".
2154 */
2155 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002157 exarg_T *eap;
2158 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160{
2161 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002162 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002163 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002164 char_u *name_start;
2165 char_u *arg_subsc;
2166 char_u *tofree;
2167 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168
2169 while (!ends_excmd(*arg) && !got_int)
2170 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002171 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002173 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002174 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2175 {
2176 emsg_severe = TRUE;
2177 EMSG(_(e_trailing));
2178 break;
2179 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002180 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002181 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002183 /* get_name_len() takes care of expanding curly braces */
2184 name_start = name = arg;
2185 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2186 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002188 /* This is mainly to keep test 49 working: when expanding
2189 * curly braces fails overrule the exception error message. */
2190 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002192 emsg_severe = TRUE;
2193 EMSG2(_(e_invarg2), arg);
2194 break;
2195 }
2196 error = TRUE;
2197 }
2198 else
2199 {
2200 if (tofree != NULL)
2201 name = tofree;
2202 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 else
2205 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 /* handle d.key, l[idx], f(expr) */
2207 arg_subsc = arg;
2208 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002209 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002211 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002213 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002214 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002215 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216 case 'g': list_glob_vars(first); break;
2217 case 'b': list_buf_vars(first); break;
2218 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002219#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002221#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002222 case 'v': list_vim_vars(first); break;
2223 case 's': list_script_vars(first); break;
2224 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 default:
2226 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002227 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002228 }
2229 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 {
2231 char_u numbuf[NUMBUFLEN];
2232 char_u *tf;
2233 int c;
2234 char_u *s;
2235
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002236 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002237 c = *arg;
2238 *arg = NUL;
2239 list_one_var_a((char_u *)"",
2240 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002241 tv.v_type,
2242 s == NULL ? (char_u *)"" : s,
2243 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 *arg = c;
2245 vim_free(tf);
2246 }
2247 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 }
2250 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002251
2252 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254
2255 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 }
2257
2258 return arg;
2259}
2260
2261/*
2262 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2263 * Returns a pointer to the char just after the var name.
2264 * Returns NULL if there is an error.
2265 */
2266 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002267ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002269 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002271 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002272 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273{
2274 int c1;
2275 char_u *name;
2276 char_u *p;
2277 char_u *arg_end = NULL;
2278 int len;
2279 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002280 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281
2282 /*
2283 * ":let $VAR = expr": Set environment variable.
2284 */
2285 if (*arg == '$')
2286 {
2287 /* Find the end of the name. */
2288 ++arg;
2289 name = arg;
2290 len = get_env_len(&arg);
2291 if (len == 0)
2292 EMSG2(_(e_invarg2), name - 1);
2293 else
2294 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002295 if (op != NULL && (*op == '+' || *op == '-'))
2296 EMSG2(_(e_letwrong), op);
2297 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002298 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 EMSG(_(e_letunexp));
2300 else
2301 {
2302 c1 = name[len];
2303 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002304 p = get_tv_string_chk(tv);
2305 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002306 {
2307 int mustfree = FALSE;
2308 char_u *s = vim_getenv(name, &mustfree);
2309
2310 if (s != NULL)
2311 {
2312 p = tofree = concat_str(s, p);
2313 if (mustfree)
2314 vim_free(s);
2315 }
2316 }
2317 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002318 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002319 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002320 if (STRICMP(name, "HOME") == 0)
2321 init_homedir();
2322 else if (didset_vim && STRICMP(name, "VIM") == 0)
2323 didset_vim = FALSE;
2324 else if (didset_vimruntime
2325 && STRICMP(name, "VIMRUNTIME") == 0)
2326 didset_vimruntime = FALSE;
2327 arg_end = arg;
2328 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 }
2332 }
2333 }
2334
2335 /*
2336 * ":let &option = expr": Set option value.
2337 * ":let &l:option = expr": Set local option value.
2338 * ":let &g:option = expr": Set global option value.
2339 */
2340 else if (*arg == '&')
2341 {
2342 /* Find the end of the name. */
2343 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002344 if (p == NULL || (endchars != NULL
2345 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 EMSG(_(e_letunexp));
2347 else
2348 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002349 long n;
2350 int opt_type;
2351 long numval;
2352 char_u *stringval = NULL;
2353 char_u *s;
2354
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355 c1 = *p;
2356 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002357
2358 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002359 s = get_tv_string_chk(tv); /* != NULL if number or string */
2360 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002361 {
2362 opt_type = get_option_value(arg, &numval,
2363 &stringval, opt_flags);
2364 if ((opt_type == 1 && *op == '.')
2365 || (opt_type == 0 && *op != '.'))
2366 EMSG2(_(e_letwrong), op);
2367 else
2368 {
2369 if (opt_type == 1) /* number */
2370 {
2371 if (*op == '+')
2372 n = numval + n;
2373 else
2374 n = numval - n;
2375 }
2376 else if (opt_type == 0 && stringval != NULL) /* string */
2377 {
2378 s = concat_str(stringval, s);
2379 vim_free(stringval);
2380 stringval = s;
2381 }
2382 }
2383 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 if (s != NULL)
2385 {
2386 set_option_value(arg, n, s, opt_flags);
2387 arg_end = p;
2388 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002391 }
2392 }
2393
2394 /*
2395 * ":let @r = expr": Set register contents.
2396 */
2397 else if (*arg == '@')
2398 {
2399 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 if (op != NULL && (*op == '+' || *op == '-'))
2401 EMSG2(_(e_letwrong), op);
2402 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002403 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 EMSG(_(e_letunexp));
2405 else
2406 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002407 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002408 char_u *s;
2409
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002410 p = get_tv_string_chk(tv);
2411 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002413 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002414 if (s != NULL)
2415 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002416 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002417 vim_free(s);
2418 }
2419 }
2420 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002421 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002422 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002423 arg_end = arg + 1;
2424 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002425 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 }
2427 }
2428
2429 /*
2430 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002431 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002433 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002435 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002436
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002437 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2441 EMSG(_(e_letunexp));
2442 else
2443 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 arg_end = p;
2446 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 }
2450
2451 else
2452 EMSG2(_(e_invarg2), arg);
2453
2454 return arg_end;
2455}
2456
2457/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2459 */
2460 static int
2461check_changedtick(arg)
2462 char_u *arg;
2463{
2464 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2465 {
2466 EMSG2(_(e_readonlyvar), arg);
2467 return TRUE;
2468 }
2469 return FALSE;
2470}
2471
2472/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 * Get an lval: variable, Dict item or List item that can be assigned a value
2474 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2475 * "name.key", "name.key[expr]" etc.
2476 * Indexing only works if "name" is an existing List or Dictionary.
2477 * "name" points to the start of the name.
2478 * If "rettv" is not NULL it points to the value to be assigned.
2479 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2480 * wrong; must end in space or cmd separator.
2481 *
2482 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002483 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002485 */
2486 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002487get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002488 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002489 typval_T *rettv;
2490 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 int unlet;
2492 int skip;
2493 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002494 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 char_u *expr_start, *expr_end;
2498 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002499 dictitem_T *v;
2500 typval_T var1;
2501 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002503 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002506 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002509 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510
2511 if (skip)
2512 {
2513 /* When skipping just find the end of the name. */
2514 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002515 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 }
2517
2518 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002519 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (expr_start != NULL)
2521 {
2522 /* Don't expand the name when we already know there is an error. */
2523 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2524 && *p != '[' && *p != '.')
2525 {
2526 EMSG(_(e_trailing));
2527 return NULL;
2528 }
2529
2530 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2531 if (lp->ll_exp_name == NULL)
2532 {
2533 /* Report an invalid expression in braces, unless the
2534 * expression evaluation has been cancelled due to an
2535 * aborting error, an interrupt, or an exception. */
2536 if (!aborting() && !quiet)
2537 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002538 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 EMSG2(_(e_invarg2), name);
2540 return NULL;
2541 }
2542 }
2543 lp->ll_name = lp->ll_exp_name;
2544 }
2545 else
2546 lp->ll_name = name;
2547
2548 /* Without [idx] or .key we are done. */
2549 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2550 return p;
2551
2552 cc = *p;
2553 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002554 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 if (v == NULL && !quiet)
2556 EMSG2(_(e_undefvar), lp->ll_name);
2557 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 if (v == NULL)
2559 return NULL;
2560
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 /*
2562 * Loop until no more [idx] or .key is following.
2563 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002566 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2568 && !(lp->ll_tv->v_type == VAR_DICT
2569 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 if (!quiet)
2572 EMSG(_("E689: Can only index a List or Dictionary"));
2573 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 if (!quiet)
2578 EMSG(_("E708: [:] must come last"));
2579 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002580 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002581
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 len = -1;
2583 if (*p == '.')
2584 {
2585 key = p + 1;
2586 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2587 ;
2588 if (len == 0)
2589 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (!quiet)
2591 EMSG(_(e_emptykey));
2592 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 }
2594 p = key + len;
2595 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002596 else
2597 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002598 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002599 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002600 if (*p == ':')
2601 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002602 else
2603 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002604 empty1 = FALSE;
2605 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002607 if (get_tv_string_chk(&var1) == NULL)
2608 {
2609 /* not a number or string */
2610 clear_tv(&var1);
2611 return NULL;
2612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 }
2614
2615 /* Optionally get the second index [ :expr]. */
2616 if (*p == ':')
2617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002621 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002622 if (!empty1)
2623 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 if (rettv != NULL && (rettv->v_type != VAR_LIST
2627 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 if (!quiet)
2630 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002631 if (!empty1)
2632 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 }
2635 p = skipwhite(p + 1);
2636 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 else
2639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2642 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 if (!empty1)
2644 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002647 if (get_tv_string_chk(&var2) == NULL)
2648 {
2649 /* not a number or string */
2650 if (!empty1)
2651 clear_tv(&var1);
2652 clear_tv(&var2);
2653 return NULL;
2654 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002657 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (!quiet)
2664 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 if (!empty1)
2666 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 }
2671
2672 /* Skip to past ']'. */
2673 ++p;
2674 }
2675
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 {
2678 if (len == -1)
2679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002681 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 if (*key == NUL)
2683 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 if (!quiet)
2685 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 }
2689 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002690 lp->ll_list = NULL;
2691 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002692 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002695 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002699 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 if (len == -1)
2701 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 }
2704 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 if (len == -1)
2709 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 p = NULL;
2712 break;
2713 }
2714 if (len == -1)
2715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718 else
2719 {
2720 /*
2721 * Get the number and item for the only or first index of the List.
2722 */
2723 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 else
2726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002727 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 clear_tv(&var1);
2729 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002730 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_list = lp->ll_tv->vval.v_list;
2732 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2733 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002735 if (lp->ll_n1 < 0)
2736 {
2737 lp->ll_n1 = 0;
2738 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2739 }
2740 }
2741 if (lp->ll_li == NULL)
2742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 }
2747
2748 /*
2749 * May need to find the item or absolute index for the second
2750 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 * When no index given: "lp->ll_empty2" is TRUE.
2752 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002756 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 }
2765
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2767 if (lp->ll_n1 < 0)
2768 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2769 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002771 }
2772
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002774 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002775 }
2776
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 return p;
2778}
2779
2780/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002781 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 */
2783 static void
2784clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002785 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786{
2787 vim_free(lp->ll_exp_name);
2788 vim_free(lp->ll_newkey);
2789}
2790
2791/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002792 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002794 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 */
2796 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002797set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002798 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002800 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002802 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803{
2804 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002805 listitem_T *ri;
2806 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807
2808 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002809 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002811 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 cc = *endp;
2813 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002814 if (op != NULL && *op != '=')
2815 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002816 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002817
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002818 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002819 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002820 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002821 {
2822 if (tv_op(&tv, rettv, op) == OK)
2823 set_var(lp->ll_name, &tv, FALSE);
2824 clear_tv(&tv);
2825 }
2826 }
2827 else
2828 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002830 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002832 else if (tv_check_lock(lp->ll_newkey == NULL
2833 ? lp->ll_tv->v_lock
2834 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2835 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 else if (lp->ll_range)
2837 {
2838 /*
2839 * Assign the List values to the list items.
2840 */
2841 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002842 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002843 if (op != NULL && *op != '=')
2844 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2845 else
2846 {
2847 clear_tv(&lp->ll_li->li_tv);
2848 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2849 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 ri = ri->li_next;
2851 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2852 break;
2853 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002854 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002856 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002857 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 ri = NULL;
2859 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002860 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002861 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_li = lp->ll_li->li_next;
2863 ++lp->ll_n1;
2864 }
2865 if (ri != NULL)
2866 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002867 else if (lp->ll_empty2
2868 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 : lp->ll_n1 != lp->ll_n2)
2870 EMSG(_("E711: List value has not enough items"));
2871 }
2872 else
2873 {
2874 /*
2875 * Assign to a List or Dictionary item.
2876 */
2877 if (lp->ll_newkey != NULL)
2878 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002879 if (op != NULL && *op != '=')
2880 {
2881 EMSG2(_(e_letwrong), op);
2882 return;
2883 }
2884
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002886 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 if (di == NULL)
2888 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002889 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2890 {
2891 vim_free(di);
2892 return;
2893 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002895 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896 else if (op != NULL && *op != '=')
2897 {
2898 tv_op(lp->ll_tv, rettv, op);
2899 return;
2900 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002901 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002903
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 /*
2905 * Assign the value to the variable or list item.
2906 */
2907 if (copy)
2908 copy_tv(rettv, lp->ll_tv);
2909 else
2910 {
2911 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002912 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002914 }
2915 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002916}
2917
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002918/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002919 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2920 * Returns OK or FAIL.
2921 */
2922 static int
2923tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 typval_T *tv1;
2925 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002926 char_u *op;
2927{
2928 long n;
2929 char_u numbuf[NUMBUFLEN];
2930 char_u *s;
2931
2932 /* Can't do anything with a Funcref or a Dict on the right. */
2933 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2934 {
2935 switch (tv1->v_type)
2936 {
2937 case VAR_DICT:
2938 case VAR_FUNC:
2939 break;
2940
2941 case VAR_LIST:
2942 if (*op != '+' || tv2->v_type != VAR_LIST)
2943 break;
2944 /* List += List */
2945 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2946 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2947 return OK;
2948
2949 case VAR_NUMBER:
2950 case VAR_STRING:
2951 if (tv2->v_type == VAR_LIST)
2952 break;
2953 if (*op == '+' || *op == '-')
2954 {
2955 /* nr += nr or nr -= nr*/
2956 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002957#ifdef FEAT_FLOAT
2958 if (tv2->v_type == VAR_FLOAT)
2959 {
2960 float_T f = n;
2961
2962 if (*op == '+')
2963 f += tv2->vval.v_float;
2964 else
2965 f -= tv2->vval.v_float;
2966 clear_tv(tv1);
2967 tv1->v_type = VAR_FLOAT;
2968 tv1->vval.v_float = f;
2969 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002971#endif
2972 {
2973 if (*op == '+')
2974 n += get_tv_number(tv2);
2975 else
2976 n -= get_tv_number(tv2);
2977 clear_tv(tv1);
2978 tv1->v_type = VAR_NUMBER;
2979 tv1->vval.v_number = n;
2980 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002981 }
2982 else
2983 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002984 if (tv2->v_type == VAR_FLOAT)
2985 break;
2986
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002987 /* str .= str */
2988 s = get_tv_string(tv1);
2989 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2990 clear_tv(tv1);
2991 tv1->v_type = VAR_STRING;
2992 tv1->vval.v_string = s;
2993 }
2994 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002995
2996#ifdef FEAT_FLOAT
2997 case VAR_FLOAT:
2998 {
2999 float_T f;
3000
3001 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3002 && tv2->v_type != VAR_NUMBER
3003 && tv2->v_type != VAR_STRING))
3004 break;
3005 if (tv2->v_type == VAR_FLOAT)
3006 f = tv2->vval.v_float;
3007 else
3008 f = get_tv_number(tv2);
3009 if (*op == '+')
3010 tv1->vval.v_float += f;
3011 else
3012 tv1->vval.v_float -= f;
3013 }
3014 return OK;
3015#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003016 }
3017 }
3018
3019 EMSG2(_(e_letwrong), op);
3020 return FAIL;
3021}
3022
3023/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003024 * Add a watcher to a list.
3025 */
3026 static void
3027list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003028 list_T *l;
3029 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003030{
3031 lw->lw_next = l->lv_watch;
3032 l->lv_watch = lw;
3033}
3034
3035/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003036 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003037 * No warning when it isn't found...
3038 */
3039 static void
3040list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003041 list_T *l;
3042 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003043{
Bram Moolenaar33570922005-01-25 22:26:29 +00003044 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003045
3046 lwp = &l->lv_watch;
3047 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3048 {
3049 if (lw == lwrem)
3050 {
3051 *lwp = lw->lw_next;
3052 break;
3053 }
3054 lwp = &lw->lw_next;
3055 }
3056}
3057
3058/*
3059 * Just before removing an item from a list: advance watchers to the next
3060 * item.
3061 */
3062 static void
3063list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003064 list_T *l;
3065 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003066{
Bram Moolenaar33570922005-01-25 22:26:29 +00003067 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003068
3069 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3070 if (lw->lw_item == item)
3071 lw->lw_item = item->li_next;
3072}
3073
3074/*
3075 * Evaluate the expression used in a ":for var in expr" command.
3076 * "arg" points to "var".
3077 * Set "*errp" to TRUE for an error, FALSE otherwise;
3078 * Return a pointer that holds the info. Null when there is an error.
3079 */
3080 void *
3081eval_for_line(arg, errp, nextcmdp, skip)
3082 char_u *arg;
3083 int *errp;
3084 char_u **nextcmdp;
3085 int skip;
3086{
Bram Moolenaar33570922005-01-25 22:26:29 +00003087 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003088 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003089 typval_T tv;
3090 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003091
3092 *errp = TRUE; /* default: there is an error */
3093
Bram Moolenaar33570922005-01-25 22:26:29 +00003094 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003095 if (fi == NULL)
3096 return NULL;
3097
3098 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3099 if (expr == NULL)
3100 return fi;
3101
3102 expr = skipwhite(expr);
3103 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3104 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003105 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003106 return fi;
3107 }
3108
3109 if (skip)
3110 ++emsg_skip;
3111 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3112 {
3113 *errp = FALSE;
3114 if (!skip)
3115 {
3116 l = tv.vval.v_list;
3117 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003118 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003119 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003120 clear_tv(&tv);
3121 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003122 else
3123 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003124 /* No need to increment the refcount, it's already set for the
3125 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003126 fi->fi_list = l;
3127 list_add_watch(l, &fi->fi_lw);
3128 fi->fi_lw.lw_item = l->lv_first;
3129 }
3130 }
3131 }
3132 if (skip)
3133 --emsg_skip;
3134
3135 return fi;
3136}
3137
3138/*
3139 * Use the first item in a ":for" list. Advance to the next.
3140 * Assign the values to the variable (list). "arg" points to the first one.
3141 * Return TRUE when a valid item was found, FALSE when at end of list or
3142 * something wrong.
3143 */
3144 int
3145next_for_item(fi_void, arg)
3146 void *fi_void;
3147 char_u *arg;
3148{
Bram Moolenaar33570922005-01-25 22:26:29 +00003149 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003151 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003152
3153 item = fi->fi_lw.lw_item;
3154 if (item == NULL)
3155 result = FALSE;
3156 else
3157 {
3158 fi->fi_lw.lw_item = item->li_next;
3159 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3160 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3161 }
3162 return result;
3163}
3164
3165/*
3166 * Free the structure used to store info used by ":for".
3167 */
3168 void
3169free_for_info(fi_void)
3170 void *fi_void;
3171{
Bram Moolenaar33570922005-01-25 22:26:29 +00003172 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003174 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003175 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003177 list_unref(fi->fi_list);
3178 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003179 vim_free(fi);
3180}
3181
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3183
3184 void
3185set_context_for_expression(xp, arg, cmdidx)
3186 expand_T *xp;
3187 char_u *arg;
3188 cmdidx_T cmdidx;
3189{
3190 int got_eq = FALSE;
3191 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194 if (cmdidx == CMD_let)
3195 {
3196 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003197 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 {
3199 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003200 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201 {
3202 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003203 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204 if (vim_iswhite(*p))
3205 break;
3206 }
3207 return;
3208 }
3209 }
3210 else
3211 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3212 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 while ((xp->xp_pattern = vim_strpbrk(arg,
3214 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3215 {
3216 c = *xp->xp_pattern;
3217 if (c == '&')
3218 {
3219 c = xp->xp_pattern[1];
3220 if (c == '&')
3221 {
3222 ++xp->xp_pattern;
3223 xp->xp_context = cmdidx != CMD_let || got_eq
3224 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3225 }
3226 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003229 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3230 xp->xp_pattern += 2;
3231
3232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 }
3234 else if (c == '$')
3235 {
3236 /* environment variable */
3237 xp->xp_context = EXPAND_ENV_VARS;
3238 }
3239 else if (c == '=')
3240 {
3241 got_eq = TRUE;
3242 xp->xp_context = EXPAND_EXPRESSION;
3243 }
3244 else if (c == '<'
3245 && xp->xp_context == EXPAND_FUNCTIONS
3246 && vim_strchr(xp->xp_pattern, '(') == NULL)
3247 {
3248 /* Function name can start with "<SNR>" */
3249 break;
3250 }
3251 else if (cmdidx != CMD_let || got_eq)
3252 {
3253 if (c == '"') /* string */
3254 {
3255 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3256 if (c == '\\' && xp->xp_pattern[1] != NUL)
3257 ++xp->xp_pattern;
3258 xp->xp_context = EXPAND_NOTHING;
3259 }
3260 else if (c == '\'') /* literal string */
3261 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003262 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3264 /* skip */ ;
3265 xp->xp_context = EXPAND_NOTHING;
3266 }
3267 else if (c == '|')
3268 {
3269 if (xp->xp_pattern[1] == '|')
3270 {
3271 ++xp->xp_pattern;
3272 xp->xp_context = EXPAND_EXPRESSION;
3273 }
3274 else
3275 xp->xp_context = EXPAND_COMMANDS;
3276 }
3277 else
3278 xp->xp_context = EXPAND_EXPRESSION;
3279 }
3280 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281 /* Doesn't look like something valid, expand as an expression
3282 * anyway. */
3283 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 arg = xp->xp_pattern;
3285 if (*arg != NUL)
3286 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3287 /* skip */ ;
3288 }
3289 xp->xp_pattern = arg;
3290}
3291
3292#endif /* FEAT_CMDL_COMPL */
3293
3294/*
3295 * ":1,25call func(arg1, arg2)" function call.
3296 */
3297 void
3298ex_call(eap)
3299 exarg_T *eap;
3300{
3301 char_u *arg = eap->arg;
3302 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003304 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003306 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 linenr_T lnum;
3308 int doesrange;
3309 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003310 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003312 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003313 if (fudi.fd_newkey != NULL)
3314 {
3315 /* Still need to give an error message for missing key. */
3316 EMSG2(_(e_dictkey), fudi.fd_newkey);
3317 vim_free(fudi.fd_newkey);
3318 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003319 if (tofree == NULL)
3320 return;
3321
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003322 /* Increase refcount on dictionary, it could get deleted when evaluating
3323 * the arguments. */
3324 if (fudi.fd_dict != NULL)
3325 ++fudi.fd_dict->dv_refcount;
3326
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003327 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003328 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003329 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330
Bram Moolenaar532c7802005-01-27 14:44:31 +00003331 /* Skip white space to allow ":call func ()". Not good, but required for
3332 * backward compatibility. */
3333 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003334 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
3336 if (*startarg != '(')
3337 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003338 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 goto end;
3340 }
3341
3342 /*
3343 * When skipping, evaluate the function once, to find the end of the
3344 * arguments.
3345 * When the function takes a range, this is discovered after the first
3346 * call, and the loop is broken.
3347 */
3348 if (eap->skip)
3349 {
3350 ++emsg_skip;
3351 lnum = eap->line2; /* do it once, also with an invalid range */
3352 }
3353 else
3354 lnum = eap->line1;
3355 for ( ; lnum <= eap->line2; ++lnum)
3356 {
3357 if (!eap->skip && eap->addr_count > 0)
3358 {
3359 curwin->w_cursor.lnum = lnum;
3360 curwin->w_cursor.col = 0;
3361 }
3362 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003363 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003364 eap->line1, eap->line2, &doesrange,
3365 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
3367 failed = TRUE;
3368 break;
3369 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003370
3371 /* Handle a function returning a Funcref, Dictionary or List. */
3372 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3373 {
3374 failed = TRUE;
3375 break;
3376 }
3377
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003378 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 if (doesrange || eap->skip)
3380 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003381
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003383 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003384 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003385 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 if (aborting())
3387 break;
3388 }
3389 if (eap->skip)
3390 --emsg_skip;
3391
3392 if (!failed)
3393 {
3394 /* Check for trailing illegal characters and a following command. */
3395 if (!ends_excmd(*arg))
3396 {
3397 emsg_severe = TRUE;
3398 EMSG(_(e_trailing));
3399 }
3400 else
3401 eap->nextcmd = check_nextcmd(arg);
3402 }
3403
3404end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003405 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003406 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407}
3408
3409/*
3410 * ":unlet[!] var1 ... " command.
3411 */
3412 void
3413ex_unlet(eap)
3414 exarg_T *eap;
3415{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003416 ex_unletlock(eap, eap->arg, 0);
3417}
3418
3419/*
3420 * ":lockvar" and ":unlockvar" commands
3421 */
3422 void
3423ex_lockvar(eap)
3424 exarg_T *eap;
3425{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003427 int deep = 2;
3428
3429 if (eap->forceit)
3430 deep = -1;
3431 else if (vim_isdigit(*arg))
3432 {
3433 deep = getdigits(&arg);
3434 arg = skipwhite(arg);
3435 }
3436
3437 ex_unletlock(eap, arg, deep);
3438}
3439
3440/*
3441 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3442 */
3443 static void
3444ex_unletlock(eap, argstart, deep)
3445 exarg_T *eap;
3446 char_u *argstart;
3447 int deep;
3448{
3449 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003452 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454 do
3455 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003456 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003457 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3458 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003459 if (lv.ll_name == NULL)
3460 error = TRUE; /* error but continue parsing */
3461 if (name_end == NULL || (!vim_iswhite(*name_end)
3462 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003464 if (name_end != NULL)
3465 {
3466 emsg_severe = TRUE;
3467 EMSG(_(e_trailing));
3468 }
3469 if (!(eap->skip || error))
3470 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 break;
3472 }
3473
3474 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003475 {
3476 if (eap->cmdidx == CMD_unlet)
3477 {
3478 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3479 error = TRUE;
3480 }
3481 else
3482 {
3483 if (do_lock_var(&lv, name_end, deep,
3484 eap->cmdidx == CMD_lockvar) == FAIL)
3485 error = TRUE;
3486 }
3487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003489 if (!eap->skip)
3490 clear_lval(&lv);
3491
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 arg = skipwhite(name_end);
3493 } while (!ends_excmd(*arg));
3494
3495 eap->nextcmd = check_nextcmd(arg);
3496}
3497
Bram Moolenaar8c711452005-01-14 21:53:12 +00003498 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003499do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003500 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003501 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003502 int forceit;
3503{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003504 int ret = OK;
3505 int cc;
3506
3507 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003508 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003509 cc = *name_end;
3510 *name_end = NUL;
3511
3512 /* Normal name or expanded name. */
3513 if (check_changedtick(lp->ll_name))
3514 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003515 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003516 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003517 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003518 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003519 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3520 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003521 else if (lp->ll_range)
3522 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003523 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003524
3525 /* Delete a range of List items. */
3526 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3527 {
3528 li = lp->ll_li->li_next;
3529 listitem_remove(lp->ll_list, lp->ll_li);
3530 lp->ll_li = li;
3531 ++lp->ll_n1;
3532 }
3533 }
3534 else
3535 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003536 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003537 /* unlet a List item. */
3538 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003539 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003540 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003541 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003542 }
3543
3544 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003545}
3546
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547/*
3548 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003549 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 */
3551 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003552do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003554 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555{
Bram Moolenaar33570922005-01-25 22:26:29 +00003556 hashtab_T *ht;
3557 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003558 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003559 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
Bram Moolenaar33570922005-01-25 22:26:29 +00003561 ht = find_var_ht(name, &varname);
3562 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003564 hi = hash_find(ht, varname);
3565 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003566 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003567 di = HI2DI(hi);
3568 if (var_check_fixed(di->di_flags, name)
3569 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003570 return FAIL;
3571 delete_var(ht, hi);
3572 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003575 if (forceit)
3576 return OK;
3577 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 return FAIL;
3579}
3580
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003581/*
3582 * Lock or unlock variable indicated by "lp".
3583 * "deep" is the levels to go (-1 for unlimited);
3584 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3585 */
3586 static int
3587do_lock_var(lp, name_end, deep, lock)
3588 lval_T *lp;
3589 char_u *name_end;
3590 int deep;
3591 int lock;
3592{
3593 int ret = OK;
3594 int cc;
3595 dictitem_T *di;
3596
3597 if (deep == 0) /* nothing to do */
3598 return OK;
3599
3600 if (lp->ll_tv == NULL)
3601 {
3602 cc = *name_end;
3603 *name_end = NUL;
3604
3605 /* Normal name or expanded name. */
3606 if (check_changedtick(lp->ll_name))
3607 ret = FAIL;
3608 else
3609 {
3610 di = find_var(lp->ll_name, NULL);
3611 if (di == NULL)
3612 ret = FAIL;
3613 else
3614 {
3615 if (lock)
3616 di->di_flags |= DI_FLAGS_LOCK;
3617 else
3618 di->di_flags &= ~DI_FLAGS_LOCK;
3619 item_lock(&di->di_tv, deep, lock);
3620 }
3621 }
3622 *name_end = cc;
3623 }
3624 else if (lp->ll_range)
3625 {
3626 listitem_T *li = lp->ll_li;
3627
3628 /* (un)lock a range of List items. */
3629 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3630 {
3631 item_lock(&li->li_tv, deep, lock);
3632 li = li->li_next;
3633 ++lp->ll_n1;
3634 }
3635 }
3636 else if (lp->ll_list != NULL)
3637 /* (un)lock a List item. */
3638 item_lock(&lp->ll_li->li_tv, deep, lock);
3639 else
3640 /* un(lock) a Dictionary item. */
3641 item_lock(&lp->ll_di->di_tv, deep, lock);
3642
3643 return ret;
3644}
3645
3646/*
3647 * Lock or unlock an item. "deep" is nr of levels to go.
3648 */
3649 static void
3650item_lock(tv, deep, lock)
3651 typval_T *tv;
3652 int deep;
3653 int lock;
3654{
3655 static int recurse = 0;
3656 list_T *l;
3657 listitem_T *li;
3658 dict_T *d;
3659 hashitem_T *hi;
3660 int todo;
3661
3662 if (recurse >= DICT_MAXNEST)
3663 {
3664 EMSG(_("E743: variable nested too deep for (un)lock"));
3665 return;
3666 }
3667 if (deep == 0)
3668 return;
3669 ++recurse;
3670
3671 /* lock/unlock the item itself */
3672 if (lock)
3673 tv->v_lock |= VAR_LOCKED;
3674 else
3675 tv->v_lock &= ~VAR_LOCKED;
3676
3677 switch (tv->v_type)
3678 {
3679 case VAR_LIST:
3680 if ((l = tv->vval.v_list) != NULL)
3681 {
3682 if (lock)
3683 l->lv_lock |= VAR_LOCKED;
3684 else
3685 l->lv_lock &= ~VAR_LOCKED;
3686 if (deep < 0 || deep > 1)
3687 /* recursive: lock/unlock the items the List contains */
3688 for (li = l->lv_first; li != NULL; li = li->li_next)
3689 item_lock(&li->li_tv, deep - 1, lock);
3690 }
3691 break;
3692 case VAR_DICT:
3693 if ((d = tv->vval.v_dict) != NULL)
3694 {
3695 if (lock)
3696 d->dv_lock |= VAR_LOCKED;
3697 else
3698 d->dv_lock &= ~VAR_LOCKED;
3699 if (deep < 0 || deep > 1)
3700 {
3701 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003702 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003703 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3704 {
3705 if (!HASHITEM_EMPTY(hi))
3706 {
3707 --todo;
3708 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3709 }
3710 }
3711 }
3712 }
3713 }
3714 --recurse;
3715}
3716
Bram Moolenaara40058a2005-07-11 22:42:07 +00003717/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003718 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3719 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003720 */
3721 static int
3722tv_islocked(tv)
3723 typval_T *tv;
3724{
3725 return (tv->v_lock & VAR_LOCKED)
3726 || (tv->v_type == VAR_LIST
3727 && tv->vval.v_list != NULL
3728 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3729 || (tv->v_type == VAR_DICT
3730 && tv->vval.v_dict != NULL
3731 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3732}
3733
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3735/*
3736 * Delete all "menutrans_" variables.
3737 */
3738 void
3739del_menutrans_vars()
3740{
Bram Moolenaar33570922005-01-25 22:26:29 +00003741 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003742 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
Bram Moolenaar33570922005-01-25 22:26:29 +00003744 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003745 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003746 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003747 {
3748 if (!HASHITEM_EMPTY(hi))
3749 {
3750 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003751 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3752 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003753 }
3754 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003755 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756}
3757#endif
3758
3759#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3760
3761/*
3762 * Local string buffer for the next two functions to store a variable name
3763 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3764 * get_user_var_name().
3765 */
3766
3767static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3768
3769static char_u *varnamebuf = NULL;
3770static int varnamebuflen = 0;
3771
3772/*
3773 * Function to concatenate a prefix and a variable name.
3774 */
3775 static char_u *
3776cat_prefix_varname(prefix, name)
3777 int prefix;
3778 char_u *name;
3779{
3780 int len;
3781
3782 len = (int)STRLEN(name) + 3;
3783 if (len > varnamebuflen)
3784 {
3785 vim_free(varnamebuf);
3786 len += 10; /* some additional space */
3787 varnamebuf = alloc(len);
3788 if (varnamebuf == NULL)
3789 {
3790 varnamebuflen = 0;
3791 return NULL;
3792 }
3793 varnamebuflen = len;
3794 }
3795 *varnamebuf = prefix;
3796 varnamebuf[1] = ':';
3797 STRCPY(varnamebuf + 2, name);
3798 return varnamebuf;
3799}
3800
3801/*
3802 * Function given to ExpandGeneric() to obtain the list of user defined
3803 * (global/buffer/window/built-in) variable names.
3804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 char_u *
3806get_user_var_name(xp, idx)
3807 expand_T *xp;
3808 int idx;
3809{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003810 static long_u gdone;
3811 static long_u bdone;
3812 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003813#ifdef FEAT_WINDOWS
3814 static long_u tdone;
3815#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003816 static int vidx;
3817 static hashitem_T *hi;
3818 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
3820 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003821 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003822 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003823#ifdef FEAT_WINDOWS
3824 tdone = 0;
3825#endif
3826 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003827
3828 /* Global variables */
3829 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003831 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003832 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003833 else
3834 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003835 while (HASHITEM_EMPTY(hi))
3836 ++hi;
3837 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3838 return cat_prefix_varname('g', hi->hi_key);
3839 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003841
3842 /* b: variables */
3843 ht = &curbuf->b_vars.dv_hashtab;
3844 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003846 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003847 hi = ht->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 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003854 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003856 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 return (char_u *)"b:changedtick";
3858 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003859
3860 /* w: variables */
3861 ht = &curwin->w_vars.dv_hashtab;
3862 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003864 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003865 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003866 else
3867 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003868 while (HASHITEM_EMPTY(hi))
3869 ++hi;
3870 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003872
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003873#ifdef FEAT_WINDOWS
3874 /* t: variables */
3875 ht = &curtab->tp_vars.dv_hashtab;
3876 if (tdone < ht->ht_used)
3877 {
3878 if (tdone++ == 0)
3879 hi = ht->ht_array;
3880 else
3881 ++hi;
3882 while (HASHITEM_EMPTY(hi))
3883 ++hi;
3884 return cat_prefix_varname('t', hi->hi_key);
3885 }
3886#endif
3887
Bram Moolenaar33570922005-01-25 22:26:29 +00003888 /* v: variables */
3889 if (vidx < VV_LEN)
3890 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
3892 vim_free(varnamebuf);
3893 varnamebuf = NULL;
3894 varnamebuflen = 0;
3895 return NULL;
3896}
3897
3898#endif /* FEAT_CMDL_COMPL */
3899
3900/*
3901 * types for expressions.
3902 */
3903typedef enum
3904{
3905 TYPE_UNKNOWN = 0
3906 , TYPE_EQUAL /* == */
3907 , TYPE_NEQUAL /* != */
3908 , TYPE_GREATER /* > */
3909 , TYPE_GEQUAL /* >= */
3910 , TYPE_SMALLER /* < */
3911 , TYPE_SEQUAL /* <= */
3912 , TYPE_MATCH /* =~ */
3913 , TYPE_NOMATCH /* !~ */
3914} exptype_T;
3915
3916/*
3917 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003918 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3920 */
3921
3922/*
3923 * Handle zero level expression.
3924 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003925 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003926 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 * Return OK or FAIL.
3928 */
3929 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003930eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 char_u **nextcmd;
3934 int evaluate;
3935{
3936 int ret;
3937 char_u *p;
3938
3939 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003940 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 if (ret == FAIL || !ends_excmd(*p))
3942 {
3943 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003944 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 /*
3946 * Report the invalid expression unless the expression evaluation has
3947 * been cancelled due to an aborting error, an interrupt, or an
3948 * exception.
3949 */
3950 if (!aborting())
3951 EMSG2(_(e_invexpr2), arg);
3952 ret = FAIL;
3953 }
3954 if (nextcmd != NULL)
3955 *nextcmd = check_nextcmd(p);
3956
3957 return ret;
3958}
3959
3960/*
3961 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003962 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 *
3964 * "arg" must point to the first non-white of the expression.
3965 * "arg" is advanced to the next non-white after the recognized expression.
3966 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003967 * Note: "rettv.v_lock" is not set.
3968 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 * Return OK or FAIL.
3970 */
3971 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003972eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003974 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 int evaluate;
3976{
3977 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003978 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979
3980 /*
3981 * Get the first variable.
3982 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003983 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 return FAIL;
3985
3986 if ((*arg)[0] == '?')
3987 {
3988 result = FALSE;
3989 if (evaluate)
3990 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003991 int error = FALSE;
3992
3993 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003995 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003996 if (error)
3997 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
3999
4000 /*
4001 * Get the second variable.
4002 */
4003 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004004 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 return FAIL;
4006
4007 /*
4008 * Check for the ":".
4009 */
4010 if ((*arg)[0] != ':')
4011 {
4012 EMSG(_("E109: Missing ':' after '?'"));
4013 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004014 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 return FAIL;
4016 }
4017
4018 /*
4019 * Get the third variable.
4020 */
4021 *arg = skipwhite(*arg + 1);
4022 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4023 {
4024 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004025 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 return FAIL;
4027 }
4028 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004029 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
4031
4032 return OK;
4033}
4034
4035/*
4036 * Handle first level expression:
4037 * expr2 || expr2 || expr2 logical OR
4038 *
4039 * "arg" must point to the first non-white of the expression.
4040 * "arg" is advanced to the next non-white after the recognized expression.
4041 *
4042 * Return OK or FAIL.
4043 */
4044 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004045eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 int evaluate;
4049{
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 long result;
4052 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004053 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
4055 /*
4056 * Get the first variable.
4057 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004058 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 return FAIL;
4060
4061 /*
4062 * Repeat until there is no following "||".
4063 */
4064 first = TRUE;
4065 result = FALSE;
4066 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4067 {
4068 if (evaluate && first)
4069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004070 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004072 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004073 if (error)
4074 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 first = FALSE;
4076 }
4077
4078 /*
4079 * Get the second variable.
4080 */
4081 *arg = skipwhite(*arg + 2);
4082 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4083 return FAIL;
4084
4085 /*
4086 * Compute the result.
4087 */
4088 if (evaluate && !result)
4089 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004090 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004093 if (error)
4094 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
4096 if (evaluate)
4097 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 rettv->v_type = VAR_NUMBER;
4099 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101 }
4102
4103 return OK;
4104}
4105
4106/*
4107 * Handle second level expression:
4108 * expr3 && expr3 && expr3 logical AND
4109 *
4110 * "arg" must point to the first non-white of the expression.
4111 * "arg" is advanced to the next non-white after the recognized expression.
4112 *
4113 * Return OK or FAIL.
4114 */
4115 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 int evaluate;
4120{
Bram Moolenaar33570922005-01-25 22:26:29 +00004121 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 long result;
4123 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004124 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125
4126 /*
4127 * Get the first variable.
4128 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 return FAIL;
4131
4132 /*
4133 * Repeat until there is no following "&&".
4134 */
4135 first = TRUE;
4136 result = TRUE;
4137 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4138 {
4139 if (evaluate && first)
4140 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004141 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004144 if (error)
4145 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 first = FALSE;
4147 }
4148
4149 /*
4150 * Get the second variable.
4151 */
4152 *arg = skipwhite(*arg + 2);
4153 if (eval4(arg, &var2, evaluate && result) == FAIL)
4154 return FAIL;
4155
4156 /*
4157 * Compute the result.
4158 */
4159 if (evaluate && result)
4160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004161 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004163 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 if (error)
4165 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
4167 if (evaluate)
4168 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 rettv->v_type = VAR_NUMBER;
4170 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 }
4172 }
4173
4174 return OK;
4175}
4176
4177/*
4178 * Handle third level expression:
4179 * var1 == var2
4180 * var1 =~ var2
4181 * var1 != var2
4182 * var1 !~ var2
4183 * var1 > var2
4184 * var1 >= var2
4185 * var1 < var2
4186 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004187 * var1 is var2
4188 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 *
4190 * "arg" must point to the first non-white of the expression.
4191 * "arg" is advanced to the next non-white after the recognized expression.
4192 *
4193 * Return OK or FAIL.
4194 */
4195 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004196eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 int evaluate;
4200{
Bram Moolenaar33570922005-01-25 22:26:29 +00004201 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 char_u *p;
4203 int i;
4204 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004205 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 int len = 2;
4207 long n1, n2;
4208 char_u *s1, *s2;
4209 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4210 regmatch_T regmatch;
4211 int ic;
4212 char_u *save_cpo;
4213
4214 /*
4215 * Get the first variable.
4216 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004217 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 return FAIL;
4219
4220 p = *arg;
4221 switch (p[0])
4222 {
4223 case '=': if (p[1] == '=')
4224 type = TYPE_EQUAL;
4225 else if (p[1] == '~')
4226 type = TYPE_MATCH;
4227 break;
4228 case '!': if (p[1] == '=')
4229 type = TYPE_NEQUAL;
4230 else if (p[1] == '~')
4231 type = TYPE_NOMATCH;
4232 break;
4233 case '>': if (p[1] != '=')
4234 {
4235 type = TYPE_GREATER;
4236 len = 1;
4237 }
4238 else
4239 type = TYPE_GEQUAL;
4240 break;
4241 case '<': if (p[1] != '=')
4242 {
4243 type = TYPE_SMALLER;
4244 len = 1;
4245 }
4246 else
4247 type = TYPE_SEQUAL;
4248 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004249 case 'i': if (p[1] == 's')
4250 {
4251 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4252 len = 5;
4253 if (!vim_isIDc(p[len]))
4254 {
4255 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4256 type_is = TRUE;
4257 }
4258 }
4259 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 }
4261
4262 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004263 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 */
4265 if (type != TYPE_UNKNOWN)
4266 {
4267 /* extra question mark appended: ignore case */
4268 if (p[len] == '?')
4269 {
4270 ic = TRUE;
4271 ++len;
4272 }
4273 /* extra '#' appended: match case */
4274 else if (p[len] == '#')
4275 {
4276 ic = FALSE;
4277 ++len;
4278 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004279 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 else
4281 ic = p_ic;
4282
4283 /*
4284 * Get the second variable.
4285 */
4286 *arg = skipwhite(p + len);
4287 if (eval5(arg, &var2, evaluate) == FAIL)
4288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 return FAIL;
4291 }
4292
4293 if (evaluate)
4294 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004295 if (type_is && rettv->v_type != var2.v_type)
4296 {
4297 /* For "is" a different type always means FALSE, for "notis"
4298 * it means TRUE. */
4299 n1 = (type == TYPE_NEQUAL);
4300 }
4301 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4302 {
4303 if (type_is)
4304 {
4305 n1 = (rettv->v_type == var2.v_type
4306 && rettv->vval.v_list == var2.vval.v_list);
4307 if (type == TYPE_NEQUAL)
4308 n1 = !n1;
4309 }
4310 else if (rettv->v_type != var2.v_type
4311 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4312 {
4313 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004314 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004315 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004316 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004317 clear_tv(rettv);
4318 clear_tv(&var2);
4319 return FAIL;
4320 }
4321 else
4322 {
4323 /* Compare two Lists for being equal or unequal. */
4324 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4325 if (type == TYPE_NEQUAL)
4326 n1 = !n1;
4327 }
4328 }
4329
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004330 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4331 {
4332 if (type_is)
4333 {
4334 n1 = (rettv->v_type == var2.v_type
4335 && rettv->vval.v_dict == var2.vval.v_dict);
4336 if (type == TYPE_NEQUAL)
4337 n1 = !n1;
4338 }
4339 else if (rettv->v_type != var2.v_type
4340 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4341 {
4342 if (rettv->v_type != var2.v_type)
4343 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4344 else
4345 EMSG(_("E736: Invalid operation for Dictionary"));
4346 clear_tv(rettv);
4347 clear_tv(&var2);
4348 return FAIL;
4349 }
4350 else
4351 {
4352 /* Compare two Dictionaries for being equal or unequal. */
4353 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4354 if (type == TYPE_NEQUAL)
4355 n1 = !n1;
4356 }
4357 }
4358
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004359 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4360 {
4361 if (rettv->v_type != var2.v_type
4362 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4363 {
4364 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004365 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004366 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004367 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004368 clear_tv(rettv);
4369 clear_tv(&var2);
4370 return FAIL;
4371 }
4372 else
4373 {
4374 /* Compare two Funcrefs for being equal or unequal. */
4375 if (rettv->vval.v_string == NULL
4376 || var2.vval.v_string == NULL)
4377 n1 = FALSE;
4378 else
4379 n1 = STRCMP(rettv->vval.v_string,
4380 var2.vval.v_string) == 0;
4381 if (type == TYPE_NEQUAL)
4382 n1 = !n1;
4383 }
4384 }
4385
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004386#ifdef FEAT_FLOAT
4387 /*
4388 * If one of the two variables is a float, compare as a float.
4389 * When using "=~" or "!~", always compare as string.
4390 */
4391 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4392 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4393 {
4394 float_T f1, f2;
4395
4396 if (rettv->v_type == VAR_FLOAT)
4397 f1 = rettv->vval.v_float;
4398 else
4399 f1 = get_tv_number(rettv);
4400 if (var2.v_type == VAR_FLOAT)
4401 f2 = var2.vval.v_float;
4402 else
4403 f2 = get_tv_number(&var2);
4404 n1 = FALSE;
4405 switch (type)
4406 {
4407 case TYPE_EQUAL: n1 = (f1 == f2); break;
4408 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4409 case TYPE_GREATER: n1 = (f1 > f2); break;
4410 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4411 case TYPE_SMALLER: n1 = (f1 < f2); break;
4412 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4413 case TYPE_UNKNOWN:
4414 case TYPE_MATCH:
4415 case TYPE_NOMATCH: break; /* avoid gcc warning */
4416 }
4417 }
4418#endif
4419
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 /*
4421 * If one of the two variables is a number, compare as a number.
4422 * When using "=~" or "!~", always compare as string.
4423 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4426 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004427 n1 = get_tv_number(rettv);
4428 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 switch (type)
4430 {
4431 case TYPE_EQUAL: n1 = (n1 == n2); break;
4432 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4433 case TYPE_GREATER: n1 = (n1 > n2); break;
4434 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4435 case TYPE_SMALLER: n1 = (n1 < n2); break;
4436 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4437 case TYPE_UNKNOWN:
4438 case TYPE_MATCH:
4439 case TYPE_NOMATCH: break; /* avoid gcc warning */
4440 }
4441 }
4442 else
4443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004444 s1 = get_tv_string_buf(rettv, buf1);
4445 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4447 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4448 else
4449 i = 0;
4450 n1 = FALSE;
4451 switch (type)
4452 {
4453 case TYPE_EQUAL: n1 = (i == 0); break;
4454 case TYPE_NEQUAL: n1 = (i != 0); break;
4455 case TYPE_GREATER: n1 = (i > 0); break;
4456 case TYPE_GEQUAL: n1 = (i >= 0); break;
4457 case TYPE_SMALLER: n1 = (i < 0); break;
4458 case TYPE_SEQUAL: n1 = (i <= 0); break;
4459
4460 case TYPE_MATCH:
4461 case TYPE_NOMATCH:
4462 /* avoid 'l' flag in 'cpoptions' */
4463 save_cpo = p_cpo;
4464 p_cpo = (char_u *)"";
4465 regmatch.regprog = vim_regcomp(s2,
4466 RE_MAGIC + RE_STRING);
4467 regmatch.rm_ic = ic;
4468 if (regmatch.regprog != NULL)
4469 {
4470 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4471 vim_free(regmatch.regprog);
4472 if (type == TYPE_NOMATCH)
4473 n1 = !n1;
4474 }
4475 p_cpo = save_cpo;
4476 break;
4477
4478 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4479 }
4480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 clear_tv(rettv);
4482 clear_tv(&var2);
4483 rettv->v_type = VAR_NUMBER;
4484 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
4486 }
4487
4488 return OK;
4489}
4490
4491/*
4492 * Handle fourth level expression:
4493 * + number addition
4494 * - number subtraction
4495 * . string concatenation
4496 *
4497 * "arg" must point to the first non-white of the expression.
4498 * "arg" is advanced to the next non-white after the recognized expression.
4499 *
4500 * Return OK or FAIL.
4501 */
4502 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004503eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 int evaluate;
4507{
Bram Moolenaar33570922005-01-25 22:26:29 +00004508 typval_T var2;
4509 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 int op;
4511 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004512#ifdef FEAT_FLOAT
4513 float_T f1 = 0, f2 = 0;
4514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 char_u *s1, *s2;
4516 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4517 char_u *p;
4518
4519 /*
4520 * Get the first variable.
4521 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004522 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 return FAIL;
4524
4525 /*
4526 * Repeat computing, until no '+', '-' or '.' is following.
4527 */
4528 for (;;)
4529 {
4530 op = **arg;
4531 if (op != '+' && op != '-' && op != '.')
4532 break;
4533
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004534 if ((op != '+' || rettv->v_type != VAR_LIST)
4535#ifdef FEAT_FLOAT
4536 && (op == '.' || rettv->v_type != VAR_FLOAT)
4537#endif
4538 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004539 {
4540 /* For "list + ...", an illegal use of the first operand as
4541 * a number cannot be determined before evaluating the 2nd
4542 * operand: if this is also a list, all is ok.
4543 * For "something . ...", "something - ..." or "non-list + ...",
4544 * we know that the first operand needs to be a string or number
4545 * without evaluating the 2nd operand. So check before to avoid
4546 * side effects after an error. */
4547 if (evaluate && get_tv_string_chk(rettv) == NULL)
4548 {
4549 clear_tv(rettv);
4550 return FAIL;
4551 }
4552 }
4553
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 /*
4555 * Get the second variable.
4556 */
4557 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004558 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004560 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 return FAIL;
4562 }
4563
4564 if (evaluate)
4565 {
4566 /*
4567 * Compute the result.
4568 */
4569 if (op == '.')
4570 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004571 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4572 s2 = get_tv_string_buf_chk(&var2, buf2);
4573 if (s2 == NULL) /* type error ? */
4574 {
4575 clear_tv(rettv);
4576 clear_tv(&var2);
4577 return FAIL;
4578 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004579 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004580 clear_tv(rettv);
4581 rettv->v_type = VAR_STRING;
4582 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004584 else if (op == '+' && rettv->v_type == VAR_LIST
4585 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004586 {
4587 /* concatenate Lists */
4588 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4589 &var3) == FAIL)
4590 {
4591 clear_tv(rettv);
4592 clear_tv(&var2);
4593 return FAIL;
4594 }
4595 clear_tv(rettv);
4596 *rettv = var3;
4597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 else
4599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004600 int error = FALSE;
4601
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004602#ifdef FEAT_FLOAT
4603 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004604 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004605 f1 = rettv->vval.v_float;
4606 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004607 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004608 else
4609#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004610 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004611 n1 = get_tv_number_chk(rettv, &error);
4612 if (error)
4613 {
4614 /* This can only happen for "list + non-list". For
4615 * "non-list + ..." or "something - ...", we returned
4616 * before evaluating the 2nd operand. */
4617 clear_tv(rettv);
4618 return FAIL;
4619 }
4620#ifdef FEAT_FLOAT
4621 if (var2.v_type == VAR_FLOAT)
4622 f1 = n1;
4623#endif
4624 }
4625#ifdef FEAT_FLOAT
4626 if (var2.v_type == VAR_FLOAT)
4627 {
4628 f2 = var2.vval.v_float;
4629 n2 = 0;
4630 }
4631 else
4632#endif
4633 {
4634 n2 = get_tv_number_chk(&var2, &error);
4635 if (error)
4636 {
4637 clear_tv(rettv);
4638 clear_tv(&var2);
4639 return FAIL;
4640 }
4641#ifdef FEAT_FLOAT
4642 if (rettv->v_type == VAR_FLOAT)
4643 f2 = n2;
4644#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004645 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004646 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004647
4648#ifdef FEAT_FLOAT
4649 /* If there is a float on either side the result is a float. */
4650 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4651 {
4652 if (op == '+')
4653 f1 = f1 + f2;
4654 else
4655 f1 = f1 - f2;
4656 rettv->v_type = VAR_FLOAT;
4657 rettv->vval.v_float = f1;
4658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004660#endif
4661 {
4662 if (op == '+')
4663 n1 = n1 + n2;
4664 else
4665 n1 = n1 - n2;
4666 rettv->v_type = VAR_NUMBER;
4667 rettv->vval.v_number = n1;
4668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004670 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 }
4672 }
4673 return OK;
4674}
4675
4676/*
4677 * Handle fifth level expression:
4678 * * number multiplication
4679 * / number division
4680 * % number modulo
4681 *
4682 * "arg" must point to the first non-white of the expression.
4683 * "arg" is advanced to the next non-white after the recognized expression.
4684 *
4685 * Return OK or FAIL.
4686 */
4687 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004688eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004692 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
Bram Moolenaar33570922005-01-25 22:26:29 +00004694 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 int op;
4696 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004697#ifdef FEAT_FLOAT
4698 int use_float = FALSE;
4699 float_T f1 = 0, f2;
4700#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004701 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
4703 /*
4704 * Get the first variable.
4705 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004706 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 return FAIL;
4708
4709 /*
4710 * Repeat computing, until no '*', '/' or '%' is following.
4711 */
4712 for (;;)
4713 {
4714 op = **arg;
4715 if (op != '*' && op != '/' && op != '%')
4716 break;
4717
4718 if (evaluate)
4719 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004720#ifdef FEAT_FLOAT
4721 if (rettv->v_type == VAR_FLOAT)
4722 {
4723 f1 = rettv->vval.v_float;
4724 use_float = TRUE;
4725 n1 = 0;
4726 }
4727 else
4728#endif
4729 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004730 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004731 if (error)
4732 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 }
4734 else
4735 n1 = 0;
4736
4737 /*
4738 * Get the second variable.
4739 */
4740 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004741 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 return FAIL;
4743
4744 if (evaluate)
4745 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004746#ifdef FEAT_FLOAT
4747 if (var2.v_type == VAR_FLOAT)
4748 {
4749 if (!use_float)
4750 {
4751 f1 = n1;
4752 use_float = TRUE;
4753 }
4754 f2 = var2.vval.v_float;
4755 n2 = 0;
4756 }
4757 else
4758#endif
4759 {
4760 n2 = get_tv_number_chk(&var2, &error);
4761 clear_tv(&var2);
4762 if (error)
4763 return FAIL;
4764#ifdef FEAT_FLOAT
4765 if (use_float)
4766 f2 = n2;
4767#endif
4768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769
4770 /*
4771 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004772 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004774#ifdef FEAT_FLOAT
4775 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004777 if (op == '*')
4778 f1 = f1 * f2;
4779 else if (op == '/')
4780 {
4781 /* We rely on the floating point library to handle divide
4782 * by zero to result in "inf" and not a crash. */
4783 f1 = f1 / f2;
4784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004787 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004788 return FAIL;
4789 }
4790 rettv->v_type = VAR_FLOAT;
4791 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 }
4793 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796 if (op == '*')
4797 n1 = n1 * n2;
4798 else if (op == '/')
4799 {
4800 if (n2 == 0) /* give an error message? */
4801 {
4802 if (n1 == 0)
4803 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4804 else if (n1 < 0)
4805 n1 = -0x7fffffffL;
4806 else
4807 n1 = 0x7fffffffL;
4808 }
4809 else
4810 n1 = n1 / n2;
4811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004813 {
4814 if (n2 == 0) /* give an error message? */
4815 n1 = 0;
4816 else
4817 n1 = n1 % n2;
4818 }
4819 rettv->v_type = VAR_NUMBER;
4820 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 }
4823 }
4824
4825 return OK;
4826}
4827
4828/*
4829 * Handle sixth level expression:
4830 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004831 * "string" string constant
4832 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 * &option-name option value
4834 * @r register contents
4835 * identifier variable value
4836 * function() function call
4837 * $VAR environment variable
4838 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004839 * [expr, expr] List
4840 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 *
4842 * Also handle:
4843 * ! in front logical NOT
4844 * - in front unary minus
4845 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004846 * trailing [] subscript in String or List
4847 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 *
4849 * "arg" must point to the first non-white of the expression.
4850 * "arg" is advanced to the next non-white after the recognized expression.
4851 *
4852 * Return OK or FAIL.
4853 */
4854 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004855eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004859 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 long n;
4862 int len;
4863 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 char_u *start_leader, *end_leader;
4865 int ret = OK;
4866 char_u *alias;
4867
4868 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004869 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004870 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004872 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873
4874 /*
4875 * Skip '!' and '-' characters. They are handled later.
4876 */
4877 start_leader = *arg;
4878 while (**arg == '!' || **arg == '-' || **arg == '+')
4879 *arg = skipwhite(*arg + 1);
4880 end_leader = *arg;
4881
4882 switch (**arg)
4883 {
4884 /*
4885 * Number constant.
4886 */
4887 case '0':
4888 case '1':
4889 case '2':
4890 case '3':
4891 case '4':
4892 case '5':
4893 case '6':
4894 case '7':
4895 case '8':
4896 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897 {
4898#ifdef FEAT_FLOAT
4899 char_u *p = skipdigits(*arg + 1);
4900 int get_float = FALSE;
4901
4902 /* We accept a float when the format matches
4903 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004904 * strict to avoid backwards compatibility problems.
4905 * Don't look for a float after the "." operator, so that
4906 * ":let vers = 1.2.3" doesn't fail. */
4907 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004909 get_float = TRUE;
4910 p = skipdigits(p + 2);
4911 if (*p == 'e' || *p == 'E')
4912 {
4913 ++p;
4914 if (*p == '-' || *p == '+')
4915 ++p;
4916 if (!vim_isdigit(*p))
4917 get_float = FALSE;
4918 else
4919 p = skipdigits(p + 1);
4920 }
4921 if (ASCII_ISALPHA(*p) || *p == '.')
4922 get_float = FALSE;
4923 }
4924 if (get_float)
4925 {
4926 float_T f;
4927
4928 *arg += string2float(*arg, &f);
4929 if (evaluate)
4930 {
4931 rettv->v_type = VAR_FLOAT;
4932 rettv->vval.v_float = f;
4933 }
4934 }
4935 else
4936#endif
4937 {
4938 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4939 *arg += len;
4940 if (evaluate)
4941 {
4942 rettv->v_type = VAR_NUMBER;
4943 rettv->vval.v_number = n;
4944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948
4949 /*
4950 * String constant: "string".
4951 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004952 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 break;
4954
4955 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004956 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004958 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004959 break;
4960
4961 /*
4962 * List: [expr, expr]
4963 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004964 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 break;
4966
4967 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968 * Dictionary: {key: val, key: val}
4969 */
4970 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4971 break;
4972
4973 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004974 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004976 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 break;
4978
4979 /*
4980 * Environment variable: $VAR.
4981 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004982 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 break;
4984
4985 /*
4986 * Register contents: @r.
4987 */
4988 case '@': ++*arg;
4989 if (evaluate)
4990 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004991 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004992 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 }
4994 if (**arg != NUL)
4995 ++*arg;
4996 break;
4997
4998 /*
4999 * nested expression: (expression).
5000 */
5001 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005002 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 if (**arg == ')')
5004 ++*arg;
5005 else if (ret == OK)
5006 {
5007 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005008 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 ret = FAIL;
5010 }
5011 break;
5012
Bram Moolenaar8c711452005-01-14 21:53:12 +00005013 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 break;
5015 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016
5017 if (ret == NOTDONE)
5018 {
5019 /*
5020 * Must be a variable or function name.
5021 * Can also be a curly-braces kind of name: {expr}.
5022 */
5023 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005024 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005025 if (alias != NULL)
5026 s = alias;
5027
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005028 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029 ret = FAIL;
5030 else
5031 {
5032 if (**arg == '(') /* recursive! */
5033 {
5034 /* If "s" is the name of a variable of type VAR_FUNC
5035 * use its contents. */
5036 s = deref_func_name(s, &len);
5037
5038 /* Invoke the function. */
5039 ret = get_func_tv(s, len, rettv, arg,
5040 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005041 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 /* Stop the expression evaluation when immediately
5043 * aborting on error, or when an interrupt occurred or
5044 * an exception was thrown but not caught. */
5045 if (aborting())
5046 {
5047 if (ret == OK)
5048 clear_tv(rettv);
5049 ret = FAIL;
5050 }
5051 }
5052 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005053 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005054 else
5055 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 }
5057
5058 if (alias != NULL)
5059 vim_free(alias);
5060 }
5061
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 *arg = skipwhite(*arg);
5063
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005064 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5065 * expr(expr). */
5066 if (ret == OK)
5067 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068
5069 /*
5070 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5071 */
5072 if (ret == OK && evaluate && end_leader > start_leader)
5073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005074 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005075 int val = 0;
5076#ifdef FEAT_FLOAT
5077 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005078
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005079 if (rettv->v_type == VAR_FLOAT)
5080 f = rettv->vval.v_float;
5081 else
5082#endif
5083 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005084 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005086 clear_tv(rettv);
5087 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005089 else
5090 {
5091 while (end_leader > start_leader)
5092 {
5093 --end_leader;
5094 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005095 {
5096#ifdef FEAT_FLOAT
5097 if (rettv->v_type == VAR_FLOAT)
5098 f = !f;
5099 else
5100#endif
5101 val = !val;
5102 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005103 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005104 {
5105#ifdef FEAT_FLOAT
5106 if (rettv->v_type == VAR_FLOAT)
5107 f = -f;
5108 else
5109#endif
5110 val = -val;
5111 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005112 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005113#ifdef FEAT_FLOAT
5114 if (rettv->v_type == VAR_FLOAT)
5115 {
5116 clear_tv(rettv);
5117 rettv->vval.v_float = f;
5118 }
5119 else
5120#endif
5121 {
5122 clear_tv(rettv);
5123 rettv->v_type = VAR_NUMBER;
5124 rettv->vval.v_number = val;
5125 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
5128
5129 return ret;
5130}
5131
5132/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005133 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5134 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005135 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5136 */
5137 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005138eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005139 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005140 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005141 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005142 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005143{
5144 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005145 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005146 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005147 long len = -1;
5148 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005149 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005151
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005152 if (rettv->v_type == VAR_FUNC
5153#ifdef FEAT_FLOAT
5154 || rettv->v_type == VAR_FLOAT
5155#endif
5156 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005157 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005158 if (verbose)
5159 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005160 return FAIL;
5161 }
5162
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 /*
5166 * dict.name
5167 */
5168 key = *arg + 1;
5169 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5170 ;
5171 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005172 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174 }
5175 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005176 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 /*
5178 * something[idx]
5179 *
5180 * Get the (first) variable from inside the [].
5181 */
5182 *arg = skipwhite(*arg + 1);
5183 if (**arg == ':')
5184 empty1 = TRUE;
5185 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5186 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005187 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5188 {
5189 /* not a number or string */
5190 clear_tv(&var1);
5191 return FAIL;
5192 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193
5194 /*
5195 * Get the second variable from inside the [:].
5196 */
5197 if (**arg == ':')
5198 {
5199 range = TRUE;
5200 *arg = skipwhite(*arg + 1);
5201 if (**arg == ']')
5202 empty2 = TRUE;
5203 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005205 if (!empty1)
5206 clear_tv(&var1);
5207 return FAIL;
5208 }
5209 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5210 {
5211 /* not a number or string */
5212 if (!empty1)
5213 clear_tv(&var1);
5214 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 return FAIL;
5216 }
5217 }
5218
5219 /* Check for the ']'. */
5220 if (**arg != ']')
5221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005222 if (verbose)
5223 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 clear_tv(&var1);
5225 if (range)
5226 clear_tv(&var2);
5227 return FAIL;
5228 }
5229 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005230 }
5231
5232 if (evaluate)
5233 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 n1 = 0;
5235 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005236 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005237 n1 = get_tv_number(&var1);
5238 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239 }
5240 if (range)
5241 {
5242 if (empty2)
5243 n2 = -1;
5244 else
5245 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005246 n2 = get_tv_number(&var2);
5247 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005248 }
5249 }
5250
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005251 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 {
5253 case VAR_NUMBER:
5254 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005255 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 len = (long)STRLEN(s);
5257 if (range)
5258 {
5259 /* The resulting variable is a substring. If the indexes
5260 * are out of range the result is empty. */
5261 if (n1 < 0)
5262 {
5263 n1 = len + n1;
5264 if (n1 < 0)
5265 n1 = 0;
5266 }
5267 if (n2 < 0)
5268 n2 = len + n2;
5269 else if (n2 >= len)
5270 n2 = len;
5271 if (n1 >= len || n2 < 0 || n1 > n2)
5272 s = NULL;
5273 else
5274 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5275 }
5276 else
5277 {
5278 /* The resulting variable is a string of a single
5279 * character. If the index is too big or negative the
5280 * result is empty. */
5281 if (n1 >= len || n1 < 0)
5282 s = NULL;
5283 else
5284 s = vim_strnsave(s + n1, 1);
5285 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005286 clear_tv(rettv);
5287 rettv->v_type = VAR_STRING;
5288 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289 break;
5290
5291 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005292 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 if (n1 < 0)
5294 n1 = len + n1;
5295 if (!empty1 && (n1 < 0 || n1 >= len))
5296 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005297 /* For a range we allow invalid values and return an empty
5298 * list. A list index out of range is an error. */
5299 if (!range)
5300 {
5301 if (verbose)
5302 EMSGN(_(e_listidx), n1);
5303 return FAIL;
5304 }
5305 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306 }
5307 if (range)
5308 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005309 list_T *l;
5310 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311
5312 if (n2 < 0)
5313 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005314 else if (n2 >= len)
5315 n2 = len - 1;
5316 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005317 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005318 l = list_alloc();
5319 if (l == NULL)
5320 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005321 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322 n1 <= n2; ++n1)
5323 {
5324 if (list_append_tv(l, &item->li_tv) == FAIL)
5325 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005326 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 return FAIL;
5328 }
5329 item = item->li_next;
5330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005331 clear_tv(rettv);
5332 rettv->v_type = VAR_LIST;
5333 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005334 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 }
5336 else
5337 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005338 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005339 clear_tv(rettv);
5340 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 }
5342 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343
5344 case VAR_DICT:
5345 if (range)
5346 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005347 if (verbose)
5348 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005349 if (len == -1)
5350 clear_tv(&var1);
5351 return FAIL;
5352 }
5353 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005354 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355
5356 if (len == -1)
5357 {
5358 key = get_tv_string(&var1);
5359 if (*key == NUL)
5360 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005361 if (verbose)
5362 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 clear_tv(&var1);
5364 return FAIL;
5365 }
5366 }
5367
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005368 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005369
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005370 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005371 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 if (len == -1)
5373 clear_tv(&var1);
5374 if (item == NULL)
5375 return FAIL;
5376
5377 copy_tv(&item->di_tv, &var1);
5378 clear_tv(rettv);
5379 *rettv = var1;
5380 }
5381 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 }
5383 }
5384
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 return OK;
5386}
5387
5388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 * Get an option value.
5390 * "arg" points to the '&' or '+' before the option name.
5391 * "arg" is advanced to character after the option name.
5392 * Return OK or FAIL.
5393 */
5394 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005395get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005397 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 int evaluate;
5399{
5400 char_u *option_end;
5401 long numval;
5402 char_u *stringval;
5403 int opt_type;
5404 int c;
5405 int working = (**arg == '+'); /* has("+option") */
5406 int ret = OK;
5407 int opt_flags;
5408
5409 /*
5410 * Isolate the option name and find its value.
5411 */
5412 option_end = find_option_end(arg, &opt_flags);
5413 if (option_end == NULL)
5414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 EMSG2(_("E112: Option name missing: %s"), *arg);
5417 return FAIL;
5418 }
5419
5420 if (!evaluate)
5421 {
5422 *arg = option_end;
5423 return OK;
5424 }
5425
5426 c = *option_end;
5427 *option_end = NUL;
5428 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005429 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430
5431 if (opt_type == -3) /* invalid name */
5432 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005433 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 EMSG2(_("E113: Unknown option: %s"), *arg);
5435 ret = FAIL;
5436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005437 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 {
5439 if (opt_type == -2) /* hidden string option */
5440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005441 rettv->v_type = VAR_STRING;
5442 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 }
5444 else if (opt_type == -1) /* hidden number option */
5445 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005446 rettv->v_type = VAR_NUMBER;
5447 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 }
5449 else if (opt_type == 1) /* number option */
5450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005451 rettv->v_type = VAR_NUMBER;
5452 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 }
5454 else /* string option */
5455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005456 rettv->v_type = VAR_STRING;
5457 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 }
5459 }
5460 else if (working && (opt_type == -2 || opt_type == -1))
5461 ret = FAIL;
5462
5463 *option_end = c; /* put back for error messages */
5464 *arg = option_end;
5465
5466 return ret;
5467}
5468
5469/*
5470 * Allocate a variable for a string constant.
5471 * Return OK or FAIL.
5472 */
5473 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005474get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 int evaluate;
5478{
5479 char_u *p;
5480 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 int extra = 0;
5482
5483 /*
5484 * Find the end of the string, skipping backslashed characters.
5485 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005486 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 {
5488 if (*p == '\\' && p[1] != NUL)
5489 {
5490 ++p;
5491 /* A "\<x>" form occupies at least 4 characters, and produces up
5492 * to 6 characters: reserve space for 2 extra */
5493 if (*p == '<')
5494 extra += 2;
5495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
5497
5498 if (*p != '"')
5499 {
5500 EMSG2(_("E114: Missing quote: %s"), *arg);
5501 return FAIL;
5502 }
5503
5504 /* If only parsing, set *arg and return here */
5505 if (!evaluate)
5506 {
5507 *arg = p + 1;
5508 return OK;
5509 }
5510
5511 /*
5512 * Copy the string into allocated memory, handling backslashed
5513 * characters.
5514 */
5515 name = alloc((unsigned)(p - *arg + extra));
5516 if (name == NULL)
5517 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005518 rettv->v_type = VAR_STRING;
5519 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520
Bram Moolenaar8c711452005-01-14 21:53:12 +00005521 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 {
5523 if (*p == '\\')
5524 {
5525 switch (*++p)
5526 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005527 case 'b': *name++ = BS; ++p; break;
5528 case 'e': *name++ = ESC; ++p; break;
5529 case 'f': *name++ = FF; ++p; break;
5530 case 'n': *name++ = NL; ++p; break;
5531 case 'r': *name++ = CAR; ++p; break;
5532 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533
5534 case 'X': /* hex: "\x1", "\x12" */
5535 case 'x':
5536 case 'u': /* Unicode: "\u0023" */
5537 case 'U':
5538 if (vim_isxdigit(p[1]))
5539 {
5540 int n, nr;
5541 int c = toupper(*p);
5542
5543 if (c == 'X')
5544 n = 2;
5545 else
5546 n = 4;
5547 nr = 0;
5548 while (--n >= 0 && vim_isxdigit(p[1]))
5549 {
5550 ++p;
5551 nr = (nr << 4) + hex2nr(*p);
5552 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005553 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554#ifdef FEAT_MBYTE
5555 /* For "\u" store the number according to
5556 * 'encoding'. */
5557 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005558 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 else
5560#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005561 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 break;
5564
5565 /* octal: "\1", "\12", "\123" */
5566 case '0':
5567 case '1':
5568 case '2':
5569 case '3':
5570 case '4':
5571 case '5':
5572 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573 case '7': *name = *p++ - '0';
5574 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005576 *name = (*name << 3) + *p++ - '0';
5577 if (*p >= '0' && *p <= '7')
5578 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005580 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 break;
5582
5583 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005584 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 if (extra != 0)
5586 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 break;
5589 }
5590 /* FALLTHROUGH */
5591
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 break;
5594 }
5595 }
5596 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005597 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005600 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 *arg = p + 1;
5602
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 return OK;
5604}
5605
5606/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 * Return OK or FAIL.
5609 */
5610 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005611get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 int evaluate;
5615{
5616 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005617 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005618 int reduce = 0;
5619
5620 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005621 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005622 */
5623 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5624 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005625 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005627 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005628 break;
5629 ++reduce;
5630 ++p;
5631 }
5632 }
5633
Bram Moolenaar8c711452005-01-14 21:53:12 +00005634 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005635 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005637 return FAIL;
5638 }
5639
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005641 if (!evaluate)
5642 {
5643 *arg = p + 1;
5644 return OK;
5645 }
5646
5647 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005648 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005649 */
5650 str = alloc((unsigned)((p - *arg) - reduce));
5651 if (str == NULL)
5652 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005653 rettv->v_type = VAR_STRING;
5654 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005655
Bram Moolenaar8c711452005-01-14 21:53:12 +00005656 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005657 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005660 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005661 break;
5662 ++p;
5663 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005664 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005665 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005666 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005667 *arg = p + 1;
5668
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005669 return OK;
5670}
5671
5672/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005673 * Allocate a variable for a List and fill it from "*arg".
5674 * Return OK or FAIL.
5675 */
5676 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005677get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005678 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005679 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005680 int evaluate;
5681{
Bram Moolenaar33570922005-01-25 22:26:29 +00005682 list_T *l = NULL;
5683 typval_T tv;
5684 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005685
5686 if (evaluate)
5687 {
5688 l = list_alloc();
5689 if (l == NULL)
5690 return FAIL;
5691 }
5692
5693 *arg = skipwhite(*arg + 1);
5694 while (**arg != ']' && **arg != NUL)
5695 {
5696 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5697 goto failret;
5698 if (evaluate)
5699 {
5700 item = listitem_alloc();
5701 if (item != NULL)
5702 {
5703 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005704 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005705 list_append(l, item);
5706 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005707 else
5708 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005709 }
5710
5711 if (**arg == ']')
5712 break;
5713 if (**arg != ',')
5714 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005715 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005716 goto failret;
5717 }
5718 *arg = skipwhite(*arg + 1);
5719 }
5720
5721 if (**arg != ']')
5722 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005723 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005724failret:
5725 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005726 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005727 return FAIL;
5728 }
5729
5730 *arg = skipwhite(*arg + 1);
5731 if (evaluate)
5732 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005733 rettv->v_type = VAR_LIST;
5734 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005735 ++l->lv_refcount;
5736 }
5737
5738 return OK;
5739}
5740
5741/*
5742 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005743 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005744 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005745 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005746list_alloc()
5747{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005748 list_T *l;
5749
5750 l = (list_T *)alloc_clear(sizeof(list_T));
5751 if (l != NULL)
5752 {
5753 /* Prepend the list to the list of lists for garbage collection. */
5754 if (first_list != NULL)
5755 first_list->lv_used_prev = l;
5756 l->lv_used_prev = NULL;
5757 l->lv_used_next = first_list;
5758 first_list = l;
5759 }
5760 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761}
5762
5763/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005764 * Allocate an empty list for a return value.
5765 * Returns OK or FAIL.
5766 */
5767 static int
5768rettv_list_alloc(rettv)
5769 typval_T *rettv;
5770{
5771 list_T *l = list_alloc();
5772
5773 if (l == NULL)
5774 return FAIL;
5775
5776 rettv->vval.v_list = l;
5777 rettv->v_type = VAR_LIST;
5778 ++l->lv_refcount;
5779 return OK;
5780}
5781
5782/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005783 * Unreference a list: decrement the reference count and free it when it
5784 * becomes zero.
5785 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005786 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005787list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005788 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005789{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005790 if (l != NULL && --l->lv_refcount <= 0)
5791 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005792}
5793
5794/*
5795 * Free a list, including all items it points to.
5796 * Ignores the reference count.
5797 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005798 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005799list_free(l, recurse)
5800 list_T *l;
5801 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802{
Bram Moolenaar33570922005-01-25 22:26:29 +00005803 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005805 /* Remove the list from the list of lists for garbage collection. */
5806 if (l->lv_used_prev == NULL)
5807 first_list = l->lv_used_next;
5808 else
5809 l->lv_used_prev->lv_used_next = l->lv_used_next;
5810 if (l->lv_used_next != NULL)
5811 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5812
Bram Moolenaard9fba312005-06-26 22:34:35 +00005813 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005815 /* Remove the item before deleting it. */
5816 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005817 if (recurse || (item->li_tv.v_type != VAR_LIST
5818 && item->li_tv.v_type != VAR_DICT))
5819 clear_tv(&item->li_tv);
5820 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821 }
5822 vim_free(l);
5823}
5824
5825/*
5826 * Allocate a list item.
5827 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005828 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005829listitem_alloc()
5830{
Bram Moolenaar33570922005-01-25 22:26:29 +00005831 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832}
5833
5834/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005835 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836 */
5837 static void
5838listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005839 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005840{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005841 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005842 vim_free(item);
5843}
5844
5845/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005846 * Remove a list item from a List and free it. Also clears the value.
5847 */
5848 static void
5849listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005850 list_T *l;
5851 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005852{
5853 list_remove(l, item, item);
5854 listitem_free(item);
5855}
5856
5857/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858 * Get the number of items in a list.
5859 */
5860 static long
5861list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005862 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 if (l == NULL)
5865 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005866 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005867}
5868
5869/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005870 * Return TRUE when two lists have exactly the same values.
5871 */
5872 static int
5873list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005874 list_T *l1;
5875 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005876 int ic; /* ignore case for strings */
5877{
Bram Moolenaar33570922005-01-25 22:26:29 +00005878 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005879
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005880 if (l1 == NULL || l2 == NULL)
5881 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005882 if (l1 == l2)
5883 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005884 if (list_len(l1) != list_len(l2))
5885 return FALSE;
5886
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005887 for (item1 = l1->lv_first, item2 = l2->lv_first;
5888 item1 != NULL && item2 != NULL;
5889 item1 = item1->li_next, item2 = item2->li_next)
5890 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5891 return FALSE;
5892 return item1 == NULL && item2 == NULL;
5893}
5894
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01005895#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_MZSCHEME) \
5896 || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005897/*
5898 * Return the dictitem that an entry in a hashtable points to.
5899 */
5900 dictitem_T *
5901dict_lookup(hi)
5902 hashitem_T *hi;
5903{
5904 return HI2DI(hi);
5905}
5906#endif
5907
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005908/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005909 * Return TRUE when two dictionaries have exactly the same key/values.
5910 */
5911 static int
5912dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005913 dict_T *d1;
5914 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005915 int ic; /* ignore case for strings */
5916{
Bram Moolenaar33570922005-01-25 22:26:29 +00005917 hashitem_T *hi;
5918 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005919 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005920
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005921 if (d1 == NULL || d2 == NULL)
5922 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005923 if (d1 == d2)
5924 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005925 if (dict_len(d1) != dict_len(d2))
5926 return FALSE;
5927
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005928 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005929 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005930 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005931 if (!HASHITEM_EMPTY(hi))
5932 {
5933 item2 = dict_find(d2, hi->hi_key, -1);
5934 if (item2 == NULL)
5935 return FALSE;
5936 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5937 return FALSE;
5938 --todo;
5939 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005940 }
5941 return TRUE;
5942}
5943
5944/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005945 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005946 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005947 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005948 */
5949 static int
5950tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005951 typval_T *tv1;
5952 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005953 int ic; /* ignore case */
5954{
5955 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005956 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005957 static int recursive = 0; /* cach recursive loops */
5958 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005959
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005960 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005961 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005962 /* Catch lists and dicts that have an endless loop by limiting
5963 * recursiveness to 1000. We guess they are equal then. */
5964 if (recursive >= 1000)
5965 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005966
5967 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005968 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005969 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005970 ++recursive;
5971 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5972 --recursive;
5973 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005974
5975 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005976 ++recursive;
5977 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5978 --recursive;
5979 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005980
5981 case VAR_FUNC:
5982 return (tv1->vval.v_string != NULL
5983 && tv2->vval.v_string != NULL
5984 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5985
5986 case VAR_NUMBER:
5987 return tv1->vval.v_number == tv2->vval.v_number;
5988
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005989#ifdef FEAT_FLOAT
5990 case VAR_FLOAT:
5991 return tv1->vval.v_float == tv2->vval.v_float;
5992#endif
5993
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005994 case VAR_STRING:
5995 s1 = get_tv_string_buf(tv1, buf1);
5996 s2 = get_tv_string_buf(tv2, buf2);
5997 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005998 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005999
6000 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006001 return TRUE;
6002}
6003
6004/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005 * Locate item with index "n" in list "l" and return it.
6006 * A negative index is counted from the end; -1 is the last item.
6007 * Returns NULL when "n" is out of range.
6008 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006009 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006011 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 long n;
6013{
Bram Moolenaar33570922005-01-25 22:26:29 +00006014 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015 long idx;
6016
6017 if (l == NULL)
6018 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006019
6020 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006022 n = l->lv_len + n;
6023
6024 /* Check for index out of range. */
6025 if (n < 0 || n >= l->lv_len)
6026 return NULL;
6027
6028 /* When there is a cached index may start search from there. */
6029 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006031 if (n < l->lv_idx / 2)
6032 {
6033 /* closest to the start of the list */
6034 item = l->lv_first;
6035 idx = 0;
6036 }
6037 else if (n > (l->lv_idx + l->lv_len) / 2)
6038 {
6039 /* closest to the end of the list */
6040 item = l->lv_last;
6041 idx = l->lv_len - 1;
6042 }
6043 else
6044 {
6045 /* closest to the cached index */
6046 item = l->lv_idx_item;
6047 idx = l->lv_idx;
6048 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 }
6050 else
6051 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006052 if (n < l->lv_len / 2)
6053 {
6054 /* closest to the start of the list */
6055 item = l->lv_first;
6056 idx = 0;
6057 }
6058 else
6059 {
6060 /* closest to the end of the list */
6061 item = l->lv_last;
6062 idx = l->lv_len - 1;
6063 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006065
6066 while (n > idx)
6067 {
6068 /* search forward */
6069 item = item->li_next;
6070 ++idx;
6071 }
6072 while (n < idx)
6073 {
6074 /* search backward */
6075 item = item->li_prev;
6076 --idx;
6077 }
6078
6079 /* cache the used index */
6080 l->lv_idx = idx;
6081 l->lv_idx_item = item;
6082
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083 return item;
6084}
6085
6086/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006087 * Get list item "l[idx]" as a number.
6088 */
6089 static long
6090list_find_nr(l, idx, errorp)
6091 list_T *l;
6092 long idx;
6093 int *errorp; /* set to TRUE when something wrong */
6094{
6095 listitem_T *li;
6096
6097 li = list_find(l, idx);
6098 if (li == NULL)
6099 {
6100 if (errorp != NULL)
6101 *errorp = TRUE;
6102 return -1L;
6103 }
6104 return get_tv_number_chk(&li->li_tv, errorp);
6105}
6106
6107/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006108 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6109 */
6110 char_u *
6111list_find_str(l, idx)
6112 list_T *l;
6113 long idx;
6114{
6115 listitem_T *li;
6116
6117 li = list_find(l, idx - 1);
6118 if (li == NULL)
6119 {
6120 EMSGN(_(e_listidx), idx);
6121 return NULL;
6122 }
6123 return get_tv_string(&li->li_tv);
6124}
6125
6126/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006127 * Locate "item" list "l" and return its index.
6128 * Returns -1 when "item" is not in the list.
6129 */
6130 static long
6131list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006132 list_T *l;
6133 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006134{
6135 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006136 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006137
6138 if (l == NULL)
6139 return -1;
6140 idx = 0;
6141 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6142 ++idx;
6143 if (li == NULL)
6144 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006145 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006146}
6147
6148/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149 * Append item "item" to the end of list "l".
6150 */
6151 static void
6152list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006153 list_T *l;
6154 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006155{
6156 if (l->lv_last == NULL)
6157 {
6158 /* empty list */
6159 l->lv_first = item;
6160 l->lv_last = item;
6161 item->li_prev = NULL;
6162 }
6163 else
6164 {
6165 l->lv_last->li_next = item;
6166 item->li_prev = l->lv_last;
6167 l->lv_last = item;
6168 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006169 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006170 item->li_next = NULL;
6171}
6172
6173/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006174 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006175 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006176 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006177 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006179 list_T *l;
6180 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006182 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006183
Bram Moolenaar05159a02005-02-26 23:04:13 +00006184 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006186 copy_tv(tv, &li->li_tv);
6187 list_append(l, li);
6188 return OK;
6189}
6190
6191/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006192 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006193 * Return FAIL when out of memory.
6194 */
6195 int
6196list_append_dict(list, dict)
6197 list_T *list;
6198 dict_T *dict;
6199{
6200 listitem_T *li = listitem_alloc();
6201
6202 if (li == NULL)
6203 return FAIL;
6204 li->li_tv.v_type = VAR_DICT;
6205 li->li_tv.v_lock = 0;
6206 li->li_tv.vval.v_dict = dict;
6207 list_append(list, li);
6208 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006209 return OK;
6210}
6211
6212/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006213 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006214 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006215 * Returns FAIL when out of memory.
6216 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006217 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006218list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006219 list_T *l;
6220 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006221 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006222{
6223 listitem_T *li = listitem_alloc();
6224
6225 if (li == NULL)
6226 return FAIL;
6227 list_append(l, li);
6228 li->li_tv.v_type = VAR_STRING;
6229 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006230 if (str == NULL)
6231 li->li_tv.vval.v_string = NULL;
6232 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006233 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006234 return FAIL;
6235 return OK;
6236}
6237
6238/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006239 * Append "n" to list "l".
6240 * Returns FAIL when out of memory.
6241 */
6242 static int
6243list_append_number(l, n)
6244 list_T *l;
6245 varnumber_T n;
6246{
6247 listitem_T *li;
6248
6249 li = listitem_alloc();
6250 if (li == NULL)
6251 return FAIL;
6252 li->li_tv.v_type = VAR_NUMBER;
6253 li->li_tv.v_lock = 0;
6254 li->li_tv.vval.v_number = n;
6255 list_append(l, li);
6256 return OK;
6257}
6258
6259/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006260 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006261 * If "item" is NULL append at the end.
6262 * Return FAIL when out of memory.
6263 */
6264 static int
6265list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006266 list_T *l;
6267 typval_T *tv;
6268 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006269{
Bram Moolenaar33570922005-01-25 22:26:29 +00006270 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006271
6272 if (ni == NULL)
6273 return FAIL;
6274 copy_tv(tv, &ni->li_tv);
6275 if (item == NULL)
6276 /* Append new item at end of list. */
6277 list_append(l, ni);
6278 else
6279 {
6280 /* Insert new item before existing item. */
6281 ni->li_prev = item->li_prev;
6282 ni->li_next = item;
6283 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006284 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006285 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006286 ++l->lv_idx;
6287 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006288 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006289 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006290 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006291 l->lv_idx_item = NULL;
6292 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006293 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006294 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006295 }
6296 return OK;
6297}
6298
6299/*
6300 * Extend "l1" with "l2".
6301 * If "bef" is NULL append at the end, otherwise insert before this item.
6302 * Returns FAIL when out of memory.
6303 */
6304 static int
6305list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 list_T *l1;
6307 list_T *l2;
6308 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006309{
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006311 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006312
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006313 /* We also quit the loop when we have inserted the original item count of
6314 * the list, avoid a hang when we extend a list with itself. */
6315 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006316 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6317 return FAIL;
6318 return OK;
6319}
6320
6321/*
6322 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6323 * Return FAIL when out of memory.
6324 */
6325 static int
6326list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 list_T *l1;
6328 list_T *l2;
6329 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006330{
Bram Moolenaar33570922005-01-25 22:26:29 +00006331 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006332
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006333 if (l1 == NULL || l2 == NULL)
6334 return FAIL;
6335
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006336 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006337 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006338 if (l == NULL)
6339 return FAIL;
6340 tv->v_type = VAR_LIST;
6341 tv->vval.v_list = l;
6342
6343 /* append all items from the second list */
6344 return list_extend(l, l2, NULL);
6345}
6346
6347/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006348 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006349 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006350 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351 * Returns NULL when out of memory.
6352 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006353 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006354list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006355 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006356 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006357 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006358{
Bram Moolenaar33570922005-01-25 22:26:29 +00006359 list_T *copy;
6360 listitem_T *item;
6361 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006362
6363 if (orig == NULL)
6364 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006365
6366 copy = list_alloc();
6367 if (copy != NULL)
6368 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006369 if (copyID != 0)
6370 {
6371 /* Do this before adding the items, because one of the items may
6372 * refer back to this list. */
6373 orig->lv_copyID = copyID;
6374 orig->lv_copylist = copy;
6375 }
6376 for (item = orig->lv_first; item != NULL && !got_int;
6377 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006378 {
6379 ni = listitem_alloc();
6380 if (ni == NULL)
6381 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006382 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006383 {
6384 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6385 {
6386 vim_free(ni);
6387 break;
6388 }
6389 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006391 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006392 list_append(copy, ni);
6393 }
6394 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006395 if (item != NULL)
6396 {
6397 list_unref(copy);
6398 copy = NULL;
6399 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400 }
6401
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006402 return copy;
6403}
6404
6405/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006406 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006407 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006408 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006409 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006410list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 list_T *l;
6412 listitem_T *item;
6413 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006414{
Bram Moolenaar33570922005-01-25 22:26:29 +00006415 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417 /* notify watchers */
6418 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006420 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006421 list_fix_watch(l, ip);
6422 if (ip == item2)
6423 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425
6426 if (item2->li_next == NULL)
6427 l->lv_last = item->li_prev;
6428 else
6429 item2->li_next->li_prev = item->li_prev;
6430 if (item->li_prev == NULL)
6431 l->lv_first = item2->li_next;
6432 else
6433 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006434 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435}
6436
6437/*
6438 * Return an allocated string with the string representation of a list.
6439 * May return NULL.
6440 */
6441 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006442list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006443 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006444 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006445{
6446 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006447
6448 if (tv->vval.v_list == NULL)
6449 return NULL;
6450 ga_init2(&ga, (int)sizeof(char), 80);
6451 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006452 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006453 {
6454 vim_free(ga.ga_data);
6455 return NULL;
6456 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457 ga_append(&ga, ']');
6458 ga_append(&ga, NUL);
6459 return (char_u *)ga.ga_data;
6460}
6461
6462/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006463 * Join list "l" into a string in "*gap", using separator "sep".
6464 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006465 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006466 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006467 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006468list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006469 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006470 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006471 char_u *sep;
6472 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006473 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006474{
6475 int first = TRUE;
6476 char_u *tofree;
6477 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006478 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006479 char_u *s;
6480
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006481 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006482 {
6483 if (first)
6484 first = FALSE;
6485 else
6486 ga_concat(gap, sep);
6487
6488 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006489 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006490 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006491 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006492 if (s != NULL)
6493 ga_concat(gap, s);
6494 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006495 if (s == NULL)
6496 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006497 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006498 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006499 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006500}
6501
6502/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006503 * Garbage collection for lists and dictionaries.
6504 *
6505 * We use reference counts to be able to free most items right away when they
6506 * are no longer used. But for composite items it's possible that it becomes
6507 * unused while the reference count is > 0: When there is a recursive
6508 * reference. Example:
6509 * :let l = [1, 2, 3]
6510 * :let d = {9: l}
6511 * :let l[1] = d
6512 *
6513 * Since this is quite unusual we handle this with garbage collection: every
6514 * once in a while find out which lists and dicts are not referenced from any
6515 * variable.
6516 *
6517 * Here is a good reference text about garbage collection (refers to Python
6518 * but it applies to all reference-counting mechanisms):
6519 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006520 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006521
6522/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006523 * Do garbage collection for lists and dicts.
6524 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006525 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006526 int
6527garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006528{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006529 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006530 buf_T *buf;
6531 win_T *wp;
6532 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006533 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006534 int did_free;
6535 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006536#ifdef FEAT_WINDOWS
6537 tabpage_T *tp;
6538#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006539
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006540 /* Only do this once. */
6541 want_garbage_collect = FALSE;
6542 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006543 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006544
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006545 /* We advance by two because we add one for items referenced through
6546 * previous_funccal. */
6547 current_copyID += COPYID_INC;
6548 copyID = current_copyID;
6549
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006550 /*
6551 * 1. Go through all accessible variables and mark all lists and dicts
6552 * with copyID.
6553 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006554
6555 /* Don't free variables in the previous_funccal list unless they are only
6556 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006557 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006558 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6559 {
6560 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6561 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6562 }
6563
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006564 /* script-local variables */
6565 for (i = 1; i <= ga_scripts.ga_len; ++i)
6566 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6567
6568 /* buffer-local variables */
6569 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6570 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6571
6572 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006573 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006574 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6575
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006576#ifdef FEAT_WINDOWS
6577 /* tabpage-local variables */
6578 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6579 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6580#endif
6581
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006582 /* global variables */
6583 set_ref_in_ht(&globvarht, copyID);
6584
6585 /* function-local variables */
6586 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6587 {
6588 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6589 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6590 }
6591
Bram Moolenaard812df62008-11-09 12:46:09 +00006592 /* v: vars */
6593 set_ref_in_ht(&vimvarht, copyID);
6594
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006595 /*
6596 * 2. Free lists and dictionaries that are not referenced.
6597 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006598 did_free = free_unref_items(copyID);
6599
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006600 /*
6601 * 3. Check if any funccal can be freed now.
6602 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006603 for (pfc = &previous_funccal; *pfc != NULL; )
6604 {
6605 if (can_free_funccal(*pfc, copyID))
6606 {
6607 fc = *pfc;
6608 *pfc = fc->caller;
6609 free_funccal(fc, TRUE);
6610 did_free = TRUE;
6611 did_free_funccal = TRUE;
6612 }
6613 else
6614 pfc = &(*pfc)->caller;
6615 }
6616 if (did_free_funccal)
6617 /* When a funccal was freed some more items might be garbage
6618 * collected, so run again. */
6619 (void)garbage_collect();
6620
6621 return did_free;
6622}
6623
6624/*
6625 * Free lists and dictionaries that are no longer referenced.
6626 */
6627 static int
6628free_unref_items(copyID)
6629 int copyID;
6630{
6631 dict_T *dd;
6632 list_T *ll;
6633 int did_free = FALSE;
6634
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006635 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006636 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006637 */
6638 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006639 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006640 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006641 /* Free the Dictionary and ordinary items it contains, but don't
6642 * recurse into Lists and Dictionaries, they will be in the list
6643 * of dicts or list of lists. */
6644 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006645 did_free = TRUE;
6646
6647 /* restart, next dict may also have been freed */
6648 dd = first_dict;
6649 }
6650 else
6651 dd = dd->dv_used_next;
6652
6653 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006654 * Go through the list of lists and free items without the copyID.
6655 * But don't free a list that has a watcher (used in a for loop), these
6656 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006657 */
6658 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006659 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6660 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006661 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006662 /* Free the List and ordinary items it contains, but don't recurse
6663 * into Lists and Dictionaries, they will be in the list of dicts
6664 * or list of lists. */
6665 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006666 did_free = TRUE;
6667
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006668 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006669 ll = first_list;
6670 }
6671 else
6672 ll = ll->lv_used_next;
6673
6674 return did_free;
6675}
6676
6677/*
6678 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6679 */
6680 static void
6681set_ref_in_ht(ht, copyID)
6682 hashtab_T *ht;
6683 int copyID;
6684{
6685 int todo;
6686 hashitem_T *hi;
6687
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006688 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006689 for (hi = ht->ht_array; todo > 0; ++hi)
6690 if (!HASHITEM_EMPTY(hi))
6691 {
6692 --todo;
6693 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6694 }
6695}
6696
6697/*
6698 * Mark all lists and dicts referenced through list "l" with "copyID".
6699 */
6700 static void
6701set_ref_in_list(l, copyID)
6702 list_T *l;
6703 int copyID;
6704{
6705 listitem_T *li;
6706
6707 for (li = l->lv_first; li != NULL; li = li->li_next)
6708 set_ref_in_item(&li->li_tv, copyID);
6709}
6710
6711/*
6712 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6713 */
6714 static void
6715set_ref_in_item(tv, copyID)
6716 typval_T *tv;
6717 int copyID;
6718{
6719 dict_T *dd;
6720 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006721
6722 switch (tv->v_type)
6723 {
6724 case VAR_DICT:
6725 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006726 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006727 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006728 /* Didn't see this dict yet. */
6729 dd->dv_copyID = copyID;
6730 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006731 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006732 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006733
6734 case VAR_LIST:
6735 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006736 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006737 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006738 /* Didn't see this list yet. */
6739 ll->lv_copyID = copyID;
6740 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006741 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006742 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006743 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006744 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006745}
6746
6747/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006748 * Allocate an empty header for a dictionary.
6749 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006750 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006751dict_alloc()
6752{
Bram Moolenaar33570922005-01-25 22:26:29 +00006753 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006754
Bram Moolenaar33570922005-01-25 22:26:29 +00006755 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006756 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006757 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006758 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006759 if (first_dict != NULL)
6760 first_dict->dv_used_prev = d;
6761 d->dv_used_next = first_dict;
6762 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006763 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006764
Bram Moolenaar33570922005-01-25 22:26:29 +00006765 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006766 d->dv_lock = 0;
6767 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006768 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006769 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006770 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006771}
6772
6773/*
6774 * Unreference a Dictionary: decrement the reference count and free it when it
6775 * becomes zero.
6776 */
6777 static void
6778dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006779 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006780{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006781 if (d != NULL && --d->dv_refcount <= 0)
6782 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006783}
6784
6785/*
6786 * Free a Dictionary, including all items it contains.
6787 * Ignores the reference count.
6788 */
6789 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006790dict_free(d, recurse)
6791 dict_T *d;
6792 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006793{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006794 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006795 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006796 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006797
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006798 /* Remove the dict from the list of dicts for garbage collection. */
6799 if (d->dv_used_prev == NULL)
6800 first_dict = d->dv_used_next;
6801 else
6802 d->dv_used_prev->dv_used_next = d->dv_used_next;
6803 if (d->dv_used_next != NULL)
6804 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6805
6806 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006807 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006808 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006809 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006810 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006811 if (!HASHITEM_EMPTY(hi))
6812 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006813 /* Remove the item before deleting it, just in case there is
6814 * something recursive causing trouble. */
6815 di = HI2DI(hi);
6816 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006817 if (recurse || (di->di_tv.v_type != VAR_LIST
6818 && di->di_tv.v_type != VAR_DICT))
6819 clear_tv(&di->di_tv);
6820 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006821 --todo;
6822 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006823 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006824 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006825 vim_free(d);
6826}
6827
6828/*
6829 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006830 * The "key" is copied to the new item.
6831 * Note that the value of the item "di_tv" still needs to be initialized!
6832 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006833 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006834 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006835dictitem_alloc(key)
6836 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006837{
Bram Moolenaar33570922005-01-25 22:26:29 +00006838 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006839
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006840 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006841 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006842 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006843 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006844 di->di_flags = 0;
6845 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006846 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006847}
6848
6849/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006850 * Make a copy of a Dictionary item.
6851 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006852 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006853dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006854 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006855{
Bram Moolenaar33570922005-01-25 22:26:29 +00006856 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006857
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006858 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6859 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006860 if (di != NULL)
6861 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006862 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006863 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006864 copy_tv(&org->di_tv, &di->di_tv);
6865 }
6866 return di;
6867}
6868
6869/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006870 * Remove item "item" from Dictionary "dict" and free it.
6871 */
6872 static void
6873dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006874 dict_T *dict;
6875 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006876{
Bram Moolenaar33570922005-01-25 22:26:29 +00006877 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006878
Bram Moolenaar33570922005-01-25 22:26:29 +00006879 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006880 if (HASHITEM_EMPTY(hi))
6881 EMSG2(_(e_intern2), "dictitem_remove()");
6882 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006883 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006884 dictitem_free(item);
6885}
6886
6887/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006888 * Free a dict item. Also clears the value.
6889 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006890 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006891dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006892 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006893{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006894 clear_tv(&item->di_tv);
6895 vim_free(item);
6896}
6897
6898/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006899 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6900 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006901 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006902 * Returns NULL when out of memory.
6903 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006904 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006905dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006906 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006907 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006908 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006909{
Bram Moolenaar33570922005-01-25 22:26:29 +00006910 dict_T *copy;
6911 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006912 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006913 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006914
6915 if (orig == NULL)
6916 return NULL;
6917
6918 copy = dict_alloc();
6919 if (copy != NULL)
6920 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006921 if (copyID != 0)
6922 {
6923 orig->dv_copyID = copyID;
6924 orig->dv_copydict = copy;
6925 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006926 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006927 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006928 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006929 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006930 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006931 --todo;
6932
6933 di = dictitem_alloc(hi->hi_key);
6934 if (di == NULL)
6935 break;
6936 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006937 {
6938 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6939 copyID) == FAIL)
6940 {
6941 vim_free(di);
6942 break;
6943 }
6944 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006945 else
6946 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6947 if (dict_add(copy, di) == FAIL)
6948 {
6949 dictitem_free(di);
6950 break;
6951 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006952 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006953 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006954
Bram Moolenaare9a41262005-01-15 22:18:47 +00006955 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006956 if (todo > 0)
6957 {
6958 dict_unref(copy);
6959 copy = NULL;
6960 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006961 }
6962
6963 return copy;
6964}
6965
6966/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006967 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006968 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006969 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006970 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006971dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006972 dict_T *d;
6973 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006974{
Bram Moolenaar33570922005-01-25 22:26:29 +00006975 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006976}
6977
Bram Moolenaar8c711452005-01-14 21:53:12 +00006978/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006979 * Add a number or string entry to dictionary "d".
6980 * When "str" is NULL use number "nr", otherwise use "str".
6981 * Returns FAIL when out of memory and when key already exists.
6982 */
6983 int
6984dict_add_nr_str(d, key, nr, str)
6985 dict_T *d;
6986 char *key;
6987 long nr;
6988 char_u *str;
6989{
6990 dictitem_T *item;
6991
6992 item = dictitem_alloc((char_u *)key);
6993 if (item == NULL)
6994 return FAIL;
6995 item->di_tv.v_lock = 0;
6996 if (str == NULL)
6997 {
6998 item->di_tv.v_type = VAR_NUMBER;
6999 item->di_tv.vval.v_number = nr;
7000 }
7001 else
7002 {
7003 item->di_tv.v_type = VAR_STRING;
7004 item->di_tv.vval.v_string = vim_strsave(str);
7005 }
7006 if (dict_add(d, item) == FAIL)
7007 {
7008 dictitem_free(item);
7009 return FAIL;
7010 }
7011 return OK;
7012}
7013
7014/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007015 * Get the number of items in a Dictionary.
7016 */
7017 static long
7018dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007019 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007020{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007021 if (d == NULL)
7022 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007023 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007024}
7025
7026/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007027 * Find item "key[len]" in Dictionary "d".
7028 * If "len" is negative use strlen(key).
7029 * Returns NULL when not found.
7030 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007031 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007032dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007033 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007034 char_u *key;
7035 int len;
7036{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007037#define AKEYLEN 200
7038 char_u buf[AKEYLEN];
7039 char_u *akey;
7040 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007041 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007042
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007043 if (len < 0)
7044 akey = key;
7045 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007046 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007047 tofree = akey = vim_strnsave(key, len);
7048 if (akey == NULL)
7049 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007050 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007051 else
7052 {
7053 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007054 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007055 akey = buf;
7056 }
7057
Bram Moolenaar33570922005-01-25 22:26:29 +00007058 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007059 vim_free(tofree);
7060 if (HASHITEM_EMPTY(hi))
7061 return NULL;
7062 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007063}
7064
7065/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007066 * Get a string item from a dictionary.
7067 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007068 * Returns NULL if the entry doesn't exist or out of memory.
7069 */
7070 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007071get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007072 dict_T *d;
7073 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007074 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007075{
7076 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007077 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007078
7079 di = dict_find(d, key, -1);
7080 if (di == NULL)
7081 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007082 s = get_tv_string(&di->di_tv);
7083 if (save && s != NULL)
7084 s = vim_strsave(s);
7085 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007086}
7087
7088/*
7089 * Get a number item from a dictionary.
7090 * Returns 0 if the entry doesn't exist or out of memory.
7091 */
7092 long
7093get_dict_number(d, key)
7094 dict_T *d;
7095 char_u *key;
7096{
7097 dictitem_T *di;
7098
7099 di = dict_find(d, key, -1);
7100 if (di == NULL)
7101 return 0;
7102 return get_tv_number(&di->di_tv);
7103}
7104
7105/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007106 * Return an allocated string with the string representation of a Dictionary.
7107 * May return NULL.
7108 */
7109 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007110dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007111 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007112 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007113{
7114 garray_T ga;
7115 int first = TRUE;
7116 char_u *tofree;
7117 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007118 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007119 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007120 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007121 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007122
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007124 return NULL;
7125 ga_init2(&ga, (int)sizeof(char), 80);
7126 ga_append(&ga, '{');
7127
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007128 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007129 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007130 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007132 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007133 --todo;
7134
7135 if (first)
7136 first = FALSE;
7137 else
7138 ga_concat(&ga, (char_u *)", ");
7139
7140 tofree = string_quote(hi->hi_key, FALSE);
7141 if (tofree != NULL)
7142 {
7143 ga_concat(&ga, tofree);
7144 vim_free(tofree);
7145 }
7146 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007147 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007148 if (s != NULL)
7149 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007150 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007151 if (s == NULL)
7152 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007153 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007154 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007155 if (todo > 0)
7156 {
7157 vim_free(ga.ga_data);
7158 return NULL;
7159 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007160
7161 ga_append(&ga, '}');
7162 ga_append(&ga, NUL);
7163 return (char_u *)ga.ga_data;
7164}
7165
7166/*
7167 * Allocate a variable for a Dictionary and fill it from "*arg".
7168 * Return OK or FAIL. Returns NOTDONE for {expr}.
7169 */
7170 static int
7171get_dict_tv(arg, rettv, evaluate)
7172 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007173 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174 int evaluate;
7175{
Bram Moolenaar33570922005-01-25 22:26:29 +00007176 dict_T *d = NULL;
7177 typval_T tvkey;
7178 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007179 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007180 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007181 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007182 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007183
7184 /*
7185 * First check if it's not a curly-braces thing: {expr}.
7186 * Must do this without evaluating, otherwise a function may be called
7187 * twice. Unfortunately this means we need to call eval1() twice for the
7188 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007189 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007190 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007191 if (*start != '}')
7192 {
7193 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7194 return FAIL;
7195 if (*start == '}')
7196 return NOTDONE;
7197 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198
7199 if (evaluate)
7200 {
7201 d = dict_alloc();
7202 if (d == NULL)
7203 return FAIL;
7204 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007205 tvkey.v_type = VAR_UNKNOWN;
7206 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007207
7208 *arg = skipwhite(*arg + 1);
7209 while (**arg != '}' && **arg != NUL)
7210 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007211 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212 goto failret;
7213 if (**arg != ':')
7214 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007215 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007216 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217 goto failret;
7218 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007219 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007221 key = get_tv_string_buf_chk(&tvkey, buf);
7222 if (key == NULL || *key == NUL)
7223 {
7224 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7225 if (key != NULL)
7226 EMSG(_(e_emptykey));
7227 clear_tv(&tvkey);
7228 goto failret;
7229 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231
7232 *arg = skipwhite(*arg + 1);
7233 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7234 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007235 if (evaluate)
7236 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007237 goto failret;
7238 }
7239 if (evaluate)
7240 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007241 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242 if (item != NULL)
7243 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007244 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007245 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246 clear_tv(&tv);
7247 goto failret;
7248 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007249 item = dictitem_alloc(key);
7250 clear_tv(&tvkey);
7251 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007254 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007255 if (dict_add(d, item) == FAIL)
7256 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257 }
7258 }
7259
7260 if (**arg == '}')
7261 break;
7262 if (**arg != ',')
7263 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007264 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265 goto failret;
7266 }
7267 *arg = skipwhite(*arg + 1);
7268 }
7269
7270 if (**arg != '}')
7271 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007272 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273failret:
7274 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007275 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276 return FAIL;
7277 }
7278
7279 *arg = skipwhite(*arg + 1);
7280 if (evaluate)
7281 {
7282 rettv->v_type = VAR_DICT;
7283 rettv->vval.v_dict = d;
7284 ++d->dv_refcount;
7285 }
7286
7287 return OK;
7288}
7289
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007291 * Return a string with the string representation of a variable.
7292 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007293 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007294 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007295 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007296 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007297 */
7298 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007299echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007300 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007301 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007302 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007303 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007304{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305 static int recurse = 0;
7306 char_u *r = NULL;
7307
Bram Moolenaar33570922005-01-25 22:26:29 +00007308 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007309 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007310 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007311 *tofree = NULL;
7312 return NULL;
7313 }
7314 ++recurse;
7315
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007316 switch (tv->v_type)
7317 {
7318 case VAR_FUNC:
7319 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 r = tv->vval.v_string;
7321 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007322
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007323 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007324 if (tv->vval.v_list == NULL)
7325 {
7326 *tofree = NULL;
7327 r = NULL;
7328 }
7329 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7330 {
7331 *tofree = NULL;
7332 r = (char_u *)"[...]";
7333 }
7334 else
7335 {
7336 tv->vval.v_list->lv_copyID = copyID;
7337 *tofree = list2string(tv, copyID);
7338 r = *tofree;
7339 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007340 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007341
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007343 if (tv->vval.v_dict == NULL)
7344 {
7345 *tofree = NULL;
7346 r = NULL;
7347 }
7348 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7349 {
7350 *tofree = NULL;
7351 r = (char_u *)"{...}";
7352 }
7353 else
7354 {
7355 tv->vval.v_dict->dv_copyID = copyID;
7356 *tofree = dict2string(tv, copyID);
7357 r = *tofree;
7358 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007359 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007360
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007361 case VAR_STRING:
7362 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007363 *tofree = NULL;
7364 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007365 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007366
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007367#ifdef FEAT_FLOAT
7368 case VAR_FLOAT:
7369 *tofree = NULL;
7370 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7371 r = numbuf;
7372 break;
7373#endif
7374
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007375 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007376 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007377 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007378 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379
7380 --recurse;
7381 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007382}
7383
7384/*
7385 * Return a string with the string representation of a variable.
7386 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7387 * "numbuf" is used for a number.
7388 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007389 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007390 */
7391 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007392tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007393 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007394 char_u **tofree;
7395 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007396 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007397{
7398 switch (tv->v_type)
7399 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007400 case VAR_FUNC:
7401 *tofree = string_quote(tv->vval.v_string, TRUE);
7402 return *tofree;
7403 case VAR_STRING:
7404 *tofree = string_quote(tv->vval.v_string, FALSE);
7405 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007406#ifdef FEAT_FLOAT
7407 case VAR_FLOAT:
7408 *tofree = NULL;
7409 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7410 return numbuf;
7411#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007412 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007413 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007415 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007416 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007417 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007418 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007419 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007420}
7421
7422/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007423 * Return string "str" in ' quotes, doubling ' characters.
7424 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007426 */
7427 static char_u *
7428string_quote(str, function)
7429 char_u *str;
7430 int function;
7431{
Bram Moolenaar33570922005-01-25 22:26:29 +00007432 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007433 char_u *p, *r, *s;
7434
Bram Moolenaar33570922005-01-25 22:26:29 +00007435 len = (function ? 13 : 3);
7436 if (str != NULL)
7437 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007438 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007439 for (p = str; *p != NUL; mb_ptr_adv(p))
7440 if (*p == '\'')
7441 ++len;
7442 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007443 s = r = alloc(len);
7444 if (r != NULL)
7445 {
7446 if (function)
7447 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007448 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007449 r += 10;
7450 }
7451 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 if (str != NULL)
7454 for (p = str; *p != NUL; )
7455 {
7456 if (*p == '\'')
7457 *r++ = '\'';
7458 MB_COPY_CHAR(p, r);
7459 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007461 if (function)
7462 *r++ = ')';
7463 *r++ = NUL;
7464 }
7465 return s;
7466}
7467
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007468#ifdef FEAT_FLOAT
7469/*
7470 * Convert the string "text" to a floating point number.
7471 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7472 * this always uses a decimal point.
7473 * Returns the length of the text that was consumed.
7474 */
7475 static int
7476string2float(text, value)
7477 char_u *text;
7478 float_T *value; /* result stored here */
7479{
7480 char *s = (char *)text;
7481 float_T f;
7482
7483 f = strtod(s, &s);
7484 *value = f;
7485 return (int)((char_u *)s - text);
7486}
7487#endif
7488
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 * Get the value of an environment variable.
7491 * "arg" is pointing to the '$'. It is advanced to after the name.
7492 * If the environment variable was not set, silently assume it is empty.
7493 * Always return OK.
7494 */
7495 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007496get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 int evaluate;
7500{
7501 char_u *string = NULL;
7502 int len;
7503 int cc;
7504 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007505 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506
7507 ++*arg;
7508 name = *arg;
7509 len = get_env_len(arg);
7510 if (evaluate)
7511 {
7512 if (len != 0)
7513 {
7514 cc = name[len];
7515 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007516 /* first try vim_getenv(), fast for normal environment vars */
7517 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007519 {
7520 if (!mustfree)
7521 string = vim_strsave(string);
7522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 else
7524 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007525 if (mustfree)
7526 vim_free(string);
7527
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 /* next try expanding things like $VIM and ${HOME} */
7529 string = expand_env_save(name - 1);
7530 if (string != NULL && *string == '$')
7531 {
7532 vim_free(string);
7533 string = NULL;
7534 }
7535 }
7536 name[len] = cc;
7537 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007538 rettv->v_type = VAR_STRING;
7539 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 }
7541
7542 return OK;
7543}
7544
7545/*
7546 * Array with names and number of arguments of all internal functions
7547 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7548 */
7549static struct fst
7550{
7551 char *f_name; /* function name */
7552 char f_min_argc; /* minimal number of arguments */
7553 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007554 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007555 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556} functions[] =
7557{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007558#ifdef FEAT_FLOAT
7559 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007560 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007561#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007562 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 {"append", 2, 2, f_append},
7564 {"argc", 0, 0, f_argc},
7565 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007566 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007567#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007568 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007569 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007570 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007573 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 {"bufexists", 1, 1, f_bufexists},
7575 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7576 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7577 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7578 {"buflisted", 1, 1, f_buflisted},
7579 {"bufloaded", 1, 1, f_bufloaded},
7580 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007581 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 {"bufwinnr", 1, 1, f_bufwinnr},
7583 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007584 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007585 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007586#ifdef FEAT_FLOAT
7587 {"ceil", 1, 1, f_ceil},
7588#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007589 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 {"char2nr", 1, 1, f_char2nr},
7591 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007592 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007594#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007595 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007596 {"complete_add", 1, 1, f_complete_add},
7597 {"complete_check", 0, 0, f_complete_check},
7598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007600 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007601#ifdef FEAT_FLOAT
7602 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007603 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007604#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007605 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007607 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007608 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 {"delete", 1, 1, f_delete},
7610 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007611 {"diff_filler", 1, 1, f_diff_filler},
7612 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007613 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007615 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 {"eventhandler", 0, 0, f_eventhandler},
7617 {"executable", 1, 1, f_executable},
7618 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007619#ifdef FEAT_FLOAT
7620 {"exp", 1, 1, f_exp},
7621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007623 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007624 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7626 {"filereadable", 1, 1, f_filereadable},
7627 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007628 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007629 {"finddir", 1, 3, f_finddir},
7630 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007631#ifdef FEAT_FLOAT
7632 {"float2nr", 1, 1, f_float2nr},
7633 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007634 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007635#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007636 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 {"fnamemodify", 2, 2, f_fnamemodify},
7638 {"foldclosed", 1, 1, f_foldclosed},
7639 {"foldclosedend", 1, 1, f_foldclosedend},
7640 {"foldlevel", 1, 1, f_foldlevel},
7641 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007642 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007644 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007645 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007646 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007647 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 {"getbufvar", 2, 2, f_getbufvar},
7649 {"getchar", 0, 1, f_getchar},
7650 {"getcharmod", 0, 0, f_getcharmod},
7651 {"getcmdline", 0, 0, f_getcmdline},
7652 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007653 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007655 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007656 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 {"getfsize", 1, 1, f_getfsize},
7658 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007659 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007660 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007661 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007662 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007663 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007664 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007665 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007666 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007668 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007669 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 {"getwinposx", 0, 0, f_getwinposx},
7671 {"getwinposy", 0, 0, f_getwinposy},
7672 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007673 {"glob", 1, 2, f_glob},
7674 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007676 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007677 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007678 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7680 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7681 {"histadd", 2, 2, f_histadd},
7682 {"histdel", 1, 2, f_histdel},
7683 {"histget", 1, 2, f_histget},
7684 {"histnr", 1, 1, f_histnr},
7685 {"hlID", 1, 1, f_hlID},
7686 {"hlexists", 1, 1, f_hlexists},
7687 {"hostname", 0, 0, f_hostname},
7688 {"iconv", 3, 3, f_iconv},
7689 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007690 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007691 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007693 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 {"inputrestore", 0, 0, f_inputrestore},
7695 {"inputsave", 0, 0, f_inputsave},
7696 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007697 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007699 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007701 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007704 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 {"libcall", 3, 3, f_libcall},
7706 {"libcallnr", 3, 3, f_libcallnr},
7707 {"line", 1, 1, f_line},
7708 {"line2byte", 1, 1, f_line2byte},
7709 {"lispindent", 1, 1, f_lispindent},
7710 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007711#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007712 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007713 {"log10", 1, 1, f_log10},
7714#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007715 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007716 {"maparg", 1, 3, f_maparg},
7717 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007718 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007719 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007720 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007721 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007722 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007723 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007724 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007725 {"max", 1, 1, f_max},
7726 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007727#ifdef vim_mkdir
7728 {"mkdir", 1, 3, f_mkdir},
7729#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007730 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007731#ifdef FEAT_MZSCHEME
7732 {"mzeval", 1, 1, f_mzeval},
7733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 {"nextnonblank", 1, 1, f_nextnonblank},
7735 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007736 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007737#ifdef FEAT_FLOAT
7738 {"pow", 2, 2, f_pow},
7739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007741 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007742 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007743 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007744 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007745 {"reltime", 0, 2, f_reltime},
7746 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 {"remote_expr", 2, 3, f_remote_expr},
7748 {"remote_foreground", 1, 1, f_remote_foreground},
7749 {"remote_peek", 1, 2, f_remote_peek},
7750 {"remote_read", 1, 1, f_remote_read},
7751 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007752 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007754 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007756 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007757#ifdef FEAT_FLOAT
7758 {"round", 1, 1, f_round},
7759#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007760 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007761 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007762 {"searchpair", 3, 7, f_searchpair},
7763 {"searchpairpos", 3, 7, f_searchpairpos},
7764 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 {"server2client", 2, 2, f_server2client},
7766 {"serverlist", 0, 0, f_serverlist},
7767 {"setbufvar", 3, 3, f_setbufvar},
7768 {"setcmdpos", 1, 1, f_setcmdpos},
7769 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007770 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007771 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007772 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007773 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007775 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007776 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007778 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007780#ifdef FEAT_FLOAT
7781 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007782 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007783#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007784 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007785 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007786 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007787 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007788 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007789#ifdef FEAT_FLOAT
7790 {"sqrt", 1, 1, f_sqrt},
7791 {"str2float", 1, 1, f_str2float},
7792#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007793 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794#ifdef HAVE_STRFTIME
7795 {"strftime", 1, 2, f_strftime},
7796#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007797 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007798 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 {"strlen", 1, 1, f_strlen},
7800 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007801 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 {"strtrans", 1, 1, f_strtrans},
7803 {"submatch", 1, 1, f_submatch},
7804 {"substitute", 4, 4, f_substitute},
7805 {"synID", 3, 3, f_synID},
7806 {"synIDattr", 2, 3, f_synIDattr},
7807 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007808 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007809 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007810 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007811 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007812 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007813 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007814 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007815#ifdef FEAT_FLOAT
7816 {"tan", 1, 1, f_tan},
7817 {"tanh", 1, 1, f_tanh},
7818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007820 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 {"tolower", 1, 1, f_tolower},
7822 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007823 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007824#ifdef FEAT_FLOAT
7825 {"trunc", 1, 1, f_trunc},
7826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007828 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 {"virtcol", 1, 1, f_virtcol},
7830 {"visualmode", 0, 1, f_visualmode},
7831 {"winbufnr", 1, 1, f_winbufnr},
7832 {"wincol", 0, 0, f_wincol},
7833 {"winheight", 1, 1, f_winheight},
7834 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007835 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007837 {"winrestview", 1, 1, f_winrestview},
7838 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007840 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841};
7842
7843#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7844
7845/*
7846 * Function given to ExpandGeneric() to obtain the list of internal
7847 * or user defined function names.
7848 */
7849 char_u *
7850get_function_name(xp, idx)
7851 expand_T *xp;
7852 int idx;
7853{
7854 static int intidx = -1;
7855 char_u *name;
7856
7857 if (idx == 0)
7858 intidx = -1;
7859 if (intidx < 0)
7860 {
7861 name = get_user_func_name(xp, idx);
7862 if (name != NULL)
7863 return name;
7864 }
7865 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7866 {
7867 STRCPY(IObuff, functions[intidx].f_name);
7868 STRCAT(IObuff, "(");
7869 if (functions[intidx].f_max_argc == 0)
7870 STRCAT(IObuff, ")");
7871 return IObuff;
7872 }
7873
7874 return NULL;
7875}
7876
7877/*
7878 * Function given to ExpandGeneric() to obtain the list of internal or
7879 * user defined variable or function names.
7880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 char_u *
7882get_expr_name(xp, idx)
7883 expand_T *xp;
7884 int idx;
7885{
7886 static int intidx = -1;
7887 char_u *name;
7888
7889 if (idx == 0)
7890 intidx = -1;
7891 if (intidx < 0)
7892 {
7893 name = get_function_name(xp, idx);
7894 if (name != NULL)
7895 return name;
7896 }
7897 return get_user_var_name(xp, ++intidx);
7898}
7899
7900#endif /* FEAT_CMDL_COMPL */
7901
7902/*
7903 * Find internal function in table above.
7904 * Return index, or -1 if not found
7905 */
7906 static int
7907find_internal_func(name)
7908 char_u *name; /* name of the function */
7909{
7910 int first = 0;
7911 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7912 int cmp;
7913 int x;
7914
7915 /*
7916 * Find the function name in the table. Binary search.
7917 */
7918 while (first <= last)
7919 {
7920 x = first + ((unsigned)(last - first) >> 1);
7921 cmp = STRCMP(name, functions[x].f_name);
7922 if (cmp < 0)
7923 last = x - 1;
7924 else if (cmp > 0)
7925 first = x + 1;
7926 else
7927 return x;
7928 }
7929 return -1;
7930}
7931
7932/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007933 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7934 * name it contains, otherwise return "name".
7935 */
7936 static char_u *
7937deref_func_name(name, lenp)
7938 char_u *name;
7939 int *lenp;
7940{
Bram Moolenaar33570922005-01-25 22:26:29 +00007941 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007942 int cc;
7943
7944 cc = name[*lenp];
7945 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007946 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007947 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007948 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007949 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007950 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007951 {
7952 *lenp = 0;
7953 return (char_u *)""; /* just in case */
7954 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007955 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007957 }
7958
7959 return name;
7960}
7961
7962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 * Allocate a variable for the result of a function.
7964 * Return OK or FAIL.
7965 */
7966 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007967get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7968 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 char_u *name; /* name of the function */
7970 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 char_u **arg; /* argument, pointing to the '(' */
7973 linenr_T firstline; /* first line of range */
7974 linenr_T lastline; /* last line of range */
7975 int *doesrange; /* return: function handled range */
7976 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007977 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
7979 char_u *argp;
7980 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007981 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 int argcount = 0; /* number of arguments found */
7983
7984 /*
7985 * Get the arguments.
7986 */
7987 argp = *arg;
7988 while (argcount < MAX_FUNC_ARGS)
7989 {
7990 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7991 if (*argp == ')' || *argp == ',' || *argp == NUL)
7992 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7994 {
7995 ret = FAIL;
7996 break;
7997 }
7998 ++argcount;
7999 if (*argp != ',')
8000 break;
8001 }
8002 if (*argp == ')')
8003 ++argp;
8004 else
8005 ret = FAIL;
8006
8007 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008008 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008009 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008011 {
8012 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008013 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008014 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008015 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017
8018 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008019 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020
8021 *arg = skipwhite(argp);
8022 return ret;
8023}
8024
8025
8026/*
8027 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008028 * Return OK when the function can't be called, FAIL otherwise.
8029 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 */
8031 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008032call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008033 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 char_u *name; /* name of the function */
8035 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008036 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008038 typval_T *argvars; /* vars for arguments, must have "argcount"
8039 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 linenr_T firstline; /* first line of range */
8041 linenr_T lastline; /* last line of range */
8042 int *doesrange; /* return: function handled range */
8043 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008044 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045{
8046 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047#define ERROR_UNKNOWN 0
8048#define ERROR_TOOMANY 1
8049#define ERROR_TOOFEW 2
8050#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008051#define ERROR_DICT 4
8052#define ERROR_NONE 5
8053#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 int error = ERROR_NONE;
8055 int i;
8056 int llen;
8057 ufunc_T *fp;
8058 int cc;
8059#define FLEN_FIXED 40
8060 char_u fname_buf[FLEN_FIXED + 1];
8061 char_u *fname;
8062
8063 /*
8064 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8065 * Change <SNR>123_name() to K_SNR 123_name().
8066 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8067 */
8068 cc = name[len];
8069 name[len] = NUL;
8070 llen = eval_fname_script(name);
8071 if (llen > 0)
8072 {
8073 fname_buf[0] = K_SPECIAL;
8074 fname_buf[1] = KS_EXTRA;
8075 fname_buf[2] = (int)KE_SNR;
8076 i = 3;
8077 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8078 {
8079 if (current_SID <= 0)
8080 error = ERROR_SCRIPT;
8081 else
8082 {
8083 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8084 i = (int)STRLEN(fname_buf);
8085 }
8086 }
8087 if (i + STRLEN(name + llen) < FLEN_FIXED)
8088 {
8089 STRCPY(fname_buf + i, name + llen);
8090 fname = fname_buf;
8091 }
8092 else
8093 {
8094 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8095 if (fname == NULL)
8096 error = ERROR_OTHER;
8097 else
8098 {
8099 mch_memmove(fname, fname_buf, (size_t)i);
8100 STRCPY(fname + i, name + llen);
8101 }
8102 }
8103 }
8104 else
8105 fname = name;
8106
8107 *doesrange = FALSE;
8108
8109
8110 /* execute the function if no errors detected and executing */
8111 if (evaluate && error == ERROR_NONE)
8112 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008113 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8114 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 error = ERROR_UNKNOWN;
8116
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008117 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {
8119 /*
8120 * User defined function.
8121 */
8122 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008123
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008125 /* Trigger FuncUndefined event, may load the function. */
8126 if (fp == NULL
8127 && apply_autocmds(EVENT_FUNCUNDEFINED,
8128 fname, fname, TRUE, NULL)
8129 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008131 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 fp = find_func(fname);
8133 }
8134#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008135 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008136 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008137 {
8138 /* loaded a package, search for the function again */
8139 fp = find_func(fname);
8140 }
8141
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 if (fp != NULL)
8143 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008144 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008146 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008148 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008150 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008151 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 else
8153 {
8154 /*
8155 * Call the user function.
8156 * Save and restore search patterns, script variables and
8157 * redo buffer.
8158 */
8159 save_search_patterns();
8160 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008161 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008162 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008163 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008164 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8165 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8166 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008167 /* Function was unreferenced while being used, free it
8168 * now. */
8169 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 restoreRedobuff();
8171 restore_search_patterns();
8172 error = ERROR_NONE;
8173 }
8174 }
8175 }
8176 else
8177 {
8178 /*
8179 * Find the function name in the table, call its implementation.
8180 */
8181 i = find_internal_func(fname);
8182 if (i >= 0)
8183 {
8184 if (argcount < functions[i].f_min_argc)
8185 error = ERROR_TOOFEW;
8186 else if (argcount > functions[i].f_max_argc)
8187 error = ERROR_TOOMANY;
8188 else
8189 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008190 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008191 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 error = ERROR_NONE;
8193 }
8194 }
8195 }
8196 /*
8197 * The function call (or "FuncUndefined" autocommand sequence) might
8198 * have been aborted by an error, an interrupt, or an explicitly thrown
8199 * exception that has not been caught so far. This situation can be
8200 * tested for by calling aborting(). For an error in an internal
8201 * function or for the "E132" error in call_user_func(), however, the
8202 * throw point at which the "force_abort" flag (temporarily reset by
8203 * emsg()) is normally updated has not been reached yet. We need to
8204 * update that flag first to make aborting() reliable.
8205 */
8206 update_force_abort();
8207 }
8208 if (error == ERROR_NONE)
8209 ret = OK;
8210
8211 /*
8212 * Report an error unless the argument evaluation or function call has been
8213 * cancelled due to an aborting error, an interrupt, or an exception.
8214 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008215 if (!aborting())
8216 {
8217 switch (error)
8218 {
8219 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008220 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008221 break;
8222 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008223 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008224 break;
8225 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008226 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008227 name);
8228 break;
8229 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008230 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008231 name);
8232 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008233 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008234 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008235 name);
8236 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008237 }
8238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239
8240 name[len] = cc;
8241 if (fname != name && fname != fname_buf)
8242 vim_free(fname);
8243
8244 return ret;
8245}
8246
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008247/*
8248 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008249 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008250 */
8251 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008252emsg_funcname(ermsg, name)
8253 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008254 char_u *name;
8255{
8256 char_u *p;
8257
8258 if (*name == K_SPECIAL)
8259 p = concat_str((char_u *)"<SNR>", name + 3);
8260 else
8261 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008262 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008263 if (p != name)
8264 vim_free(p);
8265}
8266
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008267/*
8268 * Return TRUE for a non-zero Number and a non-empty String.
8269 */
8270 static int
8271non_zero_arg(argvars)
8272 typval_T *argvars;
8273{
8274 return ((argvars[0].v_type == VAR_NUMBER
8275 && argvars[0].vval.v_number != 0)
8276 || (argvars[0].v_type == VAR_STRING
8277 && argvars[0].vval.v_string != NULL
8278 && *argvars[0].vval.v_string != NUL));
8279}
8280
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281/*********************************************
8282 * Implementation of the built-in functions
8283 */
8284
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008285#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008286static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8287
8288/*
8289 * Get the float value of "argvars[0]" into "f".
8290 * Returns FAIL when the argument is not a Number or Float.
8291 */
8292 static int
8293get_float_arg(argvars, f)
8294 typval_T *argvars;
8295 float_T *f;
8296{
8297 if (argvars[0].v_type == VAR_FLOAT)
8298 {
8299 *f = argvars[0].vval.v_float;
8300 return OK;
8301 }
8302 if (argvars[0].v_type == VAR_NUMBER)
8303 {
8304 *f = (float_T)argvars[0].vval.v_number;
8305 return OK;
8306 }
8307 EMSG(_("E808: Number or Float required"));
8308 return FAIL;
8309}
8310
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008311/*
8312 * "abs(expr)" function
8313 */
8314 static void
8315f_abs(argvars, rettv)
8316 typval_T *argvars;
8317 typval_T *rettv;
8318{
8319 if (argvars[0].v_type == VAR_FLOAT)
8320 {
8321 rettv->v_type = VAR_FLOAT;
8322 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8323 }
8324 else
8325 {
8326 varnumber_T n;
8327 int error = FALSE;
8328
8329 n = get_tv_number_chk(&argvars[0], &error);
8330 if (error)
8331 rettv->vval.v_number = -1;
8332 else if (n > 0)
8333 rettv->vval.v_number = n;
8334 else
8335 rettv->vval.v_number = -n;
8336 }
8337}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008338
8339/*
8340 * "acos()" function
8341 */
8342 static void
8343f_acos(argvars, rettv)
8344 typval_T *argvars;
8345 typval_T *rettv;
8346{
8347 float_T f;
8348
8349 rettv->v_type = VAR_FLOAT;
8350 if (get_float_arg(argvars, &f) == OK)
8351 rettv->vval.v_float = acos(f);
8352 else
8353 rettv->vval.v_float = 0.0;
8354}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008355#endif
8356
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008358 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 */
8360 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008361f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008362 typval_T *argvars;
8363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364{
Bram Moolenaar33570922005-01-25 22:26:29 +00008365 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008367 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008368 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008370 if ((l = argvars[0].vval.v_list) != NULL
8371 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8372 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008373 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008374 }
8375 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008376 EMSG(_(e_listreq));
8377}
8378
8379/*
8380 * "append(lnum, string/list)" function
8381 */
8382 static void
8383f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008384 typval_T *argvars;
8385 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008386{
8387 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008388 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008389 list_T *l = NULL;
8390 listitem_T *li = NULL;
8391 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008392 long added = 0;
8393
Bram Moolenaar0d660222005-01-07 21:51:51 +00008394 lnum = get_tv_lnum(argvars);
8395 if (lnum >= 0
8396 && lnum <= curbuf->b_ml.ml_line_count
8397 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008398 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008399 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008400 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008401 l = argvars[1].vval.v_list;
8402 if (l == NULL)
8403 return;
8404 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008405 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008406 for (;;)
8407 {
8408 if (l == NULL)
8409 tv = &argvars[1]; /* append a string */
8410 else if (li == NULL)
8411 break; /* end of list */
8412 else
8413 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008414 line = get_tv_string_chk(tv);
8415 if (line == NULL) /* type error */
8416 {
8417 rettv->vval.v_number = 1; /* Failed */
8418 break;
8419 }
8420 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008421 ++added;
8422 if (l == NULL)
8423 break;
8424 li = li->li_next;
8425 }
8426
8427 appended_lines_mark(lnum, added);
8428 if (curwin->w_cursor.lnum > lnum)
8429 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008431 else
8432 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433}
8434
8435/*
8436 * "argc()" function
8437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008439f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008440 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008443 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444}
8445
8446/*
8447 * "argidx()" function
8448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008450f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008451 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008454 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455}
8456
8457/*
8458 * "argv(nr)" function
8459 */
8460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008461f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008462 typval_T *argvars;
8463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464{
8465 int idx;
8466
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008467 if (argvars[0].v_type != VAR_UNKNOWN)
8468 {
8469 idx = get_tv_number_chk(&argvars[0], NULL);
8470 if (idx >= 0 && idx < ARGCOUNT)
8471 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8472 else
8473 rettv->vval.v_string = NULL;
8474 rettv->v_type = VAR_STRING;
8475 }
8476 else if (rettv_list_alloc(rettv) == OK)
8477 for (idx = 0; idx < ARGCOUNT; ++idx)
8478 list_append_string(rettv->vval.v_list,
8479 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480}
8481
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008482#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008483/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008484 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008485 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008486 static void
8487f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008488 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008489 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008490{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008491 float_T f;
8492
8493 rettv->v_type = VAR_FLOAT;
8494 if (get_float_arg(argvars, &f) == OK)
8495 rettv->vval.v_float = asin(f);
8496 else
8497 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008498}
8499
8500/*
8501 * "atan()" function
8502 */
8503 static void
8504f_atan(argvars, rettv)
8505 typval_T *argvars;
8506 typval_T *rettv;
8507{
8508 float_T f;
8509
8510 rettv->v_type = VAR_FLOAT;
8511 if (get_float_arg(argvars, &f) == OK)
8512 rettv->vval.v_float = atan(f);
8513 else
8514 rettv->vval.v_float = 0.0;
8515}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008516
8517/*
8518 * "atan2()" function
8519 */
8520 static void
8521f_atan2(argvars, rettv)
8522 typval_T *argvars;
8523 typval_T *rettv;
8524{
8525 float_T fx, fy;
8526
8527 rettv->v_type = VAR_FLOAT;
8528 if (get_float_arg(argvars, &fx) == OK
8529 && get_float_arg(&argvars[1], &fy) == OK)
8530 rettv->vval.v_float = atan2(fx, fy);
8531 else
8532 rettv->vval.v_float = 0.0;
8533}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008534#endif
8535
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536/*
8537 * "browse(save, title, initdir, default)" function
8538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008540f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008541 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
8544#ifdef FEAT_BROWSE
8545 int save;
8546 char_u *title;
8547 char_u *initdir;
8548 char_u *defname;
8549 char_u buf[NUMBUFLEN];
8550 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008551 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008553 save = get_tv_number_chk(&argvars[0], &error);
8554 title = get_tv_string_chk(&argvars[1]);
8555 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8556 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008558 if (error || title == NULL || initdir == NULL || defname == NULL)
8559 rettv->vval.v_string = NULL;
8560 else
8561 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008562 do_browse(save ? BROWSE_SAVE : 0,
8563 title, defname, NULL, initdir, NULL, curbuf);
8564#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008565 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008566#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008567 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008568}
8569
8570/*
8571 * "browsedir(title, initdir)" function
8572 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008574f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008575 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008576 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008577{
8578#ifdef FEAT_BROWSE
8579 char_u *title;
8580 char_u *initdir;
8581 char_u buf[NUMBUFLEN];
8582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008583 title = get_tv_string_chk(&argvars[0]);
8584 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008585
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008586 if (title == NULL || initdir == NULL)
8587 rettv->vval.v_string = NULL;
8588 else
8589 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008590 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008592 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595}
8596
Bram Moolenaar33570922005-01-25 22:26:29 +00008597static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008598
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599/*
8600 * Find a buffer by number or exact name.
8601 */
8602 static buf_T *
8603find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008604 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605{
8606 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008608 if (avar->v_type == VAR_NUMBER)
8609 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008610 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008612 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008613 if (buf == NULL)
8614 {
8615 /* No full path name match, try a match with a URL or a "nofile"
8616 * buffer, these don't use the full path. */
8617 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8618 if (buf->b_fname != NULL
8619 && (path_with_url(buf->b_fname)
8620#ifdef FEAT_QUICKFIX
8621 || bt_nofile(buf)
8622#endif
8623 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008624 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008625 break;
8626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 }
8628 return buf;
8629}
8630
8631/*
8632 * "bufexists(expr)" function
8633 */
8634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008635f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008636 typval_T *argvars;
8637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008639 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640}
8641
8642/*
8643 * "buflisted(expr)" function
8644 */
8645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008646f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008647 typval_T *argvars;
8648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649{
8650 buf_T *buf;
8651
8652 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008653 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654}
8655
8656/*
8657 * "bufloaded(expr)" function
8658 */
8659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008660f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008661 typval_T *argvars;
8662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663{
8664 buf_T *buf;
8665
8666 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008667 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668}
8669
Bram Moolenaar33570922005-01-25 22:26:29 +00008670static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008671
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672/*
8673 * Get buffer by number or pattern.
8674 */
8675 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008676get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008677 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008679 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 int save_magic;
8681 char_u *save_cpo;
8682 buf_T *buf;
8683
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008684 if (tv->v_type == VAR_NUMBER)
8685 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008686 if (tv->v_type != VAR_STRING)
8687 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 if (name == NULL || *name == NUL)
8689 return curbuf;
8690 if (name[0] == '$' && name[1] == NUL)
8691 return lastbuf;
8692
8693 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8694 save_magic = p_magic;
8695 p_magic = TRUE;
8696 save_cpo = p_cpo;
8697 p_cpo = (char_u *)"";
8698
8699 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8700 TRUE, FALSE));
8701
8702 p_magic = save_magic;
8703 p_cpo = save_cpo;
8704
8705 /* If not found, try expanding the name, like done for bufexists(). */
8706 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008707 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708
8709 return buf;
8710}
8711
8712/*
8713 * "bufname(expr)" function
8714 */
8715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008716f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008717 typval_T *argvars;
8718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719{
8720 buf_T *buf;
8721
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008722 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008724 buf = get_buf_tv(&argvars[0]);
8725 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008727 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008729 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 --emsg_off;
8731}
8732
8733/*
8734 * "bufnr(expr)" function
8735 */
8736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008738 typval_T *argvars;
8739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740{
8741 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008742 int error = FALSE;
8743 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008745 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008747 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008748 --emsg_off;
8749
8750 /* If the buffer isn't found and the second argument is not zero create a
8751 * new buffer. */
8752 if (buf == NULL
8753 && argvars[1].v_type != VAR_UNKNOWN
8754 && get_tv_number_chk(&argvars[1], &error) != 0
8755 && !error
8756 && (name = get_tv_string_chk(&argvars[0])) != NULL
8757 && !error)
8758 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8759
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008761 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008763 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764}
8765
8766/*
8767 * "bufwinnr(nr)" function
8768 */
8769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 typval_T *argvars;
8772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773{
8774#ifdef FEAT_WINDOWS
8775 win_T *wp;
8776 int winnr = 0;
8777#endif
8778 buf_T *buf;
8779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008780 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008782 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783#ifdef FEAT_WINDOWS
8784 for (wp = firstwin; wp; wp = wp->w_next)
8785 {
8786 ++winnr;
8787 if (wp->w_buffer == buf)
8788 break;
8789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008792 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793#endif
8794 --emsg_off;
8795}
8796
8797/*
8798 * "byte2line(byte)" function
8799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008801f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008802 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804{
8805#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008806 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807#else
8808 long boff = 0;
8809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008810 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008812 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 (linenr_T)0, &boff);
8816#endif
8817}
8818
8819/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008820 * "byteidx()" function
8821 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008823f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008824 typval_T *argvars;
8825 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008826{
8827#ifdef FEAT_MBYTE
8828 char_u *t;
8829#endif
8830 char_u *str;
8831 long idx;
8832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008833 str = get_tv_string_chk(&argvars[0]);
8834 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008835 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008836 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008837 return;
8838
8839#ifdef FEAT_MBYTE
8840 t = str;
8841 for ( ; idx > 0; idx--)
8842 {
8843 if (*t == NUL) /* EOL reached */
8844 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008845 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008846 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008847 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008848#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008849 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008850 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008851#endif
8852}
8853
8854/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008855 * "call(func, arglist)" function
8856 */
8857 static void
8858f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008859 typval_T *argvars;
8860 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008861{
8862 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008863 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008864 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008865 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008866 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008867 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008868
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008869 if (argvars[1].v_type != VAR_LIST)
8870 {
8871 EMSG(_(e_listreq));
8872 return;
8873 }
8874 if (argvars[1].vval.v_list == NULL)
8875 return;
8876
8877 if (argvars[0].v_type == VAR_FUNC)
8878 func = argvars[0].vval.v_string;
8879 else
8880 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008881 if (*func == NUL)
8882 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008883
Bram Moolenaare9a41262005-01-15 22:18:47 +00008884 if (argvars[2].v_type != VAR_UNKNOWN)
8885 {
8886 if (argvars[2].v_type != VAR_DICT)
8887 {
8888 EMSG(_(e_dictreq));
8889 return;
8890 }
8891 selfdict = argvars[2].vval.v_dict;
8892 }
8893
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008894 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8895 item = item->li_next)
8896 {
8897 if (argc == MAX_FUNC_ARGS)
8898 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008899 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008900 break;
8901 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008902 /* Make a copy of each argument. This is needed to be able to set
8903 * v_lock to VAR_FIXED in the copy without changing the original list.
8904 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008905 copy_tv(&item->li_tv, &argv[argc++]);
8906 }
8907
8908 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008909 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008910 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8911 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008912
8913 /* Free the arguments. */
8914 while (argc > 0)
8915 clear_tv(&argv[--argc]);
8916}
8917
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008918#ifdef FEAT_FLOAT
8919/*
8920 * "ceil({float})" function
8921 */
8922 static void
8923f_ceil(argvars, rettv)
8924 typval_T *argvars;
8925 typval_T *rettv;
8926{
8927 float_T f;
8928
8929 rettv->v_type = VAR_FLOAT;
8930 if (get_float_arg(argvars, &f) == OK)
8931 rettv->vval.v_float = ceil(f);
8932 else
8933 rettv->vval.v_float = 0.0;
8934}
8935#endif
8936
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008937/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008938 * "changenr()" function
8939 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008940 static void
8941f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008942 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008943 typval_T *rettv;
8944{
8945 rettv->vval.v_number = curbuf->b_u_seq_cur;
8946}
8947
8948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 * "char2nr(string)" function
8950 */
8951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008952f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008953 typval_T *argvars;
8954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955{
8956#ifdef FEAT_MBYTE
8957 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008958 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 else
8960#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008961 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962}
8963
8964/*
8965 * "cindent(lnum)" function
8966 */
8967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008969 typval_T *argvars;
8970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971{
8972#ifdef FEAT_CINDENT
8973 pos_T pos;
8974 linenr_T lnum;
8975
8976 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8979 {
8980 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 curwin->w_cursor = pos;
8983 }
8984 else
8985#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987}
8988
8989/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008990 * "clearmatches()" function
8991 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008992 static void
8993f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008994 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008995 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008996{
8997#ifdef FEAT_SEARCH_EXTRA
8998 clear_matches(curwin);
8999#endif
9000}
9001
9002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 * "col(string)" function
9004 */
9005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009007 typval_T *argvars;
9008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
9010 colnr_T col = 0;
9011 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009012 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009014 fp = var2fpos(&argvars[0], FALSE, &fnum);
9015 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 {
9017 if (fp->col == MAXCOL)
9018 {
9019 /* '> can be MAXCOL, get the length of the line then */
9020 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009021 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 else
9023 col = MAXCOL;
9024 }
9025 else
9026 {
9027 col = fp->col + 1;
9028#ifdef FEAT_VIRTUALEDIT
9029 /* col(".") when the cursor is on the NUL at the end of the line
9030 * because of "coladd" can be seen as an extra column. */
9031 if (virtual_active() && fp == &curwin->w_cursor)
9032 {
9033 char_u *p = ml_get_cursor();
9034
9035 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9036 curwin->w_virtcol - curwin->w_cursor.coladd))
9037 {
9038# ifdef FEAT_MBYTE
9039 int l;
9040
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009041 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 col += l;
9043# else
9044 if (*p != NUL && p[1] == NUL)
9045 ++col;
9046# endif
9047 }
9048 }
9049#endif
9050 }
9051 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009052 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053}
9054
Bram Moolenaar572cb562005-08-05 21:35:02 +00009055#if defined(FEAT_INS_EXPAND)
9056/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009057 * "complete()" function
9058 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009059 static void
9060f_complete(argvars, rettv)
9061 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009062 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009063{
9064 int startcol;
9065
9066 if ((State & INSERT) == 0)
9067 {
9068 EMSG(_("E785: complete() can only be used in Insert mode"));
9069 return;
9070 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009071
9072 /* Check for undo allowed here, because if something was already inserted
9073 * the line was already saved for undo and this check isn't done. */
9074 if (!undo_allowed())
9075 return;
9076
Bram Moolenaarade00832006-03-10 21:46:58 +00009077 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9078 {
9079 EMSG(_(e_invarg));
9080 return;
9081 }
9082
9083 startcol = get_tv_number_chk(&argvars[0], NULL);
9084 if (startcol <= 0)
9085 return;
9086
9087 set_completion(startcol - 1, argvars[1].vval.v_list);
9088}
9089
9090/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009091 * "complete_add()" function
9092 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009093 static void
9094f_complete_add(argvars, rettv)
9095 typval_T *argvars;
9096 typval_T *rettv;
9097{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009098 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009099}
9100
9101/*
9102 * "complete_check()" function
9103 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009104 static void
9105f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009106 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009107 typval_T *rettv;
9108{
9109 int saved = RedrawingDisabled;
9110
9111 RedrawingDisabled = 0;
9112 ins_compl_check_keys(0);
9113 rettv->vval.v_number = compl_interrupted;
9114 RedrawingDisabled = saved;
9115}
9116#endif
9117
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118/*
9119 * "confirm(message, buttons[, default [, type]])" function
9120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009123 typval_T *argvars UNUSED;
9124 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125{
9126#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9127 char_u *message;
9128 char_u *buttons = NULL;
9129 char_u buf[NUMBUFLEN];
9130 char_u buf2[NUMBUFLEN];
9131 int def = 1;
9132 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009133 char_u *typestr;
9134 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009136 message = get_tv_string_chk(&argvars[0]);
9137 if (message == NULL)
9138 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009139 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009141 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9142 if (buttons == NULL)
9143 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009144 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009146 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009147 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009149 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9150 if (typestr == NULL)
9151 error = TRUE;
9152 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009154 switch (TOUPPER_ASC(*typestr))
9155 {
9156 case 'E': type = VIM_ERROR; break;
9157 case 'Q': type = VIM_QUESTION; break;
9158 case 'I': type = VIM_INFO; break;
9159 case 'W': type = VIM_WARNING; break;
9160 case 'G': type = VIM_GENERIC; break;
9161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 }
9163 }
9164 }
9165 }
9166
9167 if (buttons == NULL || *buttons == NUL)
9168 buttons = (char_u *)_("&Ok");
9169
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009170 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009171 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173#endif
9174}
9175
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009176/*
9177 * "copy()" function
9178 */
9179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009180f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009181 typval_T *argvars;
9182 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009183{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009184 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009185}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009187#ifdef FEAT_FLOAT
9188/*
9189 * "cos()" function
9190 */
9191 static void
9192f_cos(argvars, rettv)
9193 typval_T *argvars;
9194 typval_T *rettv;
9195{
9196 float_T f;
9197
9198 rettv->v_type = VAR_FLOAT;
9199 if (get_float_arg(argvars, &f) == OK)
9200 rettv->vval.v_float = cos(f);
9201 else
9202 rettv->vval.v_float = 0.0;
9203}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009204
9205/*
9206 * "cosh()" function
9207 */
9208 static void
9209f_cosh(argvars, rettv)
9210 typval_T *argvars;
9211 typval_T *rettv;
9212{
9213 float_T f;
9214
9215 rettv->v_type = VAR_FLOAT;
9216 if (get_float_arg(argvars, &f) == OK)
9217 rettv->vval.v_float = cosh(f);
9218 else
9219 rettv->vval.v_float = 0.0;
9220}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009221#endif
9222
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009224 * "count()" function
9225 */
9226 static void
9227f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009228 typval_T *argvars;
9229 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009230{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009231 long n = 0;
9232 int ic = FALSE;
9233
Bram Moolenaare9a41262005-01-15 22:18:47 +00009234 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009235 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009236 listitem_T *li;
9237 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009238 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009239
Bram Moolenaare9a41262005-01-15 22:18:47 +00009240 if ((l = argvars[0].vval.v_list) != NULL)
9241 {
9242 li = l->lv_first;
9243 if (argvars[2].v_type != VAR_UNKNOWN)
9244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009245 int error = FALSE;
9246
9247 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009248 if (argvars[3].v_type != VAR_UNKNOWN)
9249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009250 idx = get_tv_number_chk(&argvars[3], &error);
9251 if (!error)
9252 {
9253 li = list_find(l, idx);
9254 if (li == NULL)
9255 EMSGN(_(e_listidx), idx);
9256 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009257 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009258 if (error)
9259 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009260 }
9261
9262 for ( ; li != NULL; li = li->li_next)
9263 if (tv_equal(&li->li_tv, &argvars[1], ic))
9264 ++n;
9265 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009266 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009267 else if (argvars[0].v_type == VAR_DICT)
9268 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009269 int todo;
9270 dict_T *d;
9271 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009272
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009273 if ((d = argvars[0].vval.v_dict) != NULL)
9274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009275 int error = FALSE;
9276
Bram Moolenaare9a41262005-01-15 22:18:47 +00009277 if (argvars[2].v_type != VAR_UNKNOWN)
9278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009279 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009280 if (argvars[3].v_type != VAR_UNKNOWN)
9281 EMSG(_(e_invarg));
9282 }
9283
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009284 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009285 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009286 {
9287 if (!HASHITEM_EMPTY(hi))
9288 {
9289 --todo;
9290 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9291 ++n;
9292 }
9293 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009294 }
9295 }
9296 else
9297 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009298 rettv->vval.v_number = n;
9299}
9300
9301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9303 *
9304 * Checks the existence of a cscope connection.
9305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009307f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009308 typval_T *argvars UNUSED;
9309 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310{
9311#ifdef FEAT_CSCOPE
9312 int num = 0;
9313 char_u *dbpath = NULL;
9314 char_u *prepend = NULL;
9315 char_u buf[NUMBUFLEN];
9316
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009317 if (argvars[0].v_type != VAR_UNKNOWN
9318 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009320 num = (int)get_tv_number(&argvars[0]);
9321 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009322 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009323 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 }
9325
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009326 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327#endif
9328}
9329
9330/*
9331 * "cursor(lnum, col)" function
9332 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009333 * Moves the cursor to the specified line and column.
9334 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009337f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009338 typval_T *argvars;
9339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340{
9341 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009342#ifdef FEAT_VIRTUALEDIT
9343 long coladd = 0;
9344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009346 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009347 if (argvars[1].v_type == VAR_UNKNOWN)
9348 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009349 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009350
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009351 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009352 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009353 line = pos.lnum;
9354 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009355#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009356 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009357#endif
9358 }
9359 else
9360 {
9361 line = get_tv_lnum(argvars);
9362 col = get_tv_number_chk(&argvars[1], NULL);
9363#ifdef FEAT_VIRTUALEDIT
9364 if (argvars[2].v_type != VAR_UNKNOWN)
9365 coladd = get_tv_number_chk(&argvars[2], NULL);
9366#endif
9367 }
9368 if (line < 0 || col < 0
9369#ifdef FEAT_VIRTUALEDIT
9370 || coladd < 0
9371#endif
9372 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009373 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 if (line > 0)
9375 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 if (col > 0)
9377 curwin->w_cursor.col = col - 1;
9378#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009379 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380#endif
9381
9382 /* Make sure the cursor is in a valid position. */
9383 check_cursor();
9384#ifdef FEAT_MBYTE
9385 /* Correct cursor for multi-byte character. */
9386 if (has_mbyte)
9387 mb_adjust_cursor();
9388#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009389
9390 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009391 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392}
9393
9394/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009395 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 */
9397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009398f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009399 typval_T *argvars;
9400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009402 int noref = 0;
9403
9404 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009405 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009406 if (noref < 0 || noref > 1)
9407 EMSG(_(e_invarg));
9408 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009409 {
9410 current_copyID += COPYID_INC;
9411 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413}
9414
9415/*
9416 * "delete()" function
9417 */
9418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009419f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009420 typval_T *argvars;
9421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422{
9423 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009424 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009426 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427}
9428
9429/*
9430 * "did_filetype()" function
9431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009433f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009434 typval_T *argvars UNUSED;
9435 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436{
9437#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439#endif
9440}
9441
9442/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009443 * "diff_filler()" function
9444 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009446f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009447 typval_T *argvars UNUSED;
9448 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009449{
9450#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009451 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009452#endif
9453}
9454
9455/*
9456 * "diff_hlID()" function
9457 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009460 typval_T *argvars UNUSED;
9461 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009462{
9463#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009465 static linenr_T prev_lnum = 0;
9466 static int changedtick = 0;
9467 static int fnum = 0;
9468 static int change_start = 0;
9469 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009470 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009471 int filler_lines;
9472 int col;
9473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009474 if (lnum < 0) /* ignore type error in {lnum} arg */
9475 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009476 if (lnum != prev_lnum
9477 || changedtick != curbuf->b_changedtick
9478 || fnum != curbuf->b_fnum)
9479 {
9480 /* New line, buffer, change: need to get the values. */
9481 filler_lines = diff_check(curwin, lnum);
9482 if (filler_lines < 0)
9483 {
9484 if (filler_lines == -1)
9485 {
9486 change_start = MAXCOL;
9487 change_end = -1;
9488 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9489 hlID = HLF_ADD; /* added line */
9490 else
9491 hlID = HLF_CHD; /* changed line */
9492 }
9493 else
9494 hlID = HLF_ADD; /* added line */
9495 }
9496 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009497 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009498 prev_lnum = lnum;
9499 changedtick = curbuf->b_changedtick;
9500 fnum = curbuf->b_fnum;
9501 }
9502
9503 if (hlID == HLF_CHD || hlID == HLF_TXD)
9504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009505 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009506 if (col >= change_start && col <= change_end)
9507 hlID = HLF_TXD; /* changed text */
9508 else
9509 hlID = HLF_CHD; /* changed line */
9510 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009511 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009512#endif
9513}
9514
9515/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009516 * "empty({expr})" function
9517 */
9518 static void
9519f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009520 typval_T *argvars;
9521 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009522{
9523 int n;
9524
9525 switch (argvars[0].v_type)
9526 {
9527 case VAR_STRING:
9528 case VAR_FUNC:
9529 n = argvars[0].vval.v_string == NULL
9530 || *argvars[0].vval.v_string == NUL;
9531 break;
9532 case VAR_NUMBER:
9533 n = argvars[0].vval.v_number == 0;
9534 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009535#ifdef FEAT_FLOAT
9536 case VAR_FLOAT:
9537 n = argvars[0].vval.v_float == 0.0;
9538 break;
9539#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009540 case VAR_LIST:
9541 n = argvars[0].vval.v_list == NULL
9542 || argvars[0].vval.v_list->lv_first == NULL;
9543 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009544 case VAR_DICT:
9545 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009546 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009547 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009548 default:
9549 EMSG2(_(e_intern2), "f_empty()");
9550 n = 0;
9551 }
9552
9553 rettv->vval.v_number = n;
9554}
9555
9556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 * "escape({string}, {chars})" function
9558 */
9559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009561 typval_T *argvars;
9562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563{
9564 char_u buf[NUMBUFLEN];
9565
Bram Moolenaar758711c2005-02-02 23:11:38 +00009566 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9567 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009568 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569}
9570
9571/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009572 * "eval()" function
9573 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009574 static void
9575f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009576 typval_T *argvars;
9577 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009578{
9579 char_u *s;
9580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009581 s = get_tv_string_chk(&argvars[0]);
9582 if (s != NULL)
9583 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009584
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9586 {
9587 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009588 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009589 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009590 else if (*s != NUL)
9591 EMSG(_(e_trailing));
9592}
9593
9594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 * "eventhandler()" function
9596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009599 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603}
9604
9605/*
9606 * "executable()" function
9607 */
9608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009609f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009610 typval_T *argvars;
9611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009613 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614}
9615
9616/*
9617 * "exists()" function
9618 */
9619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620f_exists(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 *p;
9625 char_u *name;
9626 int n = FALSE;
9627 int len = 0;
9628
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 if (*p == '$') /* environment variable */
9631 {
9632 /* first try "normal" environment variables (fast) */
9633 if (mch_getenv(p + 1) != NULL)
9634 n = TRUE;
9635 else
9636 {
9637 /* try expanding things like $VIM and ${HOME} */
9638 p = expand_env_save(p);
9639 if (p != NULL && *p != '$')
9640 n = TRUE;
9641 vim_free(p);
9642 }
9643 }
9644 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009646 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009647 if (*skipwhite(p) != NUL)
9648 n = FALSE; /* trailing garbage */
9649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 else if (*p == '*') /* internal or user defined function */
9651 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009652 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 }
9654 else if (*p == ':')
9655 {
9656 n = cmd_exists(p + 1);
9657 }
9658 else if (*p == '#')
9659 {
9660#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009661 if (p[1] == '#')
9662 n = autocmd_supported(p + 2);
9663 else
9664 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665#endif
9666 }
9667 else /* internal variable */
9668 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009669 char_u *tofree;
9670 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009672 /* get_name_len() takes care of expanding curly braces */
9673 name = p;
9674 len = get_name_len(&p, &tofree, TRUE, FALSE);
9675 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009677 if (tofree != NULL)
9678 name = tofree;
9679 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9680 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009682 /* handle d.key, l[idx], f(expr) */
9683 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9684 if (n)
9685 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 }
9687 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009688 if (*p != NUL)
9689 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009691 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 }
9693
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695}
9696
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009697#ifdef FEAT_FLOAT
9698/*
9699 * "exp()" function
9700 */
9701 static void
9702f_exp(argvars, rettv)
9703 typval_T *argvars;
9704 typval_T *rettv;
9705{
9706 float_T f;
9707
9708 rettv->v_type = VAR_FLOAT;
9709 if (get_float_arg(argvars, &f) == OK)
9710 rettv->vval.v_float = exp(f);
9711 else
9712 rettv->vval.v_float = 0.0;
9713}
9714#endif
9715
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716/*
9717 * "expand()" function
9718 */
9719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009721 typval_T *argvars;
9722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723{
9724 char_u *s;
9725 int len;
9726 char_u *errormsg;
9727 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9728 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009729 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009731 rettv->v_type = VAR_STRING;
9732 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 if (*s == '%' || *s == '#' || *s == '<')
9734 {
9735 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009736 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 --emsg_off;
9738 }
9739 else
9740 {
9741 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009742 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009743 if (argvars[1].v_type != VAR_UNKNOWN
9744 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009746 if (!error)
9747 {
9748 ExpandInit(&xpc);
9749 xpc.xp_context = EXPAND_FILES;
9750 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009751 }
9752 else
9753 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 }
9755}
9756
9757/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009758 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009759 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009760 */
9761 static void
9762f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009763 typval_T *argvars;
9764 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009765{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009766 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009767 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009768 list_T *l1, *l2;
9769 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009770 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009771 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009772
Bram Moolenaare9a41262005-01-15 22:18:47 +00009773 l1 = argvars[0].vval.v_list;
9774 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009775 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9776 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009777 {
9778 if (argvars[2].v_type != VAR_UNKNOWN)
9779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009780 before = get_tv_number_chk(&argvars[2], &error);
9781 if (error)
9782 return; /* type error; errmsg already given */
9783
Bram Moolenaar758711c2005-02-02 23:11:38 +00009784 if (before == l1->lv_len)
9785 item = NULL;
9786 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009787 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009788 item = list_find(l1, before);
9789 if (item == NULL)
9790 {
9791 EMSGN(_(e_listidx), before);
9792 return;
9793 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009794 }
9795 }
9796 else
9797 item = NULL;
9798 list_extend(l1, l2, item);
9799
Bram Moolenaare9a41262005-01-15 22:18:47 +00009800 copy_tv(&argvars[0], rettv);
9801 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009802 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009803 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9804 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009805 dict_T *d1, *d2;
9806 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009807 char_u *action;
9808 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009809 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009810 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009811
9812 d1 = argvars[0].vval.v_dict;
9813 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009814 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9815 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009816 {
9817 /* Check the third argument. */
9818 if (argvars[2].v_type != VAR_UNKNOWN)
9819 {
9820 static char *(av[]) = {"keep", "force", "error"};
9821
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009822 action = get_tv_string_chk(&argvars[2]);
9823 if (action == NULL)
9824 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009825 for (i = 0; i < 3; ++i)
9826 if (STRCMP(action, av[i]) == 0)
9827 break;
9828 if (i == 3)
9829 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009830 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009831 return;
9832 }
9833 }
9834 else
9835 action = (char_u *)"force";
9836
9837 /* Go over all entries in the second dict and add them to the
9838 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009839 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009840 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009841 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009842 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009843 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009844 --todo;
9845 di1 = dict_find(d1, hi2->hi_key, -1);
9846 if (di1 == NULL)
9847 {
9848 di1 = dictitem_copy(HI2DI(hi2));
9849 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9850 dictitem_free(di1);
9851 }
9852 else if (*action == 'e')
9853 {
9854 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9855 break;
9856 }
9857 else if (*action == 'f')
9858 {
9859 clear_tv(&di1->di_tv);
9860 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9861 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009862 }
9863 }
9864
Bram Moolenaare9a41262005-01-15 22:18:47 +00009865 copy_tv(&argvars[0], rettv);
9866 }
9867 }
9868 else
9869 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009870}
9871
9872/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009873 * "feedkeys()" function
9874 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009875 static void
9876f_feedkeys(argvars, rettv)
9877 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009878 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009879{
9880 int remap = TRUE;
9881 char_u *keys, *flags;
9882 char_u nbuf[NUMBUFLEN];
9883 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009884 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009885
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009886 /* This is not allowed in the sandbox. If the commands would still be
9887 * executed in the sandbox it would be OK, but it probably happens later,
9888 * when "sandbox" is no longer set. */
9889 if (check_secure())
9890 return;
9891
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009892 keys = get_tv_string(&argvars[0]);
9893 if (*keys != NUL)
9894 {
9895 if (argvars[1].v_type != VAR_UNKNOWN)
9896 {
9897 flags = get_tv_string_buf(&argvars[1], nbuf);
9898 for ( ; *flags != NUL; ++flags)
9899 {
9900 switch (*flags)
9901 {
9902 case 'n': remap = FALSE; break;
9903 case 'm': remap = TRUE; break;
9904 case 't': typed = TRUE; break;
9905 }
9906 }
9907 }
9908
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009909 /* Need to escape K_SPECIAL and CSI before putting the string in the
9910 * typeahead buffer. */
9911 keys_esc = vim_strsave_escape_csi(keys);
9912 if (keys_esc != NULL)
9913 {
9914 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009915 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009916 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009917 if (vgetc_busy)
9918 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009919 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009920 }
9921}
9922
9923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 * "filereadable()" function
9925 */
9926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009927f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009928 typval_T *argvars;
9929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930{
Bram Moolenaarc236c162008-07-13 17:41:49 +00009931 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 char_u *p;
9933 int n;
9934
Bram Moolenaarc236c162008-07-13 17:41:49 +00009935#ifndef O_NONBLOCK
9936# define O_NONBLOCK 0
9937#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009938 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +00009939 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
9940 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 {
9942 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009943 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 }
9945 else
9946 n = FALSE;
9947
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009948 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949}
9950
9951/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009952 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 * rights to write into.
9954 */
9955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009956f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009957 typval_T *argvars;
9958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009960 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961}
9962
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009963static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009964
9965 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009966findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009967 typval_T *argvars;
9968 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009969 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009970{
9971#ifdef FEAT_SEARCHPATH
9972 char_u *fname;
9973 char_u *fresult = NULL;
9974 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9975 char_u *p;
9976 char_u pathbuf[NUMBUFLEN];
9977 int count = 1;
9978 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009979 int error = FALSE;
9980#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009981
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009982 rettv->vval.v_string = NULL;
9983 rettv->v_type = VAR_STRING;
9984
9985#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009986 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009987
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009988 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009989 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009990 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9991 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009992 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009993 else
9994 {
9995 if (*p != NUL)
9996 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009997
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009998 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009999 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010000 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010001 }
10002
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010003 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10004 error = TRUE;
10005
10006 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010007 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010008 do
10009 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010010 if (rettv->v_type == VAR_STRING)
10011 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010012 fresult = find_file_in_path_option(first ? fname : NULL,
10013 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010014 0, first, path,
10015 find_what,
10016 curbuf->b_ffname,
10017 find_what == FINDFILE_DIR
10018 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010019 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010020
10021 if (fresult != NULL && rettv->v_type == VAR_LIST)
10022 list_append_string(rettv->vval.v_list, fresult, -1);
10023
10024 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010025 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010026
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010027 if (rettv->v_type == VAR_STRING)
10028 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010029#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010030}
10031
Bram Moolenaar33570922005-01-25 22:26:29 +000010032static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10033static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010034
10035/*
10036 * Implementation of map() and filter().
10037 */
10038 static void
10039filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010040 typval_T *argvars;
10041 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010042 int map;
10043{
10044 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010045 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010046 listitem_T *li, *nli;
10047 list_T *l = NULL;
10048 dictitem_T *di;
10049 hashtab_T *ht;
10050 hashitem_T *hi;
10051 dict_T *d = NULL;
10052 typval_T save_val;
10053 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010054 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010055 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010056 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010057 int save_did_emsg;
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010058 int index = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010059
Bram Moolenaare9a41262005-01-15 22:18:47 +000010060 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010061 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010062 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010063 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010064 return;
10065 }
10066 else if (argvars[0].v_type == VAR_DICT)
10067 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010068 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010069 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010070 return;
10071 }
10072 else
10073 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010074 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010075 return;
10076 }
10077
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010078 expr = get_tv_string_buf_chk(&argvars[1], buf);
10079 /* On type errors, the preceding call has already displayed an error
10080 * message. Avoid a misleading error message for an empty string that
10081 * was not passed as argument. */
10082 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010084 prepare_vimvar(VV_VAL, &save_val);
10085 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010086
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010087 /* We reset "did_emsg" to be able to detect whether an error
10088 * occurred during evaluation of the expression. */
10089 save_did_emsg = did_emsg;
10090 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010091
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010092 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010093 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010095 vimvars[VV_KEY].vv_type = VAR_STRING;
10096
10097 ht = &d->dv_hashtab;
10098 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010099 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010100 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010101 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010102 if (!HASHITEM_EMPTY(hi))
10103 {
10104 --todo;
10105 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010106 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010107 break;
10108 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010109 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010110 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010111 break;
10112 if (!map && rem)
10113 dictitem_remove(d, di);
10114 clear_tv(&vimvars[VV_KEY].vv_tv);
10115 }
10116 }
10117 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010118 }
10119 else
10120 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010121 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10122
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010123 for (li = l->lv_first; li != NULL; li = nli)
10124 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010125 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010126 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010127 nli = li->li_next;
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010128 vimvars[VV_KEY].vv_nr = index;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010129 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010130 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010131 break;
10132 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010133 listitem_remove(l, li);
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010134 ++index;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010135 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010136 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010137
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010138 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010139 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010140
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010141 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010142 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010143
10144 copy_tv(&argvars[0], rettv);
10145}
10146
10147 static int
10148filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010149 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010150 char_u *expr;
10151 int map;
10152 int *remp;
10153{
Bram Moolenaar33570922005-01-25 22:26:29 +000010154 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010155 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010156 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010157
Bram Moolenaar33570922005-01-25 22:26:29 +000010158 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010159 s = expr;
10160 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010161 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010162 if (*s != NUL) /* check for trailing chars after expr */
10163 {
10164 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010165 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010166 }
10167 if (map)
10168 {
10169 /* map(): replace the list item value */
10170 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010171 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010172 *tv = rettv;
10173 }
10174 else
10175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010176 int error = FALSE;
10177
Bram Moolenaare9a41262005-01-15 22:18:47 +000010178 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010179 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010180 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010181 /* On type error, nothing has been removed; return FAIL to stop the
10182 * loop. The error message was given by get_tv_number_chk(). */
10183 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010184 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010185 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010186 retval = OK;
10187theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010188 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010189 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010190}
10191
10192/*
10193 * "filter()" function
10194 */
10195 static void
10196f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010197 typval_T *argvars;
10198 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010199{
10200 filter_map(argvars, rettv, FALSE);
10201}
10202
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010203/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010204 * "finddir({fname}[, {path}[, {count}]])" function
10205 */
10206 static void
10207f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010208 typval_T *argvars;
10209 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010210{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010211 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010212}
10213
10214/*
10215 * "findfile({fname}[, {path}[, {count}]])" function
10216 */
10217 static void
10218f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010219 typval_T *argvars;
10220 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010221{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010222 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010223}
10224
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010225#ifdef FEAT_FLOAT
10226/*
10227 * "float2nr({float})" function
10228 */
10229 static void
10230f_float2nr(argvars, rettv)
10231 typval_T *argvars;
10232 typval_T *rettv;
10233{
10234 float_T f;
10235
10236 if (get_float_arg(argvars, &f) == OK)
10237 {
10238 if (f < -0x7fffffff)
10239 rettv->vval.v_number = -0x7fffffff;
10240 else if (f > 0x7fffffff)
10241 rettv->vval.v_number = 0x7fffffff;
10242 else
10243 rettv->vval.v_number = (varnumber_T)f;
10244 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010245}
10246
10247/*
10248 * "floor({float})" function
10249 */
10250 static void
10251f_floor(argvars, rettv)
10252 typval_T *argvars;
10253 typval_T *rettv;
10254{
10255 float_T f;
10256
10257 rettv->v_type = VAR_FLOAT;
10258 if (get_float_arg(argvars, &f) == OK)
10259 rettv->vval.v_float = floor(f);
10260 else
10261 rettv->vval.v_float = 0.0;
10262}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010263
10264/*
10265 * "fmod()" function
10266 */
10267 static void
10268f_fmod(argvars, rettv)
10269 typval_T *argvars;
10270 typval_T *rettv;
10271{
10272 float_T fx, fy;
10273
10274 rettv->v_type = VAR_FLOAT;
10275 if (get_float_arg(argvars, &fx) == OK
10276 && get_float_arg(&argvars[1], &fy) == OK)
10277 rettv->vval.v_float = fmod(fx, fy);
10278 else
10279 rettv->vval.v_float = 0.0;
10280}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010281#endif
10282
Bram Moolenaar0d660222005-01-07 21:51:51 +000010283/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010284 * "fnameescape({string})" function
10285 */
10286 static void
10287f_fnameescape(argvars, rettv)
10288 typval_T *argvars;
10289 typval_T *rettv;
10290{
10291 rettv->vval.v_string = vim_strsave_fnameescape(
10292 get_tv_string(&argvars[0]), FALSE);
10293 rettv->v_type = VAR_STRING;
10294}
10295
10296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 * "fnamemodify({fname}, {mods})" function
10298 */
10299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010300f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010301 typval_T *argvars;
10302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303{
10304 char_u *fname;
10305 char_u *mods;
10306 int usedlen = 0;
10307 int len;
10308 char_u *fbuf = NULL;
10309 char_u buf[NUMBUFLEN];
10310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010311 fname = get_tv_string_chk(&argvars[0]);
10312 mods = get_tv_string_buf_chk(&argvars[1], buf);
10313 if (fname == NULL || mods == NULL)
10314 fname = NULL;
10315 else
10316 {
10317 len = (int)STRLEN(fname);
10318 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010321 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010323 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010325 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 vim_free(fbuf);
10327}
10328
Bram Moolenaar33570922005-01-25 22:26:29 +000010329static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330
10331/*
10332 * "foldclosed()" function
10333 */
10334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010335foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010336 typval_T *argvars;
10337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 int end;
10339{
10340#ifdef FEAT_FOLDING
10341 linenr_T lnum;
10342 linenr_T first, last;
10343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010344 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10346 {
10347 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10348 {
10349 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010350 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010352 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 return;
10354 }
10355 }
10356#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010357 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358}
10359
10360/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010361 * "foldclosed()" function
10362 */
10363 static void
10364f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010365 typval_T *argvars;
10366 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010367{
10368 foldclosed_both(argvars, rettv, FALSE);
10369}
10370
10371/*
10372 * "foldclosedend()" function
10373 */
10374 static void
10375f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010376 typval_T *argvars;
10377 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010378{
10379 foldclosed_both(argvars, rettv, TRUE);
10380}
10381
10382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 * "foldlevel()" function
10384 */
10385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010386f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010387 typval_T *argvars;
10388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389{
10390#ifdef FEAT_FOLDING
10391 linenr_T lnum;
10392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010393 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010395 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397}
10398
10399/*
10400 * "foldtext()" function
10401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010403f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010404 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406{
10407#ifdef FEAT_FOLDING
10408 linenr_T lnum;
10409 char_u *s;
10410 char_u *r;
10411 int len;
10412 char *txt;
10413#endif
10414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010415 rettv->v_type = VAR_STRING;
10416 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010418 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10419 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10420 <= curbuf->b_ml.ml_line_count
10421 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 {
10423 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010424 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10425 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 {
10427 if (!linewhite(lnum))
10428 break;
10429 ++lnum;
10430 }
10431
10432 /* Find interesting text in this line. */
10433 s = skipwhite(ml_get(lnum));
10434 /* skip C comment-start */
10435 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010436 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010438 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010439 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010440 {
10441 s = skipwhite(ml_get(lnum + 1));
10442 if (*s == '*')
10443 s = skipwhite(s + 1);
10444 }
10445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 txt = _("+-%s%3ld lines: ");
10447 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010448 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 + 20 /* for %3ld */
10450 + STRLEN(s))); /* concatenated */
10451 if (r != NULL)
10452 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010453 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10454 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10455 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 len = (int)STRLEN(r);
10457 STRCAT(r, s);
10458 /* remove 'foldmarker' and 'commentstring' */
10459 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010460 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 }
10462 }
10463#endif
10464}
10465
10466/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010467 * "foldtextresult(lnum)" function
10468 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010471 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010472 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010473{
10474#ifdef FEAT_FOLDING
10475 linenr_T lnum;
10476 char_u *text;
10477 char_u buf[51];
10478 foldinfo_T foldinfo;
10479 int fold_count;
10480#endif
10481
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010482 rettv->v_type = VAR_STRING;
10483 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010484#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010485 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010486 /* treat illegal types and illegal string values for {lnum} the same */
10487 if (lnum < 0)
10488 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010489 fold_count = foldedCount(curwin, lnum, &foldinfo);
10490 if (fold_count > 0)
10491 {
10492 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10493 &foldinfo, buf);
10494 if (text == buf)
10495 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010496 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010497 }
10498#endif
10499}
10500
10501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 * "foreground()" function
10503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010505f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010506 typval_T *argvars UNUSED;
10507 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509#ifdef FEAT_GUI
10510 if (gui.in_use)
10511 gui_mch_set_foreground();
10512#else
10513# ifdef WIN32
10514 win32_set_foreground();
10515# endif
10516#endif
10517}
10518
10519/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010520 * "function()" function
10521 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010523f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010524 typval_T *argvars;
10525 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010526{
10527 char_u *s;
10528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010529 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010530 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010531 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010532 /* Don't check an autoload name for existence here. */
10533 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010534 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010535 else
10536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010537 rettv->vval.v_string = vim_strsave(s);
10538 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010539 }
10540}
10541
10542/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010543 * "garbagecollect()" function
10544 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010545 static void
10546f_garbagecollect(argvars, rettv)
10547 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010548 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010549{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010550 /* This is postponed until we are back at the toplevel, because we may be
10551 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10552 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010553
10554 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10555 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010556}
10557
10558/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010559 * "get()" function
10560 */
10561 static void
10562f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010563 typval_T *argvars;
10564 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010565{
Bram Moolenaar33570922005-01-25 22:26:29 +000010566 listitem_T *li;
10567 list_T *l;
10568 dictitem_T *di;
10569 dict_T *d;
10570 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010571
Bram Moolenaare9a41262005-01-15 22:18:47 +000010572 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010573 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010574 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010575 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010576 int error = FALSE;
10577
10578 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10579 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010581 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010582 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010583 else if (argvars[0].v_type == VAR_DICT)
10584 {
10585 if ((d = argvars[0].vval.v_dict) != NULL)
10586 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010587 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010588 if (di != NULL)
10589 tv = &di->di_tv;
10590 }
10591 }
10592 else
10593 EMSG2(_(e_listdictarg), "get()");
10594
10595 if (tv == NULL)
10596 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010597 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010598 copy_tv(&argvars[2], rettv);
10599 }
10600 else
10601 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010602}
10603
Bram Moolenaar342337a2005-07-21 21:11:17 +000010604static 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 +000010605
10606/*
10607 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010608 * Return a range (from start to end) of lines in rettv from the specified
10609 * buffer.
10610 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010611 */
10612 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010613get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010614 buf_T *buf;
10615 linenr_T start;
10616 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010617 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010618 typval_T *rettv;
10619{
10620 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010621
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010622 if (retlist && rettv_list_alloc(rettv) == FAIL)
10623 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010624
10625 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10626 return;
10627
10628 if (!retlist)
10629 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010630 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10631 p = ml_get_buf(buf, start, FALSE);
10632 else
10633 p = (char_u *)"";
10634
10635 rettv->v_type = VAR_STRING;
10636 rettv->vval.v_string = vim_strsave(p);
10637 }
10638 else
10639 {
10640 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010641 return;
10642
10643 if (start < 1)
10644 start = 1;
10645 if (end > buf->b_ml.ml_line_count)
10646 end = buf->b_ml.ml_line_count;
10647 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010648 if (list_append_string(rettv->vval.v_list,
10649 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010650 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010651 }
10652}
10653
10654/*
10655 * "getbufline()" function
10656 */
10657 static void
10658f_getbufline(argvars, rettv)
10659 typval_T *argvars;
10660 typval_T *rettv;
10661{
10662 linenr_T lnum;
10663 linenr_T end;
10664 buf_T *buf;
10665
10666 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10667 ++emsg_off;
10668 buf = get_buf_tv(&argvars[0]);
10669 --emsg_off;
10670
Bram Moolenaar661b1822005-07-28 22:36:45 +000010671 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010672 if (argvars[2].v_type == VAR_UNKNOWN)
10673 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010674 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010675 end = get_tv_lnum_buf(&argvars[2], buf);
10676
Bram Moolenaar342337a2005-07-21 21:11:17 +000010677 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010678}
10679
Bram Moolenaar0d660222005-01-07 21:51:51 +000010680/*
10681 * "getbufvar()" function
10682 */
10683 static void
10684f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010685 typval_T *argvars;
10686 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010687{
10688 buf_T *buf;
10689 buf_T *save_curbuf;
10690 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010691 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010693 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10694 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010695 ++emsg_off;
10696 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010697
10698 rettv->v_type = VAR_STRING;
10699 rettv->vval.v_string = NULL;
10700
10701 if (buf != NULL && varname != NULL)
10702 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010703 /* set curbuf to be our buf, temporarily */
10704 save_curbuf = curbuf;
10705 curbuf = buf;
10706
Bram Moolenaar0d660222005-01-07 21:51:51 +000010707 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010708 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010709 else
10710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010711 if (*varname == NUL)
10712 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10713 * scope prefix before the NUL byte is required by
10714 * find_var_in_ht(). */
10715 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010716 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010717 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010718 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010719 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010720 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010721
10722 /* restore previous notion of curbuf */
10723 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010724 }
10725
10726 --emsg_off;
10727}
10728
10729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 * "getchar()" function
10731 */
10732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010733f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010734 typval_T *argvars;
10735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736{
10737 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010738 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010740 /* Position the cursor. Needed after a message that ends in a space. */
10741 windgoto(msg_row, msg_col);
10742
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 ++no_mapping;
10744 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010745 for (;;)
10746 {
10747 if (argvars[0].v_type == VAR_UNKNOWN)
10748 /* getchar(): blocking wait. */
10749 n = safe_vgetc();
10750 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10751 /* getchar(1): only check if char avail */
10752 n = vpeekc();
10753 else if (error || vpeekc() == NUL)
10754 /* illegal argument or getchar(0) and no char avail: return zero */
10755 n = 0;
10756 else
10757 /* getchar(0) and char avail: return char */
10758 n = safe_vgetc();
10759 if (n == K_IGNORE)
10760 continue;
10761 break;
10762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 --no_mapping;
10764 --allow_keys;
10765
Bram Moolenaar219b8702006-11-01 14:32:36 +000010766 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10767 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10768 vimvars[VV_MOUSE_COL].vv_nr = 0;
10769
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010770 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 if (IS_SPECIAL(n) || mod_mask != 0)
10772 {
10773 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10774 int i = 0;
10775
10776 /* Turn a special key into three bytes, plus modifier. */
10777 if (mod_mask != 0)
10778 {
10779 temp[i++] = K_SPECIAL;
10780 temp[i++] = KS_MODIFIER;
10781 temp[i++] = mod_mask;
10782 }
10783 if (IS_SPECIAL(n))
10784 {
10785 temp[i++] = K_SPECIAL;
10786 temp[i++] = K_SECOND(n);
10787 temp[i++] = K_THIRD(n);
10788 }
10789#ifdef FEAT_MBYTE
10790 else if (has_mbyte)
10791 i += (*mb_char2bytes)(n, temp + i);
10792#endif
10793 else
10794 temp[i++] = n;
10795 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010796 rettv->v_type = VAR_STRING;
10797 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010798
10799#ifdef FEAT_MOUSE
10800 if (n == K_LEFTMOUSE
10801 || n == K_LEFTMOUSE_NM
10802 || n == K_LEFTDRAG
10803 || n == K_LEFTRELEASE
10804 || n == K_LEFTRELEASE_NM
10805 || n == K_MIDDLEMOUSE
10806 || n == K_MIDDLEDRAG
10807 || n == K_MIDDLERELEASE
10808 || n == K_RIGHTMOUSE
10809 || n == K_RIGHTDRAG
10810 || n == K_RIGHTRELEASE
10811 || n == K_X1MOUSE
10812 || n == K_X1DRAG
10813 || n == K_X1RELEASE
10814 || n == K_X2MOUSE
10815 || n == K_X2DRAG
10816 || n == K_X2RELEASE
10817 || n == K_MOUSEDOWN
10818 || n == K_MOUSEUP)
10819 {
10820 int row = mouse_row;
10821 int col = mouse_col;
10822 win_T *win;
10823 linenr_T lnum;
10824# ifdef FEAT_WINDOWS
10825 win_T *wp;
10826# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010827 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010828
10829 if (row >= 0 && col >= 0)
10830 {
10831 /* Find the window at the mouse coordinates and compute the
10832 * text position. */
10833 win = mouse_find_win(&row, &col);
10834 (void)mouse_comp_pos(win, &row, &col, &lnum);
10835# ifdef FEAT_WINDOWS
10836 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010837 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010838# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010839 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010840 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10841 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10842 }
10843 }
10844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 }
10846}
10847
10848/*
10849 * "getcharmod()" function
10850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010853 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010856 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857}
10858
10859/*
10860 * "getcmdline()" function
10861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010863f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010864 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010867 rettv->v_type = VAR_STRING;
10868 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869}
10870
10871/*
10872 * "getcmdpos()" function
10873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010875f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010876 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010879 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880}
10881
10882/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010883 * "getcmdtype()" function
10884 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010885 static void
10886f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010887 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010888 typval_T *rettv;
10889{
10890 rettv->v_type = VAR_STRING;
10891 rettv->vval.v_string = alloc(2);
10892 if (rettv->vval.v_string != NULL)
10893 {
10894 rettv->vval.v_string[0] = get_cmdline_type();
10895 rettv->vval.v_string[1] = NUL;
10896 }
10897}
10898
10899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 * "getcwd()" function
10901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010903f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010904 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906{
10907 char_u cwd[MAXPATHL];
10908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010909 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010911 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 else
10913 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010914 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010916 if (rettv->vval.v_string != NULL)
10917 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918#endif
10919 }
10920}
10921
10922/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010923 * "getfontname()" function
10924 */
10925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010927 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010928 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010929{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010930 rettv->v_type = VAR_STRING;
10931 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010932#ifdef FEAT_GUI
10933 if (gui.in_use)
10934 {
10935 GuiFont font;
10936 char_u *name = NULL;
10937
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010938 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010939 {
10940 /* Get the "Normal" font. Either the name saved by
10941 * hl_set_font_name() or from the font ID. */
10942 font = gui.norm_font;
10943 name = hl_get_font_name();
10944 }
10945 else
10946 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010947 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010948 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10949 return;
10950 font = gui_mch_get_font(name, FALSE);
10951 if (font == NOFONT)
10952 return; /* Invalid font name, return empty string. */
10953 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010955 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010956 gui_mch_free_font(font);
10957 }
10958#endif
10959}
10960
10961/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010962 * "getfperm({fname})" function
10963 */
10964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010965f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010966 typval_T *argvars;
10967 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010968{
10969 char_u *fname;
10970 struct stat st;
10971 char_u *perm = NULL;
10972 char_u flags[] = "rwx";
10973 int i;
10974
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010975 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010978 if (mch_stat((char *)fname, &st) >= 0)
10979 {
10980 perm = vim_strsave((char_u *)"---------");
10981 if (perm != NULL)
10982 {
10983 for (i = 0; i < 9; i++)
10984 {
10985 if (st.st_mode & (1 << (8 - i)))
10986 perm[i] = flags[i % 3];
10987 }
10988 }
10989 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010990 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010991}
10992
10993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 * "getfsize({fname})" function
10995 */
10996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010997f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010998 typval_T *argvars;
10999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000{
11001 char_u *fname;
11002 struct stat st;
11003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011006 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007
11008 if (mch_stat((char *)fname, &st) >= 0)
11009 {
11010 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011011 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011014 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011015
11016 /* non-perfect check for overflow */
11017 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11018 rettv->vval.v_number = -2;
11019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 }
11021 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023}
11024
11025/*
11026 * "getftime({fname})" function
11027 */
11028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011029f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011030 typval_T *argvars;
11031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032{
11033 char_u *fname;
11034 struct stat st;
11035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037
11038 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042}
11043
11044/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011045 * "getftype({fname})" function
11046 */
11047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011049 typval_T *argvars;
11050 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011051{
11052 char_u *fname;
11053 struct stat st;
11054 char_u *type = NULL;
11055 char *t;
11056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011057 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011060 if (mch_lstat((char *)fname, &st) >= 0)
11061 {
11062#ifdef S_ISREG
11063 if (S_ISREG(st.st_mode))
11064 t = "file";
11065 else if (S_ISDIR(st.st_mode))
11066 t = "dir";
11067# ifdef S_ISLNK
11068 else if (S_ISLNK(st.st_mode))
11069 t = "link";
11070# endif
11071# ifdef S_ISBLK
11072 else if (S_ISBLK(st.st_mode))
11073 t = "bdev";
11074# endif
11075# ifdef S_ISCHR
11076 else if (S_ISCHR(st.st_mode))
11077 t = "cdev";
11078# endif
11079# ifdef S_ISFIFO
11080 else if (S_ISFIFO(st.st_mode))
11081 t = "fifo";
11082# endif
11083# ifdef S_ISSOCK
11084 else if (S_ISSOCK(st.st_mode))
11085 t = "fifo";
11086# endif
11087 else
11088 t = "other";
11089#else
11090# ifdef S_IFMT
11091 switch (st.st_mode & S_IFMT)
11092 {
11093 case S_IFREG: t = "file"; break;
11094 case S_IFDIR: t = "dir"; break;
11095# ifdef S_IFLNK
11096 case S_IFLNK: t = "link"; break;
11097# endif
11098# ifdef S_IFBLK
11099 case S_IFBLK: t = "bdev"; break;
11100# endif
11101# ifdef S_IFCHR
11102 case S_IFCHR: t = "cdev"; break;
11103# endif
11104# ifdef S_IFIFO
11105 case S_IFIFO: t = "fifo"; break;
11106# endif
11107# ifdef S_IFSOCK
11108 case S_IFSOCK: t = "socket"; break;
11109# endif
11110 default: t = "other";
11111 }
11112# else
11113 if (mch_isdir(fname))
11114 t = "dir";
11115 else
11116 t = "file";
11117# endif
11118#endif
11119 type = vim_strsave((char_u *)t);
11120 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011121 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011122}
11123
11124/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011125 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011126 */
11127 static void
11128f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011129 typval_T *argvars;
11130 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011131{
11132 linenr_T lnum;
11133 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011134 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011135
11136 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011137 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011138 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011139 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011140 retlist = FALSE;
11141 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011142 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011143 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011144 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011145 retlist = TRUE;
11146 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011147
Bram Moolenaar342337a2005-07-21 21:11:17 +000011148 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011149}
11150
11151/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011152 * "getmatches()" function
11153 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011154 static void
11155f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011156 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011157 typval_T *rettv;
11158{
11159#ifdef FEAT_SEARCH_EXTRA
11160 dict_T *dict;
11161 matchitem_T *cur = curwin->w_match_head;
11162
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011163 if (rettv_list_alloc(rettv) == OK)
11164 {
11165 while (cur != NULL)
11166 {
11167 dict = dict_alloc();
11168 if (dict == NULL)
11169 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011170 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11171 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11172 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11173 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11174 list_append_dict(rettv->vval.v_list, dict);
11175 cur = cur->next;
11176 }
11177 }
11178#endif
11179}
11180
11181/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011182 * "getpid()" function
11183 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011184 static void
11185f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011186 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011187 typval_T *rettv;
11188{
11189 rettv->vval.v_number = mch_get_pid();
11190}
11191
11192/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011193 * "getpos(string)" function
11194 */
11195 static void
11196f_getpos(argvars, rettv)
11197 typval_T *argvars;
11198 typval_T *rettv;
11199{
11200 pos_T *fp;
11201 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011202 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011203
11204 if (rettv_list_alloc(rettv) == OK)
11205 {
11206 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011207 fp = var2fpos(&argvars[0], TRUE, &fnum);
11208 if (fnum != -1)
11209 list_append_number(l, (varnumber_T)fnum);
11210 else
11211 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011212 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11213 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011214 list_append_number(l, (fp != NULL)
11215 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011216 : (varnumber_T)0);
11217 list_append_number(l,
11218#ifdef FEAT_VIRTUALEDIT
11219 (fp != NULL) ? (varnumber_T)fp->coladd :
11220#endif
11221 (varnumber_T)0);
11222 }
11223 else
11224 rettv->vval.v_number = FALSE;
11225}
11226
11227/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011228 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011229 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011230 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011231f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011232 typval_T *argvars UNUSED;
11233 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011234{
11235#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011236 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011237#endif
11238
Bram Moolenaar2641f772005-03-25 21:58:17 +000011239#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011240 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011241 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011242 wp = NULL;
11243 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11244 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011245 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011246 if (wp == NULL)
11247 return;
11248 }
11249
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011250 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011251 }
11252#endif
11253}
11254
11255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 * "getreg()" function
11257 */
11258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011260 typval_T *argvars;
11261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262{
11263 char_u *strregname;
11264 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011265 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011266 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011268 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011270 strregname = get_tv_string_chk(&argvars[0]);
11271 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011272 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011273 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011276 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 regname = (strregname == NULL ? '"' : *strregname);
11278 if (regname == 0)
11279 regname = '"';
11280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011282 rettv->vval.v_string = error ? NULL :
11283 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284}
11285
11286/*
11287 * "getregtype()" function
11288 */
11289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011290f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011291 typval_T *argvars;
11292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293{
11294 char_u *strregname;
11295 int regname;
11296 char_u buf[NUMBUFLEN + 2];
11297 long reglen = 0;
11298
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011299 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011300 {
11301 strregname = get_tv_string_chk(&argvars[0]);
11302 if (strregname == NULL) /* type error; errmsg already given */
11303 {
11304 rettv->v_type = VAR_STRING;
11305 rettv->vval.v_string = NULL;
11306 return;
11307 }
11308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309 else
11310 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011311 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312
11313 regname = (strregname == NULL ? '"' : *strregname);
11314 if (regname == 0)
11315 regname = '"';
11316
11317 buf[0] = NUL;
11318 buf[1] = NUL;
11319 switch (get_reg_type(regname, &reglen))
11320 {
11321 case MLINE: buf[0] = 'V'; break;
11322 case MCHAR: buf[0] = 'v'; break;
11323#ifdef FEAT_VISUAL
11324 case MBLOCK:
11325 buf[0] = Ctrl_V;
11326 sprintf((char *)buf + 1, "%ld", reglen + 1);
11327 break;
11328#endif
11329 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330 rettv->v_type = VAR_STRING;
11331 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332}
11333
11334/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011335 * "gettabvar()" function
11336 */
11337 static void
11338f_gettabvar(argvars, rettv)
11339 typval_T *argvars;
11340 typval_T *rettv;
11341{
11342 tabpage_T *tp;
11343 dictitem_T *v;
11344 char_u *varname;
11345
11346 rettv->v_type = VAR_STRING;
11347 rettv->vval.v_string = NULL;
11348
11349 varname = get_tv_string_chk(&argvars[1]);
11350 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11351 if (tp != NULL && varname != NULL)
11352 {
11353 /* look up the variable */
11354 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11355 if (v != NULL)
11356 copy_tv(&v->di_tv, rettv);
11357 }
11358}
11359
11360/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011361 * "gettabwinvar()" function
11362 */
11363 static void
11364f_gettabwinvar(argvars, rettv)
11365 typval_T *argvars;
11366 typval_T *rettv;
11367{
11368 getwinvar(argvars, rettv, 1);
11369}
11370
11371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 * "getwinposx()" function
11373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011375f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011376 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011379 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380#ifdef FEAT_GUI
11381 if (gui.in_use)
11382 {
11383 int x, y;
11384
11385 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387 }
11388#endif
11389}
11390
11391/*
11392 * "getwinposy()" function
11393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011395f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011396 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011399 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400#ifdef FEAT_GUI
11401 if (gui.in_use)
11402 {
11403 int x, y;
11404
11405 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 }
11408#endif
11409}
11410
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011411/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011412 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011413 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011414 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011415find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011416 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011417 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011418{
11419#ifdef FEAT_WINDOWS
11420 win_T *wp;
11421#endif
11422 int nr;
11423
11424 nr = get_tv_number_chk(vp, NULL);
11425
11426#ifdef FEAT_WINDOWS
11427 if (nr < 0)
11428 return NULL;
11429 if (nr == 0)
11430 return curwin;
11431
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011432 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11433 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011434 if (--nr <= 0)
11435 break;
11436 return wp;
11437#else
11438 if (nr == 0 || nr == 1)
11439 return curwin;
11440 return NULL;
11441#endif
11442}
11443
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444/*
11445 * "getwinvar()" function
11446 */
11447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011449 typval_T *argvars;
11450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011452 getwinvar(argvars, rettv, 0);
11453}
11454
11455/*
11456 * getwinvar() and gettabwinvar()
11457 */
11458 static void
11459getwinvar(argvars, rettv, off)
11460 typval_T *argvars;
11461 typval_T *rettv;
11462 int off; /* 1 for gettabwinvar() */
11463{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464 win_T *win, *oldcurwin;
11465 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011466 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011467 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011469#ifdef FEAT_WINDOWS
11470 if (off == 1)
11471 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11472 else
11473 tp = curtab;
11474#endif
11475 win = find_win_by_nr(&argvars[off], tp);
11476 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011477 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011479 rettv->v_type = VAR_STRING;
11480 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481
11482 if (win != NULL && varname != NULL)
11483 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011484 /* Set curwin to be our win, temporarily. Also set curbuf, so
11485 * that we can get buffer-local options. */
11486 oldcurwin = curwin;
11487 curwin = win;
11488 curbuf = win->w_buffer;
11489
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 else
11493 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011494 if (*varname == NUL)
11495 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11496 * scope prefix before the NUL byte is required by
11497 * find_var_in_ht(). */
11498 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011500 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011504
11505 /* restore previous notion of curwin */
11506 curwin = oldcurwin;
11507 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508 }
11509
11510 --emsg_off;
11511}
11512
11513/*
11514 * "glob()" function
11515 */
11516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011517f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011518 typval_T *argvars;
11519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011521 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011523 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011525 /* When the optional second argument is non-zero, don't remove matches
11526 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11527 if (argvars[1].v_type != VAR_UNKNOWN
11528 && get_tv_number_chk(&argvars[1], &error))
11529 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011530 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011531 if (!error)
11532 {
11533 ExpandInit(&xpc);
11534 xpc.xp_context = EXPAND_FILES;
11535 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11536 NULL, flags, WILD_ALL);
11537 }
11538 else
11539 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540}
11541
11542/*
11543 * "globpath()" function
11544 */
11545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011546f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011547 typval_T *argvars;
11548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011550 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011552 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011553 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011555 /* When the optional second argument is non-zero, don't remove matches
11556 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11557 if (argvars[2].v_type != VAR_UNKNOWN
11558 && get_tv_number_chk(&argvars[2], &error))
11559 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011561 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011562 rettv->vval.v_string = NULL;
11563 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011564 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11565 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566}
11567
11568/*
11569 * "has()" function
11570 */
11571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011572f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011573 typval_T *argvars;
11574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575{
11576 int i;
11577 char_u *name;
11578 int n = FALSE;
11579 static char *(has_list[]) =
11580 {
11581#ifdef AMIGA
11582 "amiga",
11583# ifdef FEAT_ARP
11584 "arp",
11585# endif
11586#endif
11587#ifdef __BEOS__
11588 "beos",
11589#endif
11590#ifdef MSDOS
11591# ifdef DJGPP
11592 "dos32",
11593# else
11594 "dos16",
11595# endif
11596#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011597#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598 "mac",
11599#endif
11600#if defined(MACOS_X_UNIX)
11601 "macunix",
11602#endif
11603#ifdef OS2
11604 "os2",
11605#endif
11606#ifdef __QNX__
11607 "qnx",
11608#endif
11609#ifdef RISCOS
11610 "riscos",
11611#endif
11612#ifdef UNIX
11613 "unix",
11614#endif
11615#ifdef VMS
11616 "vms",
11617#endif
11618#ifdef WIN16
11619 "win16",
11620#endif
11621#ifdef WIN32
11622 "win32",
11623#endif
11624#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11625 "win32unix",
11626#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011627#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628 "win64",
11629#endif
11630#ifdef EBCDIC
11631 "ebcdic",
11632#endif
11633#ifndef CASE_INSENSITIVE_FILENAME
11634 "fname_case",
11635#endif
11636#ifdef FEAT_ARABIC
11637 "arabic",
11638#endif
11639#ifdef FEAT_AUTOCMD
11640 "autocmd",
11641#endif
11642#ifdef FEAT_BEVAL
11643 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011644# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11645 "balloon_multiline",
11646# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647#endif
11648#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11649 "builtin_terms",
11650# ifdef ALL_BUILTIN_TCAPS
11651 "all_builtin_terms",
11652# endif
11653#endif
11654#ifdef FEAT_BYTEOFF
11655 "byte_offset",
11656#endif
11657#ifdef FEAT_CINDENT
11658 "cindent",
11659#endif
11660#ifdef FEAT_CLIENTSERVER
11661 "clientserver",
11662#endif
11663#ifdef FEAT_CLIPBOARD
11664 "clipboard",
11665#endif
11666#ifdef FEAT_CMDL_COMPL
11667 "cmdline_compl",
11668#endif
11669#ifdef FEAT_CMDHIST
11670 "cmdline_hist",
11671#endif
11672#ifdef FEAT_COMMENTS
11673 "comments",
11674#endif
11675#ifdef FEAT_CRYPT
11676 "cryptv",
11677#endif
11678#ifdef FEAT_CSCOPE
11679 "cscope",
11680#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011681#ifdef CURSOR_SHAPE
11682 "cursorshape",
11683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684#ifdef DEBUG
11685 "debug",
11686#endif
11687#ifdef FEAT_CON_DIALOG
11688 "dialog_con",
11689#endif
11690#ifdef FEAT_GUI_DIALOG
11691 "dialog_gui",
11692#endif
11693#ifdef FEAT_DIFF
11694 "diff",
11695#endif
11696#ifdef FEAT_DIGRAPHS
11697 "digraphs",
11698#endif
11699#ifdef FEAT_DND
11700 "dnd",
11701#endif
11702#ifdef FEAT_EMACS_TAGS
11703 "emacs_tags",
11704#endif
11705 "eval", /* always present, of course! */
11706#ifdef FEAT_EX_EXTRA
11707 "ex_extra",
11708#endif
11709#ifdef FEAT_SEARCH_EXTRA
11710 "extra_search",
11711#endif
11712#ifdef FEAT_FKMAP
11713 "farsi",
11714#endif
11715#ifdef FEAT_SEARCHPATH
11716 "file_in_path",
11717#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011718#if defined(UNIX) && !defined(USE_SYSTEM)
11719 "filterpipe",
11720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721#ifdef FEAT_FIND_ID
11722 "find_in_path",
11723#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011724#ifdef FEAT_FLOAT
11725 "float",
11726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727#ifdef FEAT_FOLDING
11728 "folding",
11729#endif
11730#ifdef FEAT_FOOTER
11731 "footer",
11732#endif
11733#if !defined(USE_SYSTEM) && defined(UNIX)
11734 "fork",
11735#endif
11736#ifdef FEAT_GETTEXT
11737 "gettext",
11738#endif
11739#ifdef FEAT_GUI
11740 "gui",
11741#endif
11742#ifdef FEAT_GUI_ATHENA
11743# ifdef FEAT_GUI_NEXTAW
11744 "gui_neXtaw",
11745# else
11746 "gui_athena",
11747# endif
11748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749#ifdef FEAT_GUI_GTK
11750 "gui_gtk",
11751# ifdef HAVE_GTK2
11752 "gui_gtk2",
11753# endif
11754#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011755#ifdef FEAT_GUI_GNOME
11756 "gui_gnome",
11757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758#ifdef FEAT_GUI_MAC
11759 "gui_mac",
11760#endif
11761#ifdef FEAT_GUI_MOTIF
11762 "gui_motif",
11763#endif
11764#ifdef FEAT_GUI_PHOTON
11765 "gui_photon",
11766#endif
11767#ifdef FEAT_GUI_W16
11768 "gui_win16",
11769#endif
11770#ifdef FEAT_GUI_W32
11771 "gui_win32",
11772#endif
11773#ifdef FEAT_HANGULIN
11774 "hangul_input",
11775#endif
11776#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11777 "iconv",
11778#endif
11779#ifdef FEAT_INS_EXPAND
11780 "insert_expand",
11781#endif
11782#ifdef FEAT_JUMPLIST
11783 "jumplist",
11784#endif
11785#ifdef FEAT_KEYMAP
11786 "keymap",
11787#endif
11788#ifdef FEAT_LANGMAP
11789 "langmap",
11790#endif
11791#ifdef FEAT_LIBCALL
11792 "libcall",
11793#endif
11794#ifdef FEAT_LINEBREAK
11795 "linebreak",
11796#endif
11797#ifdef FEAT_LISP
11798 "lispindent",
11799#endif
11800#ifdef FEAT_LISTCMDS
11801 "listcmds",
11802#endif
11803#ifdef FEAT_LOCALMAP
11804 "localmap",
11805#endif
11806#ifdef FEAT_MENU
11807 "menu",
11808#endif
11809#ifdef FEAT_SESSION
11810 "mksession",
11811#endif
11812#ifdef FEAT_MODIFY_FNAME
11813 "modify_fname",
11814#endif
11815#ifdef FEAT_MOUSE
11816 "mouse",
11817#endif
11818#ifdef FEAT_MOUSESHAPE
11819 "mouseshape",
11820#endif
11821#if defined(UNIX) || defined(VMS)
11822# ifdef FEAT_MOUSE_DEC
11823 "mouse_dec",
11824# endif
11825# ifdef FEAT_MOUSE_GPM
11826 "mouse_gpm",
11827# endif
11828# ifdef FEAT_MOUSE_JSB
11829 "mouse_jsbterm",
11830# endif
11831# ifdef FEAT_MOUSE_NET
11832 "mouse_netterm",
11833# endif
11834# ifdef FEAT_MOUSE_PTERM
11835 "mouse_pterm",
11836# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011837# ifdef FEAT_SYSMOUSE
11838 "mouse_sysmouse",
11839# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840# ifdef FEAT_MOUSE_XTERM
11841 "mouse_xterm",
11842# endif
11843#endif
11844#ifdef FEAT_MBYTE
11845 "multi_byte",
11846#endif
11847#ifdef FEAT_MBYTE_IME
11848 "multi_byte_ime",
11849#endif
11850#ifdef FEAT_MULTI_LANG
11851 "multi_lang",
11852#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011853#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011854#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011855 "mzscheme",
11856#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858#ifdef FEAT_OLE
11859 "ole",
11860#endif
11861#ifdef FEAT_OSFILETYPE
11862 "osfiletype",
11863#endif
11864#ifdef FEAT_PATH_EXTRA
11865 "path_extra",
11866#endif
11867#ifdef FEAT_PERL
11868#ifndef DYNAMIC_PERL
11869 "perl",
11870#endif
11871#endif
11872#ifdef FEAT_PYTHON
11873#ifndef DYNAMIC_PYTHON
11874 "python",
11875#endif
11876#endif
11877#ifdef FEAT_POSTSCRIPT
11878 "postscript",
11879#endif
11880#ifdef FEAT_PRINTER
11881 "printer",
11882#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011883#ifdef FEAT_PROFILE
11884 "profile",
11885#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011886#ifdef FEAT_RELTIME
11887 "reltime",
11888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889#ifdef FEAT_QUICKFIX
11890 "quickfix",
11891#endif
11892#ifdef FEAT_RIGHTLEFT
11893 "rightleft",
11894#endif
11895#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11896 "ruby",
11897#endif
11898#ifdef FEAT_SCROLLBIND
11899 "scrollbind",
11900#endif
11901#ifdef FEAT_CMDL_INFO
11902 "showcmd",
11903 "cmdline_info",
11904#endif
11905#ifdef FEAT_SIGNS
11906 "signs",
11907#endif
11908#ifdef FEAT_SMARTINDENT
11909 "smartindent",
11910#endif
11911#ifdef FEAT_SNIFF
11912 "sniff",
11913#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000011914#ifdef STARTUPTIME
11915 "startuptime",
11916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917#ifdef FEAT_STL_OPT
11918 "statusline",
11919#endif
11920#ifdef FEAT_SUN_WORKSHOP
11921 "sun_workshop",
11922#endif
11923#ifdef FEAT_NETBEANS_INTG
11924 "netbeans_intg",
11925#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011926#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011927 "spell",
11928#endif
11929#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930 "syntax",
11931#endif
11932#if defined(USE_SYSTEM) || !defined(UNIX)
11933 "system",
11934#endif
11935#ifdef FEAT_TAG_BINS
11936 "tag_binary",
11937#endif
11938#ifdef FEAT_TAG_OLDSTATIC
11939 "tag_old_static",
11940#endif
11941#ifdef FEAT_TAG_ANYWHITE
11942 "tag_any_white",
11943#endif
11944#ifdef FEAT_TCL
11945# ifndef DYNAMIC_TCL
11946 "tcl",
11947# endif
11948#endif
11949#ifdef TERMINFO
11950 "terminfo",
11951#endif
11952#ifdef FEAT_TERMRESPONSE
11953 "termresponse",
11954#endif
11955#ifdef FEAT_TEXTOBJ
11956 "textobjects",
11957#endif
11958#ifdef HAVE_TGETENT
11959 "tgetent",
11960#endif
11961#ifdef FEAT_TITLE
11962 "title",
11963#endif
11964#ifdef FEAT_TOOLBAR
11965 "toolbar",
11966#endif
11967#ifdef FEAT_USR_CMDS
11968 "user-commands", /* was accidentally included in 5.4 */
11969 "user_commands",
11970#endif
11971#ifdef FEAT_VIMINFO
11972 "viminfo",
11973#endif
11974#ifdef FEAT_VERTSPLIT
11975 "vertsplit",
11976#endif
11977#ifdef FEAT_VIRTUALEDIT
11978 "virtualedit",
11979#endif
11980#ifdef FEAT_VISUAL
11981 "visual",
11982#endif
11983#ifdef FEAT_VISUALEXTRA
11984 "visualextra",
11985#endif
11986#ifdef FEAT_VREPLACE
11987 "vreplace",
11988#endif
11989#ifdef FEAT_WILDIGN
11990 "wildignore",
11991#endif
11992#ifdef FEAT_WILDMENU
11993 "wildmenu",
11994#endif
11995#ifdef FEAT_WINDOWS
11996 "windows",
11997#endif
11998#ifdef FEAT_WAK
11999 "winaltkeys",
12000#endif
12001#ifdef FEAT_WRITEBACKUP
12002 "writebackup",
12003#endif
12004#ifdef FEAT_XIM
12005 "xim",
12006#endif
12007#ifdef FEAT_XFONTSET
12008 "xfontset",
12009#endif
12010#ifdef USE_XSMP
12011 "xsmp",
12012#endif
12013#ifdef USE_XSMP_INTERACT
12014 "xsmp_interact",
12015#endif
12016#ifdef FEAT_XCLIPBOARD
12017 "xterm_clipboard",
12018#endif
12019#ifdef FEAT_XTERM_SAVE
12020 "xterm_save",
12021#endif
12022#if defined(UNIX) && defined(FEAT_X11)
12023 "X11",
12024#endif
12025 NULL
12026 };
12027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 for (i = 0; has_list[i] != NULL; ++i)
12030 if (STRICMP(name, has_list[i]) == 0)
12031 {
12032 n = TRUE;
12033 break;
12034 }
12035
12036 if (n == FALSE)
12037 {
12038 if (STRNICMP(name, "patch", 5) == 0)
12039 n = has_patch(atoi((char *)name + 5));
12040 else if (STRICMP(name, "vim_starting") == 0)
12041 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012042#ifdef FEAT_MBYTE
12043 else if (STRICMP(name, "multi_byte_encoding") == 0)
12044 n = has_mbyte;
12045#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012046#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12047 else if (STRICMP(name, "balloon_multiline") == 0)
12048 n = multiline_balloon_available();
12049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050#ifdef DYNAMIC_TCL
12051 else if (STRICMP(name, "tcl") == 0)
12052 n = tcl_enabled(FALSE);
12053#endif
12054#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12055 else if (STRICMP(name, "iconv") == 0)
12056 n = iconv_enabled(FALSE);
12057#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012058#ifdef DYNAMIC_MZSCHEME
12059 else if (STRICMP(name, "mzscheme") == 0)
12060 n = mzscheme_enabled(FALSE);
12061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062#ifdef DYNAMIC_RUBY
12063 else if (STRICMP(name, "ruby") == 0)
12064 n = ruby_enabled(FALSE);
12065#endif
12066#ifdef DYNAMIC_PYTHON
12067 else if (STRICMP(name, "python") == 0)
12068 n = python_enabled(FALSE);
12069#endif
12070#ifdef DYNAMIC_PERL
12071 else if (STRICMP(name, "perl") == 0)
12072 n = perl_enabled(FALSE);
12073#endif
12074#ifdef FEAT_GUI
12075 else if (STRICMP(name, "gui_running") == 0)
12076 n = (gui.in_use || gui.starting);
12077# ifdef FEAT_GUI_W32
12078 else if (STRICMP(name, "gui_win32s") == 0)
12079 n = gui_is_win32s();
12080# endif
12081# ifdef FEAT_BROWSE
12082 else if (STRICMP(name, "browse") == 0)
12083 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12084# endif
12085#endif
12086#ifdef FEAT_SYN_HL
12087 else if (STRICMP(name, "syntax_items") == 0)
12088 n = syntax_present(curbuf);
12089#endif
12090#if defined(WIN3264)
12091 else if (STRICMP(name, "win95") == 0)
12092 n = mch_windows95();
12093#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012094#ifdef FEAT_NETBEANS_INTG
12095 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012096 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098 }
12099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012100 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101}
12102
12103/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012104 * "has_key()" function
12105 */
12106 static void
12107f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012108 typval_T *argvars;
12109 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012110{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012111 if (argvars[0].v_type != VAR_DICT)
12112 {
12113 EMSG(_(e_dictreq));
12114 return;
12115 }
12116 if (argvars[0].vval.v_dict == NULL)
12117 return;
12118
12119 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012120 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012121}
12122
12123/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012124 * "haslocaldir()" function
12125 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012126 static void
12127f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012128 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012129 typval_T *rettv;
12130{
12131 rettv->vval.v_number = (curwin->w_localdir != NULL);
12132}
12133
12134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135 * "hasmapto()" function
12136 */
12137 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012138f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012139 typval_T *argvars;
12140 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141{
12142 char_u *name;
12143 char_u *mode;
12144 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012145 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012147 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012148 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149 mode = (char_u *)"nvo";
12150 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012152 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012153 if (argvars[2].v_type != VAR_UNKNOWN)
12154 abbr = get_tv_number(&argvars[2]);
12155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156
Bram Moolenaar2c932302006-03-18 21:42:09 +000012157 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012158 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161}
12162
12163/*
12164 * "histadd()" function
12165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012167f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012168 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170{
12171#ifdef FEAT_CMDHIST
12172 int histype;
12173 char_u *str;
12174 char_u buf[NUMBUFLEN];
12175#endif
12176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012177 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178 if (check_restricted() || check_secure())
12179 return;
12180#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012181 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12182 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183 if (histype >= 0)
12184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012185 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186 if (*str != NUL)
12187 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012188 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012190 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191 return;
12192 }
12193 }
12194#endif
12195}
12196
12197/*
12198 * "histdel()" function
12199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012201f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012202 typval_T *argvars UNUSED;
12203 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204{
12205#ifdef FEAT_CMDHIST
12206 int n;
12207 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012208 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012210 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12211 if (str == NULL)
12212 n = 0;
12213 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012215 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012216 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012217 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012218 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012219 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220 else
12221 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012222 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012223 get_tv_string_buf(&argvars[1], buf));
12224 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225#endif
12226}
12227
12228/*
12229 * "histget()" function
12230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012232f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012233 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235{
12236#ifdef FEAT_CMDHIST
12237 int type;
12238 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012239 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012241 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12242 if (str == NULL)
12243 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012245 {
12246 type = get_histtype(str);
12247 if (argvars[1].v_type == VAR_UNKNOWN)
12248 idx = get_history_idx(type);
12249 else
12250 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12251 /* -1 on type error */
12252 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012255 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012257 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258}
12259
12260/*
12261 * "histnr()" function
12262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012264f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012265 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267{
12268 int i;
12269
12270#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012271 char_u *history = get_tv_string_chk(&argvars[0]);
12272
12273 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274 if (i >= HIST_CMD && i < HIST_COUNT)
12275 i = get_history_idx(i);
12276 else
12277#endif
12278 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012279 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280}
12281
12282/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283 * "highlightID(name)" function
12284 */
12285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012286f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012287 typval_T *argvars;
12288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012290 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291}
12292
12293/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012294 * "highlight_exists()" function
12295 */
12296 static void
12297f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012298 typval_T *argvars;
12299 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012300{
12301 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12302}
12303
12304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305 * "hostname()" function
12306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012308f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012309 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311{
12312 char_u hostname[256];
12313
12314 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012315 rettv->v_type = VAR_STRING;
12316 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317}
12318
12319/*
12320 * iconv() function
12321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012323f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012324 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326{
12327#ifdef FEAT_MBYTE
12328 char_u buf1[NUMBUFLEN];
12329 char_u buf2[NUMBUFLEN];
12330 char_u *from, *to, *str;
12331 vimconv_T vimconv;
12332#endif
12333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012334 rettv->v_type = VAR_STRING;
12335 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336
12337#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338 str = get_tv_string(&argvars[0]);
12339 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12340 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341 vimconv.vc_type = CONV_NONE;
12342 convert_setup(&vimconv, from, to);
12343
12344 /* If the encodings are equal, no conversion needed. */
12345 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012346 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012348 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349
12350 convert_setup(&vimconv, NULL, NULL);
12351 vim_free(from);
12352 vim_free(to);
12353#endif
12354}
12355
12356/*
12357 * "indent()" function
12358 */
12359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012360f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012361 typval_T *argvars;
12362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363{
12364 linenr_T lnum;
12365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012366 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012368 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012370 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371}
12372
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012373/*
12374 * "index()" function
12375 */
12376 static void
12377f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012378 typval_T *argvars;
12379 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012380{
Bram Moolenaar33570922005-01-25 22:26:29 +000012381 list_T *l;
12382 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012383 long idx = 0;
12384 int ic = FALSE;
12385
12386 rettv->vval.v_number = -1;
12387 if (argvars[0].v_type != VAR_LIST)
12388 {
12389 EMSG(_(e_listreq));
12390 return;
12391 }
12392 l = argvars[0].vval.v_list;
12393 if (l != NULL)
12394 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012395 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012396 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012398 int error = FALSE;
12399
Bram Moolenaar758711c2005-02-02 23:11:38 +000012400 /* Start at specified item. Use the cached index that list_find()
12401 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012402 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012403 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012404 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012405 ic = get_tv_number_chk(&argvars[3], &error);
12406 if (error)
12407 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012408 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012409
Bram Moolenaar758711c2005-02-02 23:11:38 +000012410 for ( ; item != NULL; item = item->li_next, ++idx)
12411 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012412 {
12413 rettv->vval.v_number = idx;
12414 break;
12415 }
12416 }
12417}
12418
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419static int inputsecret_flag = 0;
12420
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012421static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12422
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012424 * This function is used by f_input() and f_inputdialog() functions. The third
12425 * argument to f_input() specifies the type of completion to use at the
12426 * prompt. The third argument to f_inputdialog() specifies the value to return
12427 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428 */
12429 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012430get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012431 typval_T *argvars;
12432 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012433 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012435 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436 char_u *p = NULL;
12437 int c;
12438 char_u buf[NUMBUFLEN];
12439 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012440 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012441 int xp_type = EXPAND_NOTHING;
12442 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012444 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012445 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446
12447#ifdef NO_CONSOLE_INPUT
12448 /* While starting up, there is no place to enter text. */
12449 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451#endif
12452
12453 cmd_silent = FALSE; /* Want to see the prompt. */
12454 if (prompt != NULL)
12455 {
12456 /* Only the part of the message after the last NL is considered as
12457 * prompt for the command line */
12458 p = vim_strrchr(prompt, '\n');
12459 if (p == NULL)
12460 p = prompt;
12461 else
12462 {
12463 ++p;
12464 c = *p;
12465 *p = NUL;
12466 msg_start();
12467 msg_clr_eos();
12468 msg_puts_attr(prompt, echo_attr);
12469 msg_didout = FALSE;
12470 msg_starthere();
12471 *p = c;
12472 }
12473 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012474
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012475 if (argvars[1].v_type != VAR_UNKNOWN)
12476 {
12477 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12478 if (defstr != NULL)
12479 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012481 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012482 {
12483 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012484 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012485 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012486
Bram Moolenaar4463f292005-09-25 22:20:24 +000012487 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012488
Bram Moolenaar4463f292005-09-25 22:20:24 +000012489 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12490 if (xp_name == NULL)
12491 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012492
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012493 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012494
Bram Moolenaar4463f292005-09-25 22:20:24 +000012495 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12496 &xp_arg) == FAIL)
12497 return;
12498 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012499 }
12500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012501 if (defstr != NULL)
12502 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012503 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12504 xp_type, xp_arg);
12505
12506 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012508 /* since the user typed this, no need to wait for return */
12509 need_wait_return = FALSE;
12510 msg_didout = FALSE;
12511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512 cmd_silent = cmd_silent_save;
12513}
12514
12515/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012516 * "input()" function
12517 * Also handles inputsecret() when inputsecret is set.
12518 */
12519 static void
12520f_input(argvars, rettv)
12521 typval_T *argvars;
12522 typval_T *rettv;
12523{
12524 get_user_input(argvars, rettv, FALSE);
12525}
12526
12527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528 * "inputdialog()" function
12529 */
12530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012531f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012532 typval_T *argvars;
12533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534{
12535#if defined(FEAT_GUI_TEXTDIALOG)
12536 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12537 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12538 {
12539 char_u *message;
12540 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012541 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012543 message = get_tv_string_chk(&argvars[0]);
12544 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012545 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012546 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012547 else
12548 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012549 if (message != NULL && defstr != NULL
12550 && do_dialog(VIM_QUESTION, NULL, message,
12551 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012552 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553 else
12554 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012555 if (message != NULL && defstr != NULL
12556 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012557 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012558 rettv->vval.v_string = vim_strsave(
12559 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012561 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012563 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564 }
12565 else
12566#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012567 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012568}
12569
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012570/*
12571 * "inputlist()" function
12572 */
12573 static void
12574f_inputlist(argvars, rettv)
12575 typval_T *argvars;
12576 typval_T *rettv;
12577{
12578 listitem_T *li;
12579 int selected;
12580 int mouse_used;
12581
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012582#ifdef NO_CONSOLE_INPUT
12583 /* While starting up, there is no place to enter text. */
12584 if (no_console_input())
12585 return;
12586#endif
12587 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12588 {
12589 EMSG2(_(e_listarg), "inputlist()");
12590 return;
12591 }
12592
12593 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012594 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012595 lines_left = Rows; /* avoid more prompt */
12596 msg_scroll = TRUE;
12597 msg_clr_eos();
12598
12599 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12600 {
12601 msg_puts(get_tv_string(&li->li_tv));
12602 msg_putchar('\n');
12603 }
12604
12605 /* Ask for choice. */
12606 selected = prompt_for_number(&mouse_used);
12607 if (mouse_used)
12608 selected -= lines_left;
12609
12610 rettv->vval.v_number = selected;
12611}
12612
12613
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12615
12616/*
12617 * "inputrestore()" function
12618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012620f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012621 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012623{
12624 if (ga_userinput.ga_len > 0)
12625 {
12626 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012627 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12628 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012629 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630 }
12631 else if (p_verbose > 1)
12632 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012633 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012634 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635 }
12636}
12637
12638/*
12639 * "inputsave()" function
12640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012642f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012643 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012646 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647 if (ga_grow(&ga_userinput, 1) == OK)
12648 {
12649 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12650 + ga_userinput.ga_len);
12651 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012652 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653 }
12654 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012655 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656}
12657
12658/*
12659 * "inputsecret()" function
12660 */
12661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012662f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012663 typval_T *argvars;
12664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665{
12666 ++cmdline_star;
12667 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012668 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669 --cmdline_star;
12670 --inputsecret_flag;
12671}
12672
12673/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012674 * "insert()" function
12675 */
12676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012677f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012678 typval_T *argvars;
12679 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012680{
12681 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012682 listitem_T *item;
12683 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012684 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012685
12686 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012687 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012688 else if ((l = argvars[0].vval.v_list) != NULL
12689 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012690 {
12691 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012692 before = get_tv_number_chk(&argvars[2], &error);
12693 if (error)
12694 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012695
Bram Moolenaar758711c2005-02-02 23:11:38 +000012696 if (before == l->lv_len)
12697 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012698 else
12699 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012700 item = list_find(l, before);
12701 if (item == NULL)
12702 {
12703 EMSGN(_(e_listidx), before);
12704 l = NULL;
12705 }
12706 }
12707 if (l != NULL)
12708 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012709 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012710 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012711 }
12712 }
12713}
12714
12715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716 * "isdirectory()" function
12717 */
12718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012719f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012720 typval_T *argvars;
12721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012723 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724}
12725
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012726/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012727 * "islocked()" function
12728 */
12729 static void
12730f_islocked(argvars, rettv)
12731 typval_T *argvars;
12732 typval_T *rettv;
12733{
12734 lval_T lv;
12735 char_u *end;
12736 dictitem_T *di;
12737
12738 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012739 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12740 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012741 if (end != NULL && lv.ll_name != NULL)
12742 {
12743 if (*end != NUL)
12744 EMSG(_(e_trailing));
12745 else
12746 {
12747 if (lv.ll_tv == NULL)
12748 {
12749 if (check_changedtick(lv.ll_name))
12750 rettv->vval.v_number = 1; /* always locked */
12751 else
12752 {
12753 di = find_var(lv.ll_name, NULL);
12754 if (di != NULL)
12755 {
12756 /* Consider a variable locked when:
12757 * 1. the variable itself is locked
12758 * 2. the value of the variable is locked.
12759 * 3. the List or Dict value is locked.
12760 */
12761 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12762 || tv_islocked(&di->di_tv));
12763 }
12764 }
12765 }
12766 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012767 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012768 else if (lv.ll_newkey != NULL)
12769 EMSG2(_(e_dictkey), lv.ll_newkey);
12770 else if (lv.ll_list != NULL)
12771 /* List item. */
12772 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12773 else
12774 /* Dictionary item. */
12775 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12776 }
12777 }
12778
12779 clear_lval(&lv);
12780}
12781
Bram Moolenaar33570922005-01-25 22:26:29 +000012782static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012783
12784/*
12785 * Turn a dict into a list:
12786 * "what" == 0: list of keys
12787 * "what" == 1: list of values
12788 * "what" == 2: list of items
12789 */
12790 static void
12791dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012792 typval_T *argvars;
12793 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012794 int what;
12795{
Bram Moolenaar33570922005-01-25 22:26:29 +000012796 list_T *l2;
12797 dictitem_T *di;
12798 hashitem_T *hi;
12799 listitem_T *li;
12800 listitem_T *li2;
12801 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012802 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012803
Bram Moolenaar8c711452005-01-14 21:53:12 +000012804 if (argvars[0].v_type != VAR_DICT)
12805 {
12806 EMSG(_(e_dictreq));
12807 return;
12808 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012809 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012810 return;
12811
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012812 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012813 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012814
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012815 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012816 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012817 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012818 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012819 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012820 --todo;
12821 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012822
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012823 li = listitem_alloc();
12824 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012825 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012826 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012827
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012828 if (what == 0)
12829 {
12830 /* keys() */
12831 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012832 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012833 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12834 }
12835 else if (what == 1)
12836 {
12837 /* values() */
12838 copy_tv(&di->di_tv, &li->li_tv);
12839 }
12840 else
12841 {
12842 /* items() */
12843 l2 = list_alloc();
12844 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012845 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012846 li->li_tv.vval.v_list = l2;
12847 if (l2 == NULL)
12848 break;
12849 ++l2->lv_refcount;
12850
12851 li2 = listitem_alloc();
12852 if (li2 == NULL)
12853 break;
12854 list_append(l2, li2);
12855 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012856 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012857 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12858
12859 li2 = listitem_alloc();
12860 if (li2 == NULL)
12861 break;
12862 list_append(l2, li2);
12863 copy_tv(&di->di_tv, &li2->li_tv);
12864 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012865 }
12866 }
12867}
12868
12869/*
12870 * "items(dict)" function
12871 */
12872 static void
12873f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012874 typval_T *argvars;
12875 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012876{
12877 dict_list(argvars, rettv, 2);
12878}
12879
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012881 * "join()" function
12882 */
12883 static void
12884f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012885 typval_T *argvars;
12886 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012887{
12888 garray_T ga;
12889 char_u *sep;
12890
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012891 if (argvars[0].v_type != VAR_LIST)
12892 {
12893 EMSG(_(e_listreq));
12894 return;
12895 }
12896 if (argvars[0].vval.v_list == NULL)
12897 return;
12898 if (argvars[1].v_type == VAR_UNKNOWN)
12899 sep = (char_u *)" ";
12900 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012901 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012902
12903 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012904
12905 if (sep != NULL)
12906 {
12907 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012908 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012909 ga_append(&ga, NUL);
12910 rettv->vval.v_string = (char_u *)ga.ga_data;
12911 }
12912 else
12913 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012914}
12915
12916/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012917 * "keys()" function
12918 */
12919 static void
12920f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012921 typval_T *argvars;
12922 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012923{
12924 dict_list(argvars, rettv, 0);
12925}
12926
12927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928 * "last_buffer_nr()" function.
12929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012931f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012932 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934{
12935 int n = 0;
12936 buf_T *buf;
12937
12938 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12939 if (n < buf->b_fnum)
12940 n = buf->b_fnum;
12941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012942 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012943}
12944
12945/*
12946 * "len()" function
12947 */
12948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012949f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012950 typval_T *argvars;
12951 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012952{
12953 switch (argvars[0].v_type)
12954 {
12955 case VAR_STRING:
12956 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012957 rettv->vval.v_number = (varnumber_T)STRLEN(
12958 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012959 break;
12960 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012961 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012962 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012963 case VAR_DICT:
12964 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12965 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012966 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012967 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012968 break;
12969 }
12970}
12971
Bram Moolenaar33570922005-01-25 22:26:29 +000012972static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012973
12974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012975libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012976 typval_T *argvars;
12977 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012978 int type;
12979{
12980#ifdef FEAT_LIBCALL
12981 char_u *string_in;
12982 char_u **string_result;
12983 int nr_result;
12984#endif
12985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012986 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012987 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012988 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012989
12990 if (check_restricted() || check_secure())
12991 return;
12992
12993#ifdef FEAT_LIBCALL
12994 /* The first two args must be strings, otherwise its meaningless */
12995 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12996 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012997 string_in = NULL;
12998 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012999 string_in = argvars[2].vval.v_string;
13000 if (type == VAR_NUMBER)
13001 string_result = NULL;
13002 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013003 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013004 if (mch_libcall(argvars[0].vval.v_string,
13005 argvars[1].vval.v_string,
13006 string_in,
13007 argvars[2].vval.v_number,
13008 string_result,
13009 &nr_result) == OK
13010 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013011 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013012 }
13013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014}
13015
13016/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013017 * "libcall()" function
13018 */
13019 static void
13020f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013021 typval_T *argvars;
13022 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013023{
13024 libcall_common(argvars, rettv, VAR_STRING);
13025}
13026
13027/*
13028 * "libcallnr()" function
13029 */
13030 static void
13031f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013032 typval_T *argvars;
13033 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013034{
13035 libcall_common(argvars, rettv, VAR_NUMBER);
13036}
13037
13038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039 * "line(string)" function
13040 */
13041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013042f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013043 typval_T *argvars;
13044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045{
13046 linenr_T lnum = 0;
13047 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013048 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013050 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051 if (fp != NULL)
13052 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013053 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054}
13055
13056/*
13057 * "line2byte(lnum)" function
13058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013060f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013061 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063{
13064#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013065 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066#else
13067 linenr_T lnum;
13068
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013069 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013071 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013072 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013073 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13074 if (rettv->vval.v_number >= 0)
13075 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076#endif
13077}
13078
13079/*
13080 * "lispindent(lnum)" function
13081 */
13082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013083f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013084 typval_T *argvars;
13085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086{
13087#ifdef FEAT_LISP
13088 pos_T pos;
13089 linenr_T lnum;
13090
13091 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013092 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13094 {
13095 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013096 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013097 curwin->w_cursor = pos;
13098 }
13099 else
13100#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013101 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102}
13103
13104/*
13105 * "localtime()" function
13106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013108f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013109 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013112 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113}
13114
Bram Moolenaar33570922005-01-25 22:26:29 +000013115static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116
13117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013118get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013119 typval_T *argvars;
13120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121 int exact;
13122{
13123 char_u *keys;
13124 char_u *which;
13125 char_u buf[NUMBUFLEN];
13126 char_u *keys_buf = NULL;
13127 char_u *rhs;
13128 int mode;
13129 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013130 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131
13132 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013133 rettv->v_type = VAR_STRING;
13134 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013136 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137 if (*keys == NUL)
13138 return;
13139
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013140 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013142 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013143 if (argvars[2].v_type != VAR_UNKNOWN)
13144 abbr = get_tv_number(&argvars[2]);
13145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146 else
13147 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013148 if (which == NULL)
13149 return;
13150
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151 mode = get_map_mode(&which, 0);
13152
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013153 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013154 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155 vim_free(keys_buf);
13156 if (rhs != NULL)
13157 {
13158 ga_init(&ga);
13159 ga.ga_itemsize = 1;
13160 ga.ga_growsize = 40;
13161
13162 while (*rhs != NUL)
13163 ga_concat(&ga, str2special(&rhs, FALSE));
13164
13165 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013166 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167 }
13168}
13169
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013170#ifdef FEAT_FLOAT
13171/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013172 * "log()" function
13173 */
13174 static void
13175f_log(argvars, rettv)
13176 typval_T *argvars;
13177 typval_T *rettv;
13178{
13179 float_T f;
13180
13181 rettv->v_type = VAR_FLOAT;
13182 if (get_float_arg(argvars, &f) == OK)
13183 rettv->vval.v_float = log(f);
13184 else
13185 rettv->vval.v_float = 0.0;
13186}
13187
13188/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013189 * "log10()" function
13190 */
13191 static void
13192f_log10(argvars, rettv)
13193 typval_T *argvars;
13194 typval_T *rettv;
13195{
13196 float_T f;
13197
13198 rettv->v_type = VAR_FLOAT;
13199 if (get_float_arg(argvars, &f) == OK)
13200 rettv->vval.v_float = log10(f);
13201 else
13202 rettv->vval.v_float = 0.0;
13203}
13204#endif
13205
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013207 * "map()" function
13208 */
13209 static void
13210f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013211 typval_T *argvars;
13212 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013213{
13214 filter_map(argvars, rettv, TRUE);
13215}
13216
13217/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013218 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 */
13220 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013221f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013222 typval_T *argvars;
13223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013225 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226}
13227
13228/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013229 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230 */
13231 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013232f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013233 typval_T *argvars;
13234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013236 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237}
13238
Bram Moolenaar33570922005-01-25 22:26:29 +000013239static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240
13241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013242find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013243 typval_T *argvars;
13244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245 int type;
13246{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013247 char_u *str = NULL;
13248 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249 char_u *pat;
13250 regmatch_T regmatch;
13251 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013252 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253 char_u *save_cpo;
13254 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013255 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013256 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013257 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013258 list_T *l = NULL;
13259 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013260 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013261 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262
13263 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13264 save_cpo = p_cpo;
13265 p_cpo = (char_u *)"";
13266
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013267 rettv->vval.v_number = -1;
13268 if (type == 3)
13269 {
13270 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013271 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013272 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013273 }
13274 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013276 rettv->v_type = VAR_STRING;
13277 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013280 if (argvars[0].v_type == VAR_LIST)
13281 {
13282 if ((l = argvars[0].vval.v_list) == NULL)
13283 goto theend;
13284 li = l->lv_first;
13285 }
13286 else
13287 expr = str = get_tv_string(&argvars[0]);
13288
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013289 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13290 if (pat == NULL)
13291 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013292
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013293 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013295 int error = FALSE;
13296
13297 start = get_tv_number_chk(&argvars[2], &error);
13298 if (error)
13299 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013300 if (l != NULL)
13301 {
13302 li = list_find(l, start);
13303 if (li == NULL)
13304 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013305 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013306 }
13307 else
13308 {
13309 if (start < 0)
13310 start = 0;
13311 if (start > (long)STRLEN(str))
13312 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013313 /* When "count" argument is there ignore matches before "start",
13314 * otherwise skip part of the string. Differs when pattern is "^"
13315 * or "\<". */
13316 if (argvars[3].v_type != VAR_UNKNOWN)
13317 startcol = start;
13318 else
13319 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013320 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013321
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013322 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013323 nth = get_tv_number_chk(&argvars[3], &error);
13324 if (error)
13325 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326 }
13327
13328 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13329 if (regmatch.regprog != NULL)
13330 {
13331 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013332
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013333 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013334 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013335 if (l != NULL)
13336 {
13337 if (li == NULL)
13338 {
13339 match = FALSE;
13340 break;
13341 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013342 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013343 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013344 if (str == NULL)
13345 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013346 }
13347
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013348 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013349
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013350 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013351 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013352 if (l == NULL && !match)
13353 break;
13354
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013355 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013356 if (l != NULL)
13357 {
13358 li = li->li_next;
13359 ++idx;
13360 }
13361 else
13362 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013363#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013364 startcol = (colnr_T)(regmatch.startp[0]
13365 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013366#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013367 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013368#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013369 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013370 }
13371
13372 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013373 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013374 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013375 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013376 int i;
13377
13378 /* return list with matched string and submatches */
13379 for (i = 0; i < NSUBEXP; ++i)
13380 {
13381 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013382 {
13383 if (list_append_string(rettv->vval.v_list,
13384 (char_u *)"", 0) == FAIL)
13385 break;
13386 }
13387 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013388 regmatch.startp[i],
13389 (int)(regmatch.endp[i] - regmatch.startp[i]))
13390 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013391 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013392 }
13393 }
13394 else if (type == 2)
13395 {
13396 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013397 if (l != NULL)
13398 copy_tv(&li->li_tv, rettv);
13399 else
13400 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013402 }
13403 else if (l != NULL)
13404 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405 else
13406 {
13407 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013408 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409 (varnumber_T)(regmatch.startp[0] - str);
13410 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013411 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013413 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414 }
13415 }
13416 vim_free(regmatch.regprog);
13417 }
13418
13419theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013420 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421 p_cpo = save_cpo;
13422}
13423
13424/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013425 * "match()" function
13426 */
13427 static void
13428f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013429 typval_T *argvars;
13430 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013431{
13432 find_some_match(argvars, rettv, 1);
13433}
13434
13435/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013436 * "matchadd()" function
13437 */
13438 static void
13439f_matchadd(argvars, rettv)
13440 typval_T *argvars;
13441 typval_T *rettv;
13442{
13443#ifdef FEAT_SEARCH_EXTRA
13444 char_u buf[NUMBUFLEN];
13445 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13446 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13447 int prio = 10; /* default priority */
13448 int id = -1;
13449 int error = FALSE;
13450
13451 rettv->vval.v_number = -1;
13452
13453 if (grp == NULL || pat == NULL)
13454 return;
13455 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013456 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013457 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013458 if (argvars[3].v_type != VAR_UNKNOWN)
13459 id = get_tv_number_chk(&argvars[3], &error);
13460 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013461 if (error == TRUE)
13462 return;
13463 if (id >= 1 && id <= 3)
13464 {
13465 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13466 return;
13467 }
13468
13469 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13470#endif
13471}
13472
13473/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013474 * "matcharg()" function
13475 */
13476 static void
13477f_matcharg(argvars, rettv)
13478 typval_T *argvars;
13479 typval_T *rettv;
13480{
13481 if (rettv_list_alloc(rettv) == OK)
13482 {
13483#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013484 int id = get_tv_number(&argvars[0]);
13485 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013486
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013487 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013488 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013489 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13490 {
13491 list_append_string(rettv->vval.v_list,
13492 syn_id2name(m->hlg_id), -1);
13493 list_append_string(rettv->vval.v_list, m->pattern, -1);
13494 }
13495 else
13496 {
13497 list_append_string(rettv->vval.v_list, NUL, -1);
13498 list_append_string(rettv->vval.v_list, NUL, -1);
13499 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013500 }
13501#endif
13502 }
13503}
13504
13505/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013506 * "matchdelete()" function
13507 */
13508 static void
13509f_matchdelete(argvars, rettv)
13510 typval_T *argvars;
13511 typval_T *rettv;
13512{
13513#ifdef FEAT_SEARCH_EXTRA
13514 rettv->vval.v_number = match_delete(curwin,
13515 (int)get_tv_number(&argvars[0]), TRUE);
13516#endif
13517}
13518
13519/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013520 * "matchend()" function
13521 */
13522 static void
13523f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013524 typval_T *argvars;
13525 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013526{
13527 find_some_match(argvars, rettv, 0);
13528}
13529
13530/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013531 * "matchlist()" function
13532 */
13533 static void
13534f_matchlist(argvars, rettv)
13535 typval_T *argvars;
13536 typval_T *rettv;
13537{
13538 find_some_match(argvars, rettv, 3);
13539}
13540
13541/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013542 * "matchstr()" function
13543 */
13544 static void
13545f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013546 typval_T *argvars;
13547 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013548{
13549 find_some_match(argvars, rettv, 2);
13550}
13551
Bram Moolenaar33570922005-01-25 22:26:29 +000013552static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013553
13554 static void
13555max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013556 typval_T *argvars;
13557 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013558 int domax;
13559{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013560 long n = 0;
13561 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013562 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013563
13564 if (argvars[0].v_type == VAR_LIST)
13565 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013566 list_T *l;
13567 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013568
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013569 l = argvars[0].vval.v_list;
13570 if (l != NULL)
13571 {
13572 li = l->lv_first;
13573 if (li != NULL)
13574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013575 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013576 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013577 {
13578 li = li->li_next;
13579 if (li == NULL)
13580 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013581 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013582 if (domax ? i > n : i < n)
13583 n = i;
13584 }
13585 }
13586 }
13587 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013588 else if (argvars[0].v_type == VAR_DICT)
13589 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013590 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013591 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013592 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013593 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013594
13595 d = argvars[0].vval.v_dict;
13596 if (d != NULL)
13597 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013598 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013599 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013600 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013601 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013602 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013603 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013604 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013605 if (first)
13606 {
13607 n = i;
13608 first = FALSE;
13609 }
13610 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013611 n = i;
13612 }
13613 }
13614 }
13615 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013616 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013617 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013618 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013619}
13620
13621/*
13622 * "max()" function
13623 */
13624 static void
13625f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013626 typval_T *argvars;
13627 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013628{
13629 max_min(argvars, rettv, TRUE);
13630}
13631
13632/*
13633 * "min()" function
13634 */
13635 static void
13636f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013637 typval_T *argvars;
13638 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013639{
13640 max_min(argvars, rettv, FALSE);
13641}
13642
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013643static int mkdir_recurse __ARGS((char_u *dir, int prot));
13644
13645/*
13646 * Create the directory in which "dir" is located, and higher levels when
13647 * needed.
13648 */
13649 static int
13650mkdir_recurse(dir, prot)
13651 char_u *dir;
13652 int prot;
13653{
13654 char_u *p;
13655 char_u *updir;
13656 int r = FAIL;
13657
13658 /* Get end of directory name in "dir".
13659 * We're done when it's "/" or "c:/". */
13660 p = gettail_sep(dir);
13661 if (p <= get_past_head(dir))
13662 return OK;
13663
13664 /* If the directory exists we're done. Otherwise: create it.*/
13665 updir = vim_strnsave(dir, (int)(p - dir));
13666 if (updir == NULL)
13667 return FAIL;
13668 if (mch_isdir(updir))
13669 r = OK;
13670 else if (mkdir_recurse(updir, prot) == OK)
13671 r = vim_mkdir_emsg(updir, prot);
13672 vim_free(updir);
13673 return r;
13674}
13675
13676#ifdef vim_mkdir
13677/*
13678 * "mkdir()" function
13679 */
13680 static void
13681f_mkdir(argvars, rettv)
13682 typval_T *argvars;
13683 typval_T *rettv;
13684{
13685 char_u *dir;
13686 char_u buf[NUMBUFLEN];
13687 int prot = 0755;
13688
13689 rettv->vval.v_number = FAIL;
13690 if (check_restricted() || check_secure())
13691 return;
13692
13693 dir = get_tv_string_buf(&argvars[0], buf);
13694 if (argvars[1].v_type != VAR_UNKNOWN)
13695 {
13696 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013697 prot = get_tv_number_chk(&argvars[2], NULL);
13698 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013699 mkdir_recurse(dir, prot);
13700 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013701 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013702}
13703#endif
13704
Bram Moolenaar0d660222005-01-07 21:51:51 +000013705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 * "mode()" function
13707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013709f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013710 typval_T *argvars;
13711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013713 char_u buf[3];
13714
13715 buf[1] = NUL;
13716 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717
13718#ifdef FEAT_VISUAL
13719 if (VIsual_active)
13720 {
13721 if (VIsual_select)
13722 buf[0] = VIsual_mode + 's' - 'v';
13723 else
13724 buf[0] = VIsual_mode;
13725 }
13726 else
13727#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013728 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13729 || State == CONFIRM)
13730 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013732 if (State == ASKMORE)
13733 buf[1] = 'm';
13734 else if (State == CONFIRM)
13735 buf[1] = '?';
13736 }
13737 else if (State == EXTERNCMD)
13738 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739 else if (State & INSERT)
13740 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013741#ifdef FEAT_VREPLACE
13742 if (State & VREPLACE_FLAG)
13743 {
13744 buf[0] = 'R';
13745 buf[1] = 'v';
13746 }
13747 else
13748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 if (State & REPLACE_FLAG)
13750 buf[0] = 'R';
13751 else
13752 buf[0] = 'i';
13753 }
13754 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013757 if (exmode_active)
13758 buf[1] = 'v';
13759 }
13760 else if (exmode_active)
13761 {
13762 buf[0] = 'c';
13763 buf[1] = 'e';
13764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013766 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013768 if (finish_op)
13769 buf[1] = 'o';
13770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013772 /* Clear out the minor mode when the argument is not a non-zero number or
13773 * non-empty string. */
13774 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013775 buf[1] = NUL;
13776
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013777 rettv->vval.v_string = vim_strsave(buf);
13778 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779}
13780
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013781#ifdef FEAT_MZSCHEME
13782/*
13783 * "mzeval()" function
13784 */
13785 static void
13786f_mzeval(argvars, rettv)
13787 typval_T *argvars;
13788 typval_T *rettv;
13789{
13790 char_u *str;
13791 char_u buf[NUMBUFLEN];
13792
13793 str = get_tv_string_buf(&argvars[0], buf);
13794 do_mzeval(str, rettv);
13795}
13796#endif
13797
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013799 * "nextnonblank()" function
13800 */
13801 static void
13802f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013803 typval_T *argvars;
13804 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013805{
13806 linenr_T lnum;
13807
13808 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013810 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013811 {
13812 lnum = 0;
13813 break;
13814 }
13815 if (*skipwhite(ml_get(lnum)) != NUL)
13816 break;
13817 }
13818 rettv->vval.v_number = lnum;
13819}
13820
13821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822 * "nr2char()" function
13823 */
13824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013825f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013826 typval_T *argvars;
13827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828{
13829 char_u buf[NUMBUFLEN];
13830
13831#ifdef FEAT_MBYTE
13832 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013833 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 else
13835#endif
13836 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013837 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838 buf[1] = NUL;
13839 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013840 rettv->v_type = VAR_STRING;
13841 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013842}
13843
13844/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013845 * "pathshorten()" function
13846 */
13847 static void
13848f_pathshorten(argvars, rettv)
13849 typval_T *argvars;
13850 typval_T *rettv;
13851{
13852 char_u *p;
13853
13854 rettv->v_type = VAR_STRING;
13855 p = get_tv_string_chk(&argvars[0]);
13856 if (p == NULL)
13857 rettv->vval.v_string = NULL;
13858 else
13859 {
13860 p = vim_strsave(p);
13861 rettv->vval.v_string = p;
13862 if (p != NULL)
13863 shorten_dir(p);
13864 }
13865}
13866
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013867#ifdef FEAT_FLOAT
13868/*
13869 * "pow()" function
13870 */
13871 static void
13872f_pow(argvars, rettv)
13873 typval_T *argvars;
13874 typval_T *rettv;
13875{
13876 float_T fx, fy;
13877
13878 rettv->v_type = VAR_FLOAT;
13879 if (get_float_arg(argvars, &fx) == OK
13880 && get_float_arg(&argvars[1], &fy) == OK)
13881 rettv->vval.v_float = pow(fx, fy);
13882 else
13883 rettv->vval.v_float = 0.0;
13884}
13885#endif
13886
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013887/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013888 * "prevnonblank()" function
13889 */
13890 static void
13891f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013892 typval_T *argvars;
13893 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013894{
13895 linenr_T lnum;
13896
13897 lnum = get_tv_lnum(argvars);
13898 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13899 lnum = 0;
13900 else
13901 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13902 --lnum;
13903 rettv->vval.v_number = lnum;
13904}
13905
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013906#ifdef HAVE_STDARG_H
13907/* This dummy va_list is here because:
13908 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13909 * - locally in the function results in a "used before set" warning
13910 * - using va_start() to initialize it gives "function with fixed args" error */
13911static va_list ap;
13912#endif
13913
Bram Moolenaar8c711452005-01-14 21:53:12 +000013914/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013915 * "printf()" function
13916 */
13917 static void
13918f_printf(argvars, rettv)
13919 typval_T *argvars;
13920 typval_T *rettv;
13921{
13922 rettv->v_type = VAR_STRING;
13923 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013924#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013925 {
13926 char_u buf[NUMBUFLEN];
13927 int len;
13928 char_u *s;
13929 int saved_did_emsg = did_emsg;
13930 char *fmt;
13931
13932 /* Get the required length, allocate the buffer and do it for real. */
13933 did_emsg = FALSE;
13934 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013935 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013936 if (!did_emsg)
13937 {
13938 s = alloc(len + 1);
13939 if (s != NULL)
13940 {
13941 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013942 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013943 }
13944 }
13945 did_emsg |= saved_did_emsg;
13946 }
13947#endif
13948}
13949
13950/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013951 * "pumvisible()" function
13952 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013953 static void
13954f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013955 typval_T *argvars UNUSED;
13956 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013957{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013958#ifdef FEAT_INS_EXPAND
13959 if (pum_visible())
13960 rettv->vval.v_number = 1;
13961#endif
13962}
13963
13964/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013965 * "range()" function
13966 */
13967 static void
13968f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013969 typval_T *argvars;
13970 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013971{
13972 long start;
13973 long end;
13974 long stride = 1;
13975 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013976 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013978 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013979 if (argvars[1].v_type == VAR_UNKNOWN)
13980 {
13981 end = start - 1;
13982 start = 0;
13983 }
13984 else
13985 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013986 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013987 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013988 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013989 }
13990
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013991 if (error)
13992 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013993 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013994 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013995 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013996 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013997 else
13998 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013999 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014000 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014001 if (list_append_number(rettv->vval.v_list,
14002 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014003 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014004 }
14005}
14006
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014007/*
14008 * "readfile()" function
14009 */
14010 static void
14011f_readfile(argvars, rettv)
14012 typval_T *argvars;
14013 typval_T *rettv;
14014{
14015 int binary = FALSE;
14016 char_u *fname;
14017 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014018 listitem_T *li;
14019#define FREAD_SIZE 200 /* optimized for text lines */
14020 char_u buf[FREAD_SIZE];
14021 int readlen; /* size of last fread() */
14022 int buflen; /* nr of valid chars in buf[] */
14023 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14024 int tolist; /* first byte in buf[] still to be put in list */
14025 int chop; /* how many CR to chop off */
14026 char_u *prev = NULL; /* previously read bytes, if any */
14027 int prevlen = 0; /* length of "prev" if not NULL */
14028 char_u *s;
14029 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014030 long maxline = MAXLNUM;
14031 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014032
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014033 if (argvars[1].v_type != VAR_UNKNOWN)
14034 {
14035 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14036 binary = TRUE;
14037 if (argvars[2].v_type != VAR_UNKNOWN)
14038 maxline = get_tv_number(&argvars[2]);
14039 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014040
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014041 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014042 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014043
14044 /* Always open the file in binary mode, library functions have a mind of
14045 * their own about CR-LF conversion. */
14046 fname = get_tv_string(&argvars[0]);
14047 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14048 {
14049 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14050 return;
14051 }
14052
14053 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014054 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014055 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014056 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014057 buflen = filtd + readlen;
14058 tolist = 0;
14059 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14060 {
14061 if (buf[filtd] == '\n' || readlen <= 0)
14062 {
14063 /* Only when in binary mode add an empty list item when the
14064 * last line ends in a '\n'. */
14065 if (!binary && readlen == 0 && filtd == 0)
14066 break;
14067
14068 /* Found end-of-line or end-of-file: add a text line to the
14069 * list. */
14070 chop = 0;
14071 if (!binary)
14072 while (filtd - chop - 1 >= tolist
14073 && buf[filtd - chop - 1] == '\r')
14074 ++chop;
14075 len = filtd - tolist - chop;
14076 if (prev == NULL)
14077 s = vim_strnsave(buf + tolist, len);
14078 else
14079 {
14080 s = alloc((unsigned)(prevlen + len + 1));
14081 if (s != NULL)
14082 {
14083 mch_memmove(s, prev, prevlen);
14084 vim_free(prev);
14085 prev = NULL;
14086 mch_memmove(s + prevlen, buf + tolist, len);
14087 s[prevlen + len] = NUL;
14088 }
14089 }
14090 tolist = filtd + 1;
14091
14092 li = listitem_alloc();
14093 if (li == NULL)
14094 {
14095 vim_free(s);
14096 break;
14097 }
14098 li->li_tv.v_type = VAR_STRING;
14099 li->li_tv.v_lock = 0;
14100 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014101 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014102
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014103 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014104 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014105 if (readlen <= 0)
14106 break;
14107 }
14108 else if (buf[filtd] == NUL)
14109 buf[filtd] = '\n';
14110 }
14111 if (readlen <= 0)
14112 break;
14113
14114 if (tolist == 0)
14115 {
14116 /* "buf" is full, need to move text to an allocated buffer */
14117 if (prev == NULL)
14118 {
14119 prev = vim_strnsave(buf, buflen);
14120 prevlen = buflen;
14121 }
14122 else
14123 {
14124 s = alloc((unsigned)(prevlen + buflen));
14125 if (s != NULL)
14126 {
14127 mch_memmove(s, prev, prevlen);
14128 mch_memmove(s + prevlen, buf, buflen);
14129 vim_free(prev);
14130 prev = s;
14131 prevlen += buflen;
14132 }
14133 }
14134 filtd = 0;
14135 }
14136 else
14137 {
14138 mch_memmove(buf, buf + tolist, buflen - tolist);
14139 filtd -= tolist;
14140 }
14141 }
14142
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014143 /*
14144 * For a negative line count use only the lines at the end of the file,
14145 * free the rest.
14146 */
14147 if (maxline < 0)
14148 while (cnt > -maxline)
14149 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014150 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014151 --cnt;
14152 }
14153
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014154 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014155 fclose(fd);
14156}
14157
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014158#if defined(FEAT_RELTIME)
14159static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14160
14161/*
14162 * Convert a List to proftime_T.
14163 * Return FAIL when there is something wrong.
14164 */
14165 static int
14166list2proftime(arg, tm)
14167 typval_T *arg;
14168 proftime_T *tm;
14169{
14170 long n1, n2;
14171 int error = FALSE;
14172
14173 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14174 || arg->vval.v_list->lv_len != 2)
14175 return FAIL;
14176 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14177 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14178# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014179 tm->HighPart = n1;
14180 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014181# else
14182 tm->tv_sec = n1;
14183 tm->tv_usec = n2;
14184# endif
14185 return error ? FAIL : OK;
14186}
14187#endif /* FEAT_RELTIME */
14188
14189/*
14190 * "reltime()" function
14191 */
14192 static void
14193f_reltime(argvars, rettv)
14194 typval_T *argvars;
14195 typval_T *rettv;
14196{
14197#ifdef FEAT_RELTIME
14198 proftime_T res;
14199 proftime_T start;
14200
14201 if (argvars[0].v_type == VAR_UNKNOWN)
14202 {
14203 /* No arguments: get current time. */
14204 profile_start(&res);
14205 }
14206 else if (argvars[1].v_type == VAR_UNKNOWN)
14207 {
14208 if (list2proftime(&argvars[0], &res) == FAIL)
14209 return;
14210 profile_end(&res);
14211 }
14212 else
14213 {
14214 /* Two arguments: compute the difference. */
14215 if (list2proftime(&argvars[0], &start) == FAIL
14216 || list2proftime(&argvars[1], &res) == FAIL)
14217 return;
14218 profile_sub(&res, &start);
14219 }
14220
14221 if (rettv_list_alloc(rettv) == OK)
14222 {
14223 long n1, n2;
14224
14225# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014226 n1 = res.HighPart;
14227 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014228# else
14229 n1 = res.tv_sec;
14230 n2 = res.tv_usec;
14231# endif
14232 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14233 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14234 }
14235#endif
14236}
14237
14238/*
14239 * "reltimestr()" function
14240 */
14241 static void
14242f_reltimestr(argvars, rettv)
14243 typval_T *argvars;
14244 typval_T *rettv;
14245{
14246#ifdef FEAT_RELTIME
14247 proftime_T tm;
14248#endif
14249
14250 rettv->v_type = VAR_STRING;
14251 rettv->vval.v_string = NULL;
14252#ifdef FEAT_RELTIME
14253 if (list2proftime(&argvars[0], &tm) == OK)
14254 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14255#endif
14256}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014257
Bram Moolenaar0d660222005-01-07 21:51:51 +000014258#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14259static void make_connection __ARGS((void));
14260static int check_connection __ARGS((void));
14261
14262 static void
14263make_connection()
14264{
14265 if (X_DISPLAY == NULL
14266# ifdef FEAT_GUI
14267 && !gui.in_use
14268# endif
14269 )
14270 {
14271 x_force_connect = TRUE;
14272 setup_term_clip();
14273 x_force_connect = FALSE;
14274 }
14275}
14276
14277 static int
14278check_connection()
14279{
14280 make_connection();
14281 if (X_DISPLAY == NULL)
14282 {
14283 EMSG(_("E240: No connection to Vim server"));
14284 return FAIL;
14285 }
14286 return OK;
14287}
14288#endif
14289
14290#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014291static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014292
14293 static void
14294remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014295 typval_T *argvars;
14296 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014297 int expr;
14298{
14299 char_u *server_name;
14300 char_u *keys;
14301 char_u *r = NULL;
14302 char_u buf[NUMBUFLEN];
14303# ifdef WIN32
14304 HWND w;
14305# else
14306 Window w;
14307# endif
14308
14309 if (check_restricted() || check_secure())
14310 return;
14311
14312# ifdef FEAT_X11
14313 if (check_connection() == FAIL)
14314 return;
14315# endif
14316
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014317 server_name = get_tv_string_chk(&argvars[0]);
14318 if (server_name == NULL)
14319 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014320 keys = get_tv_string_buf(&argvars[1], buf);
14321# ifdef WIN32
14322 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14323# else
14324 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14325 < 0)
14326# endif
14327 {
14328 if (r != NULL)
14329 EMSG(r); /* sending worked but evaluation failed */
14330 else
14331 EMSG2(_("E241: Unable to send to %s"), server_name);
14332 return;
14333 }
14334
14335 rettv->vval.v_string = r;
14336
14337 if (argvars[2].v_type != VAR_UNKNOWN)
14338 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014339 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014340 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014341 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014342
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014343 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014344 v.di_tv.v_type = VAR_STRING;
14345 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014346 idvar = get_tv_string_chk(&argvars[2]);
14347 if (idvar != NULL)
14348 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014349 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014350 }
14351}
14352#endif
14353
14354/*
14355 * "remote_expr()" function
14356 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014357 static void
14358f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014359 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014360 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014361{
14362 rettv->v_type = VAR_STRING;
14363 rettv->vval.v_string = NULL;
14364#ifdef FEAT_CLIENTSERVER
14365 remote_common(argvars, rettv, TRUE);
14366#endif
14367}
14368
14369/*
14370 * "remote_foreground()" function
14371 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014372 static void
14373f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014374 typval_T *argvars UNUSED;
14375 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014376{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014377#ifdef FEAT_CLIENTSERVER
14378# ifdef WIN32
14379 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014380 {
14381 char_u *server_name = get_tv_string_chk(&argvars[0]);
14382
14383 if (server_name != NULL)
14384 serverForeground(server_name);
14385 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014386# else
14387 /* Send a foreground() expression to the server. */
14388 argvars[1].v_type = VAR_STRING;
14389 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14390 argvars[2].v_type = VAR_UNKNOWN;
14391 remote_common(argvars, rettv, TRUE);
14392 vim_free(argvars[1].vval.v_string);
14393# endif
14394#endif
14395}
14396
Bram Moolenaar0d660222005-01-07 21:51:51 +000014397 static void
14398f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014399 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014400 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014401{
14402#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014403 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014404 char_u *s = NULL;
14405# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014406 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014407# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014408 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014409
14410 if (check_restricted() || check_secure())
14411 {
14412 rettv->vval.v_number = -1;
14413 return;
14414 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014415 serverid = get_tv_string_chk(&argvars[0]);
14416 if (serverid == NULL)
14417 {
14418 rettv->vval.v_number = -1;
14419 return; /* type error; errmsg already given */
14420 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014421# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014422 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014423 if (n == 0)
14424 rettv->vval.v_number = -1;
14425 else
14426 {
14427 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14428 rettv->vval.v_number = (s != NULL);
14429 }
14430# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014431 if (check_connection() == FAIL)
14432 return;
14433
14434 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014435 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014436# endif
14437
14438 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014440 char_u *retvar;
14441
Bram Moolenaar33570922005-01-25 22:26:29 +000014442 v.di_tv.v_type = VAR_STRING;
14443 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014444 retvar = get_tv_string_chk(&argvars[1]);
14445 if (retvar != NULL)
14446 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014447 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014448 }
14449#else
14450 rettv->vval.v_number = -1;
14451#endif
14452}
14453
Bram Moolenaar0d660222005-01-07 21:51:51 +000014454 static void
14455f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014456 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014457 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014458{
14459 char_u *r = NULL;
14460
14461#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014462 char_u *serverid = get_tv_string_chk(&argvars[0]);
14463
14464 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014465 {
14466# ifdef WIN32
14467 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014468 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014469
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014470 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014471 if (n != 0)
14472 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14473 if (r == NULL)
14474# else
14475 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014476 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014477# endif
14478 EMSG(_("E277: Unable to read a server reply"));
14479 }
14480#endif
14481 rettv->v_type = VAR_STRING;
14482 rettv->vval.v_string = r;
14483}
14484
14485/*
14486 * "remote_send()" function
14487 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014488 static void
14489f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014490 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014491 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014492{
14493 rettv->v_type = VAR_STRING;
14494 rettv->vval.v_string = NULL;
14495#ifdef FEAT_CLIENTSERVER
14496 remote_common(argvars, rettv, FALSE);
14497#endif
14498}
14499
14500/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014501 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014502 */
14503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014504f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014505 typval_T *argvars;
14506 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014507{
Bram Moolenaar33570922005-01-25 22:26:29 +000014508 list_T *l;
14509 listitem_T *item, *item2;
14510 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014511 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014512 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014513 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014514 dict_T *d;
14515 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014516
Bram Moolenaar8c711452005-01-14 21:53:12 +000014517 if (argvars[0].v_type == VAR_DICT)
14518 {
14519 if (argvars[2].v_type != VAR_UNKNOWN)
14520 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014521 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014522 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014523 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014524 key = get_tv_string_chk(&argvars[1]);
14525 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014527 di = dict_find(d, key, -1);
14528 if (di == NULL)
14529 EMSG2(_(e_dictkey), key);
14530 else
14531 {
14532 *rettv = di->di_tv;
14533 init_tv(&di->di_tv);
14534 dictitem_remove(d, di);
14535 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014536 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014537 }
14538 }
14539 else if (argvars[0].v_type != VAR_LIST)
14540 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014541 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014542 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014544 int error = FALSE;
14545
14546 idx = get_tv_number_chk(&argvars[1], &error);
14547 if (error)
14548 ; /* type error: do nothing, errmsg already given */
14549 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014550 EMSGN(_(e_listidx), idx);
14551 else
14552 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014553 if (argvars[2].v_type == VAR_UNKNOWN)
14554 {
14555 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014556 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014557 *rettv = item->li_tv;
14558 vim_free(item);
14559 }
14560 else
14561 {
14562 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014563 end = get_tv_number_chk(&argvars[2], &error);
14564 if (error)
14565 ; /* type error: do nothing */
14566 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014567 EMSGN(_(e_listidx), end);
14568 else
14569 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014570 int cnt = 0;
14571
14572 for (li = item; li != NULL; li = li->li_next)
14573 {
14574 ++cnt;
14575 if (li == item2)
14576 break;
14577 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014578 if (li == NULL) /* didn't find "item2" after "item" */
14579 EMSG(_(e_invrange));
14580 else
14581 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014582 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014583 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014584 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014585 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014586 l->lv_first = item;
14587 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014588 item->li_prev = NULL;
14589 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014590 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014591 }
14592 }
14593 }
14594 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014595 }
14596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597}
14598
14599/*
14600 * "rename({from}, {to})" function
14601 */
14602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014603f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014604 typval_T *argvars;
14605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606{
14607 char_u buf[NUMBUFLEN];
14608
14609 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014610 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014611 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014612 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14613 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614}
14615
14616/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014617 * "repeat()" function
14618 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014619 static void
14620f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014621 typval_T *argvars;
14622 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014623{
14624 char_u *p;
14625 int n;
14626 int slen;
14627 int len;
14628 char_u *r;
14629 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014630
14631 n = get_tv_number(&argvars[1]);
14632 if (argvars[0].v_type == VAR_LIST)
14633 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014634 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014635 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014636 if (list_extend(rettv->vval.v_list,
14637 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014638 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014639 }
14640 else
14641 {
14642 p = get_tv_string(&argvars[0]);
14643 rettv->v_type = VAR_STRING;
14644 rettv->vval.v_string = NULL;
14645
14646 slen = (int)STRLEN(p);
14647 len = slen * n;
14648 if (len <= 0)
14649 return;
14650
14651 r = alloc(len + 1);
14652 if (r != NULL)
14653 {
14654 for (i = 0; i < n; i++)
14655 mch_memmove(r + i * slen, p, (size_t)slen);
14656 r[len] = NUL;
14657 }
14658
14659 rettv->vval.v_string = r;
14660 }
14661}
14662
14663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664 * "resolve()" function
14665 */
14666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014667f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014668 typval_T *argvars;
14669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670{
14671 char_u *p;
14672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014673 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674#ifdef FEAT_SHORTCUT
14675 {
14676 char_u *v = NULL;
14677
14678 v = mch_resolve_shortcut(p);
14679 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014680 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014682 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683 }
14684#else
14685# ifdef HAVE_READLINK
14686 {
14687 char_u buf[MAXPATHL + 1];
14688 char_u *cpy;
14689 int len;
14690 char_u *remain = NULL;
14691 char_u *q;
14692 int is_relative_to_current = FALSE;
14693 int has_trailing_pathsep = FALSE;
14694 int limit = 100;
14695
14696 p = vim_strsave(p);
14697
14698 if (p[0] == '.' && (vim_ispathsep(p[1])
14699 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14700 is_relative_to_current = TRUE;
14701
14702 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014703 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704 has_trailing_pathsep = TRUE;
14705
14706 q = getnextcomp(p);
14707 if (*q != NUL)
14708 {
14709 /* Separate the first path component in "p", and keep the
14710 * remainder (beginning with the path separator). */
14711 remain = vim_strsave(q - 1);
14712 q[-1] = NUL;
14713 }
14714
14715 for (;;)
14716 {
14717 for (;;)
14718 {
14719 len = readlink((char *)p, (char *)buf, MAXPATHL);
14720 if (len <= 0)
14721 break;
14722 buf[len] = NUL;
14723
14724 if (limit-- == 0)
14725 {
14726 vim_free(p);
14727 vim_free(remain);
14728 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014729 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730 goto fail;
14731 }
14732
14733 /* Ensure that the result will have a trailing path separator
14734 * if the argument has one. */
14735 if (remain == NULL && has_trailing_pathsep)
14736 add_pathsep(buf);
14737
14738 /* Separate the first path component in the link value and
14739 * concatenate the remainders. */
14740 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14741 if (*q != NUL)
14742 {
14743 if (remain == NULL)
14744 remain = vim_strsave(q - 1);
14745 else
14746 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014747 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748 if (cpy != NULL)
14749 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 vim_free(remain);
14751 remain = cpy;
14752 }
14753 }
14754 q[-1] = NUL;
14755 }
14756
14757 q = gettail(p);
14758 if (q > p && *q == NUL)
14759 {
14760 /* Ignore trailing path separator. */
14761 q[-1] = NUL;
14762 q = gettail(p);
14763 }
14764 if (q > p && !mch_isFullName(buf))
14765 {
14766 /* symlink is relative to directory of argument */
14767 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14768 if (cpy != NULL)
14769 {
14770 STRCPY(cpy, p);
14771 STRCPY(gettail(cpy), buf);
14772 vim_free(p);
14773 p = cpy;
14774 }
14775 }
14776 else
14777 {
14778 vim_free(p);
14779 p = vim_strsave(buf);
14780 }
14781 }
14782
14783 if (remain == NULL)
14784 break;
14785
14786 /* Append the first path component of "remain" to "p". */
14787 q = getnextcomp(remain + 1);
14788 len = q - remain - (*q != NUL);
14789 cpy = vim_strnsave(p, STRLEN(p) + len);
14790 if (cpy != NULL)
14791 {
14792 STRNCAT(cpy, remain, len);
14793 vim_free(p);
14794 p = cpy;
14795 }
14796 /* Shorten "remain". */
14797 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014798 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014799 else
14800 {
14801 vim_free(remain);
14802 remain = NULL;
14803 }
14804 }
14805
14806 /* If the result is a relative path name, make it explicitly relative to
14807 * the current directory if and only if the argument had this form. */
14808 if (!vim_ispathsep(*p))
14809 {
14810 if (is_relative_to_current
14811 && *p != NUL
14812 && !(p[0] == '.'
14813 && (p[1] == NUL
14814 || vim_ispathsep(p[1])
14815 || (p[1] == '.'
14816 && (p[2] == NUL
14817 || vim_ispathsep(p[2]))))))
14818 {
14819 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014820 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821 if (cpy != NULL)
14822 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823 vim_free(p);
14824 p = cpy;
14825 }
14826 }
14827 else if (!is_relative_to_current)
14828 {
14829 /* Strip leading "./". */
14830 q = p;
14831 while (q[0] == '.' && vim_ispathsep(q[1]))
14832 q += 2;
14833 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014834 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835 }
14836 }
14837
14838 /* Ensure that the result will have no trailing path separator
14839 * if the argument had none. But keep "/" or "//". */
14840 if (!has_trailing_pathsep)
14841 {
14842 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014843 if (after_pathsep(p, q))
14844 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845 }
14846
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014847 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848 }
14849# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014850 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851# endif
14852#endif
14853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014854 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855
14856#ifdef HAVE_READLINK
14857fail:
14858#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014859 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014860}
14861
14862/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014863 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864 */
14865 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014866f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014867 typval_T *argvars;
14868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014869{
Bram Moolenaar33570922005-01-25 22:26:29 +000014870 list_T *l;
14871 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872
Bram Moolenaar0d660222005-01-07 21:51:51 +000014873 if (argvars[0].v_type != VAR_LIST)
14874 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014875 else if ((l = argvars[0].vval.v_list) != NULL
14876 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014877 {
14878 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014879 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014880 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014881 while (li != NULL)
14882 {
14883 ni = li->li_prev;
14884 list_append(l, li);
14885 li = ni;
14886 }
14887 rettv->vval.v_list = l;
14888 rettv->v_type = VAR_LIST;
14889 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014890 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892}
14893
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014894#define SP_NOMOVE 0x01 /* don't move cursor */
14895#define SP_REPEAT 0x02 /* repeat to find outer pair */
14896#define SP_RETCOUNT 0x04 /* return matchcount */
14897#define SP_SETPCMARK 0x08 /* set previous context mark */
14898#define SP_START 0x10 /* accept match at start position */
14899#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14900#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014901
Bram Moolenaar33570922005-01-25 22:26:29 +000014902static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014903
14904/*
14905 * Get flags for a search function.
14906 * Possibly sets "p_ws".
14907 * Returns BACKWARD, FORWARD or zero (for an error).
14908 */
14909 static int
14910get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014911 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014912 int *flagsp;
14913{
14914 int dir = FORWARD;
14915 char_u *flags;
14916 char_u nbuf[NUMBUFLEN];
14917 int mask;
14918
14919 if (varp->v_type != VAR_UNKNOWN)
14920 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014921 flags = get_tv_string_buf_chk(varp, nbuf);
14922 if (flags == NULL)
14923 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014924 while (*flags != NUL)
14925 {
14926 switch (*flags)
14927 {
14928 case 'b': dir = BACKWARD; break;
14929 case 'w': p_ws = TRUE; break;
14930 case 'W': p_ws = FALSE; break;
14931 default: mask = 0;
14932 if (flagsp != NULL)
14933 switch (*flags)
14934 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014935 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014936 case 'e': mask = SP_END; break;
14937 case 'm': mask = SP_RETCOUNT; break;
14938 case 'n': mask = SP_NOMOVE; break;
14939 case 'p': mask = SP_SUBPAT; break;
14940 case 'r': mask = SP_REPEAT; break;
14941 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014942 }
14943 if (mask == 0)
14944 {
14945 EMSG2(_(e_invarg2), flags);
14946 dir = 0;
14947 }
14948 else
14949 *flagsp |= mask;
14950 }
14951 if (dir == 0)
14952 break;
14953 ++flags;
14954 }
14955 }
14956 return dir;
14957}
14958
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014960 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014962 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014963search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014964 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014965 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014966 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014968 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969 char_u *pat;
14970 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014971 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972 int save_p_ws = p_ws;
14973 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014974 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014975 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014976 proftime_T tm;
14977#ifdef FEAT_RELTIME
14978 long time_limit = 0;
14979#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014980 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014981 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014983 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014984 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014985 if (dir == 0)
14986 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014987 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014988 if (flags & SP_START)
14989 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014990 if (flags & SP_END)
14991 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014992
Bram Moolenaar76929292008-01-06 19:07:36 +000014993 /* Optional arguments: line number to stop searching and timeout. */
14994 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014995 {
14996 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14997 if (lnum_stop < 0)
14998 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014999#ifdef FEAT_RELTIME
15000 if (argvars[3].v_type != VAR_UNKNOWN)
15001 {
15002 time_limit = get_tv_number_chk(&argvars[3], NULL);
15003 if (time_limit < 0)
15004 goto theend;
15005 }
15006#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015007 }
15008
Bram Moolenaar76929292008-01-06 19:07:36 +000015009#ifdef FEAT_RELTIME
15010 /* Set the time limit, if there is one. */
15011 profile_setlimit(time_limit, &tm);
15012#endif
15013
Bram Moolenaar231334e2005-07-25 20:46:57 +000015014 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015015 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015016 * Check to make sure only those flags are set.
15017 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15018 * flags cannot be set. Check for that condition also.
15019 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015020 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015021 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015022 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015023 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015024 goto theend;
15025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015027 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015028 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015029 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015030 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015031 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015032 if (flags & SP_SUBPAT)
15033 retval = subpatnum;
15034 else
15035 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015036 if (flags & SP_SETPCMARK)
15037 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015039 if (match_pos != NULL)
15040 {
15041 /* Store the match cursor position */
15042 match_pos->lnum = pos.lnum;
15043 match_pos->col = pos.col + 1;
15044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045 /* "/$" will put the cursor after the end of the line, may need to
15046 * correct that here */
15047 check_cursor();
15048 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015049
15050 /* If 'n' flag is used: restore cursor position. */
15051 if (flags & SP_NOMOVE)
15052 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015053 else
15054 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015055theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015056 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015057
15058 return retval;
15059}
15060
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015061#ifdef FEAT_FLOAT
15062/*
15063 * "round({float})" function
15064 */
15065 static void
15066f_round(argvars, rettv)
15067 typval_T *argvars;
15068 typval_T *rettv;
15069{
15070 float_T f;
15071
15072 rettv->v_type = VAR_FLOAT;
15073 if (get_float_arg(argvars, &f) == OK)
15074 /* round() is not in C90, use ceil() or floor() instead. */
15075 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15076 else
15077 rettv->vval.v_float = 0.0;
15078}
15079#endif
15080
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015081/*
15082 * "search()" function
15083 */
15084 static void
15085f_search(argvars, rettv)
15086 typval_T *argvars;
15087 typval_T *rettv;
15088{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015089 int flags = 0;
15090
15091 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015092}
15093
Bram Moolenaar071d4272004-06-13 20:20:40 +000015094/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015095 * "searchdecl()" function
15096 */
15097 static void
15098f_searchdecl(argvars, rettv)
15099 typval_T *argvars;
15100 typval_T *rettv;
15101{
15102 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015103 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015104 int error = FALSE;
15105 char_u *name;
15106
15107 rettv->vval.v_number = 1; /* default: FAIL */
15108
15109 name = get_tv_string_chk(&argvars[0]);
15110 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015111 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015112 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015113 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15114 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15115 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015116 if (!error && name != NULL)
15117 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015118 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015119}
15120
15121/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015122 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015124 static int
15125searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015126 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015127 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128{
15129 char_u *spat, *mpat, *epat;
15130 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132 int dir;
15133 int flags = 0;
15134 char_u nbuf1[NUMBUFLEN];
15135 char_u nbuf2[NUMBUFLEN];
15136 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015137 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015138 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015139 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015142 spat = get_tv_string_chk(&argvars[0]);
15143 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15144 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15145 if (spat == NULL || mpat == NULL || epat == NULL)
15146 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148 /* Handle the optional fourth argument: flags */
15149 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015150 if (dir == 0)
15151 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015152
15153 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015154 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15155 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015156 if ((flags & (SP_END | SP_SUBPAT)) != 0
15157 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015158 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015159 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015160 goto theend;
15161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015163 /* Using 'r' implies 'W', otherwise it doesn't work. */
15164 if (flags & SP_REPEAT)
15165 p_ws = FALSE;
15166
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015167 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015168 if (argvars[3].v_type == VAR_UNKNOWN
15169 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015170 skip = (char_u *)"";
15171 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015172 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015173 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015174 if (argvars[5].v_type != VAR_UNKNOWN)
15175 {
15176 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15177 if (lnum_stop < 0)
15178 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015179#ifdef FEAT_RELTIME
15180 if (argvars[6].v_type != VAR_UNKNOWN)
15181 {
15182 time_limit = get_tv_number_chk(&argvars[6], NULL);
15183 if (time_limit < 0)
15184 goto theend;
15185 }
15186#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015187 }
15188 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015189 if (skip == NULL)
15190 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015191
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015192 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015193 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015194
15195theend:
15196 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015197
15198 return retval;
15199}
15200
15201/*
15202 * "searchpair()" function
15203 */
15204 static void
15205f_searchpair(argvars, rettv)
15206 typval_T *argvars;
15207 typval_T *rettv;
15208{
15209 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15210}
15211
15212/*
15213 * "searchpairpos()" function
15214 */
15215 static void
15216f_searchpairpos(argvars, rettv)
15217 typval_T *argvars;
15218 typval_T *rettv;
15219{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015220 pos_T match_pos;
15221 int lnum = 0;
15222 int col = 0;
15223
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015224 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015225 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015226
15227 if (searchpair_cmn(argvars, &match_pos) > 0)
15228 {
15229 lnum = match_pos.lnum;
15230 col = match_pos.col;
15231 }
15232
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015233 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15234 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015235}
15236
15237/*
15238 * Search for a start/middle/end thing.
15239 * Used by searchpair(), see its documentation for the details.
15240 * Returns 0 or -1 for no match,
15241 */
15242 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015243do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15244 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015245 char_u *spat; /* start pattern */
15246 char_u *mpat; /* middle pattern */
15247 char_u *epat; /* end pattern */
15248 int dir; /* BACKWARD or FORWARD */
15249 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015250 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015251 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015252 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015253 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015254{
15255 char_u *save_cpo;
15256 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15257 long retval = 0;
15258 pos_T pos;
15259 pos_T firstpos;
15260 pos_T foundpos;
15261 pos_T save_cursor;
15262 pos_T save_pos;
15263 int n;
15264 int r;
15265 int nest = 1;
15266 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015267 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015268 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015269
15270 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15271 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015272 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015273
Bram Moolenaar76929292008-01-06 19:07:36 +000015274#ifdef FEAT_RELTIME
15275 /* Set the time limit, if there is one. */
15276 profile_setlimit(time_limit, &tm);
15277#endif
15278
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015279 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15280 * start/middle/end (pat3, for the top pair). */
15281 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15282 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15283 if (pat2 == NULL || pat3 == NULL)
15284 goto theend;
15285 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15286 if (*mpat == NUL)
15287 STRCPY(pat3, pat2);
15288 else
15289 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15290 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015291 if (flags & SP_START)
15292 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015293
Bram Moolenaar071d4272004-06-13 20:20:40 +000015294 save_cursor = curwin->w_cursor;
15295 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015296 clearpos(&firstpos);
15297 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298 pat = pat3;
15299 for (;;)
15300 {
15301 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015302 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15304 /* didn't find it or found the first match again: FAIL */
15305 break;
15306
15307 if (firstpos.lnum == 0)
15308 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015309 if (equalpos(pos, foundpos))
15310 {
15311 /* Found the same position again. Can happen with a pattern that
15312 * has "\zs" at the end and searching backwards. Advance one
15313 * character and try again. */
15314 if (dir == BACKWARD)
15315 decl(&pos);
15316 else
15317 incl(&pos);
15318 }
15319 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015321 /* clear the start flag to avoid getting stuck here */
15322 options &= ~SEARCH_START;
15323
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324 /* If the skip pattern matches, ignore this match. */
15325 if (*skip != NUL)
15326 {
15327 save_pos = curwin->w_cursor;
15328 curwin->w_cursor = pos;
15329 r = eval_to_bool(skip, &err, NULL, FALSE);
15330 curwin->w_cursor = save_pos;
15331 if (err)
15332 {
15333 /* Evaluating {skip} caused an error, break here. */
15334 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015335 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336 break;
15337 }
15338 if (r)
15339 continue;
15340 }
15341
15342 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15343 {
15344 /* Found end when searching backwards or start when searching
15345 * forward: nested pair. */
15346 ++nest;
15347 pat = pat2; /* nested, don't search for middle */
15348 }
15349 else
15350 {
15351 /* Found end when searching forward or start when searching
15352 * backward: end of (nested) pair; or found middle in outer pair. */
15353 if (--nest == 1)
15354 pat = pat3; /* outer level, search for middle */
15355 }
15356
15357 if (nest == 0)
15358 {
15359 /* Found the match: return matchcount or line number. */
15360 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015361 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015363 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015364 if (flags & SP_SETPCMARK)
15365 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366 curwin->w_cursor = pos;
15367 if (!(flags & SP_REPEAT))
15368 break;
15369 nest = 1; /* search for next unmatched */
15370 }
15371 }
15372
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015373 if (match_pos != NULL)
15374 {
15375 /* Store the match cursor position */
15376 match_pos->lnum = curwin->w_cursor.lnum;
15377 match_pos->col = curwin->w_cursor.col + 1;
15378 }
15379
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015381 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382 curwin->w_cursor = save_cursor;
15383
15384theend:
15385 vim_free(pat2);
15386 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015387 if (p_cpo == empty_option)
15388 p_cpo = save_cpo;
15389 else
15390 /* Darn, evaluating the {skip} expression changed the value. */
15391 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015392
15393 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394}
15395
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015396/*
15397 * "searchpos()" function
15398 */
15399 static void
15400f_searchpos(argvars, rettv)
15401 typval_T *argvars;
15402 typval_T *rettv;
15403{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015404 pos_T match_pos;
15405 int lnum = 0;
15406 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015407 int n;
15408 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015409
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015410 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015411 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015412
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015413 n = search_cmn(argvars, &match_pos, &flags);
15414 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015415 {
15416 lnum = match_pos.lnum;
15417 col = match_pos.col;
15418 }
15419
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015420 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15421 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015422 if (flags & SP_SUBPAT)
15423 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015424}
15425
15426
Bram Moolenaar0d660222005-01-07 21:51:51 +000015427 static void
15428f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015429 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015432#ifdef FEAT_CLIENTSERVER
15433 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015434 char_u *server = get_tv_string_chk(&argvars[0]);
15435 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015436
Bram Moolenaar0d660222005-01-07 21:51:51 +000015437 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015438 if (server == NULL || reply == NULL)
15439 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015440 if (check_restricted() || check_secure())
15441 return;
15442# ifdef FEAT_X11
15443 if (check_connection() == FAIL)
15444 return;
15445# endif
15446
15447 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015449 EMSG(_("E258: Unable to send to client"));
15450 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015452 rettv->vval.v_number = 0;
15453#else
15454 rettv->vval.v_number = -1;
15455#endif
15456}
15457
Bram Moolenaar0d660222005-01-07 21:51:51 +000015458 static void
15459f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015460 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015461 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015462{
15463 char_u *r = NULL;
15464
15465#ifdef FEAT_CLIENTSERVER
15466# ifdef WIN32
15467 r = serverGetVimNames();
15468# else
15469 make_connection();
15470 if (X_DISPLAY != NULL)
15471 r = serverGetVimNames(X_DISPLAY);
15472# endif
15473#endif
15474 rettv->v_type = VAR_STRING;
15475 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476}
15477
15478/*
15479 * "setbufvar()" function
15480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015482f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015483 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015484 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485{
15486 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015488 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015489 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490 char_u nbuf[NUMBUFLEN];
15491
15492 if (check_restricted() || check_secure())
15493 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015494 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15495 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015496 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015497 varp = &argvars[2];
15498
15499 if (buf != NULL && varname != NULL && varp != NULL)
15500 {
15501 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503
15504 if (*varname == '&')
15505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015506 long numval;
15507 char_u *strval;
15508 int error = FALSE;
15509
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015511 numval = get_tv_number_chk(varp, &error);
15512 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015513 if (!error && strval != NULL)
15514 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515 }
15516 else
15517 {
15518 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15519 if (bufvarname != NULL)
15520 {
15521 STRCPY(bufvarname, "b:");
15522 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015523 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524 vim_free(bufvarname);
15525 }
15526 }
15527
15528 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531}
15532
15533/*
15534 * "setcmdpos()" function
15535 */
15536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015537f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015538 typval_T *argvars;
15539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015541 int pos = (int)get_tv_number(&argvars[0]) - 1;
15542
15543 if (pos >= 0)
15544 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545}
15546
15547/*
15548 * "setline()" function
15549 */
15550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015551f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015552 typval_T *argvars;
15553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554{
15555 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015556 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015557 list_T *l = NULL;
15558 listitem_T *li = NULL;
15559 long added = 0;
15560 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015562 lnum = get_tv_lnum(&argvars[0]);
15563 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015565 l = argvars[1].vval.v_list;
15566 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015568 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015569 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015570
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015571 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015572 for (;;)
15573 {
15574 if (l != NULL)
15575 {
15576 /* list argument, get next string */
15577 if (li == NULL)
15578 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015579 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015580 li = li->li_next;
15581 }
15582
15583 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015584 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015585 break;
15586 if (lnum <= curbuf->b_ml.ml_line_count)
15587 {
15588 /* existing line, replace it */
15589 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15590 {
15591 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015592 if (lnum == curwin->w_cursor.lnum)
15593 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015594 rettv->vval.v_number = 0; /* OK */
15595 }
15596 }
15597 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15598 {
15599 /* lnum is one past the last line, append the line */
15600 ++added;
15601 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15602 rettv->vval.v_number = 0; /* OK */
15603 }
15604
15605 if (l == NULL) /* only one string argument */
15606 break;
15607 ++lnum;
15608 }
15609
15610 if (added > 0)
15611 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612}
15613
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015614static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15615
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015617 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015618 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015619 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015620set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015621 win_T *wp UNUSED;
15622 typval_T *list_arg UNUSED;
15623 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015624 typval_T *rettv;
15625{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015626#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015627 char_u *act;
15628 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015629#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015630
Bram Moolenaar2641f772005-03-25 21:58:17 +000015631 rettv->vval.v_number = -1;
15632
15633#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015634 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015635 EMSG(_(e_listreq));
15636 else
15637 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015638 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015639
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015640 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015641 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015642 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015643 if (act == NULL)
15644 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015645 if (*act == 'a' || *act == 'r')
15646 action = *act;
15647 }
15648
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015649 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015650 rettv->vval.v_number = 0;
15651 }
15652#endif
15653}
15654
15655/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015656 * "setloclist()" function
15657 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015658 static void
15659f_setloclist(argvars, rettv)
15660 typval_T *argvars;
15661 typval_T *rettv;
15662{
15663 win_T *win;
15664
15665 rettv->vval.v_number = -1;
15666
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015667 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015668 if (win != NULL)
15669 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15670}
15671
15672/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015673 * "setmatches()" function
15674 */
15675 static void
15676f_setmatches(argvars, rettv)
15677 typval_T *argvars;
15678 typval_T *rettv;
15679{
15680#ifdef FEAT_SEARCH_EXTRA
15681 list_T *l;
15682 listitem_T *li;
15683 dict_T *d;
15684
15685 rettv->vval.v_number = -1;
15686 if (argvars[0].v_type != VAR_LIST)
15687 {
15688 EMSG(_(e_listreq));
15689 return;
15690 }
15691 if ((l = argvars[0].vval.v_list) != NULL)
15692 {
15693
15694 /* To some extent make sure that we are dealing with a list from
15695 * "getmatches()". */
15696 li = l->lv_first;
15697 while (li != NULL)
15698 {
15699 if (li->li_tv.v_type != VAR_DICT
15700 || (d = li->li_tv.vval.v_dict) == NULL)
15701 {
15702 EMSG(_(e_invarg));
15703 return;
15704 }
15705 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15706 && dict_find(d, (char_u *)"pattern", -1) != NULL
15707 && dict_find(d, (char_u *)"priority", -1) != NULL
15708 && dict_find(d, (char_u *)"id", -1) != NULL))
15709 {
15710 EMSG(_(e_invarg));
15711 return;
15712 }
15713 li = li->li_next;
15714 }
15715
15716 clear_matches(curwin);
15717 li = l->lv_first;
15718 while (li != NULL)
15719 {
15720 d = li->li_tv.vval.v_dict;
15721 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15722 get_dict_string(d, (char_u *)"pattern", FALSE),
15723 (int)get_dict_number(d, (char_u *)"priority"),
15724 (int)get_dict_number(d, (char_u *)"id"));
15725 li = li->li_next;
15726 }
15727 rettv->vval.v_number = 0;
15728 }
15729#endif
15730}
15731
15732/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015733 * "setpos()" function
15734 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015735 static void
15736f_setpos(argvars, rettv)
15737 typval_T *argvars;
15738 typval_T *rettv;
15739{
15740 pos_T pos;
15741 int fnum;
15742 char_u *name;
15743
Bram Moolenaar08250432008-02-13 11:42:46 +000015744 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015745 name = get_tv_string_chk(argvars);
15746 if (name != NULL)
15747 {
15748 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15749 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015750 if (--pos.col < 0)
15751 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015752 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015753 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015754 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015755 if (fnum == curbuf->b_fnum)
15756 {
15757 curwin->w_cursor = pos;
15758 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015759 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015760 }
15761 else
15762 EMSG(_(e_invarg));
15763 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015764 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15765 {
15766 /* set mark */
15767 if (setmark_pos(name[1], &pos, fnum) == OK)
15768 rettv->vval.v_number = 0;
15769 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015770 else
15771 EMSG(_(e_invarg));
15772 }
15773 }
15774}
15775
15776/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015777 * "setqflist()" function
15778 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015779 static void
15780f_setqflist(argvars, rettv)
15781 typval_T *argvars;
15782 typval_T *rettv;
15783{
15784 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15785}
15786
15787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788 * "setreg()" function
15789 */
15790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015791f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015792 typval_T *argvars;
15793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794{
15795 int regname;
15796 char_u *strregname;
15797 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015798 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015799 int append;
15800 char_u yank_type;
15801 long block_len;
15802
15803 block_len = -1;
15804 yank_type = MAUTO;
15805 append = FALSE;
15806
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015807 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015808 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015810 if (strregname == NULL)
15811 return; /* type error; errmsg already given */
15812 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015813 if (regname == 0 || regname == '@')
15814 regname = '"';
15815 else if (regname == '=')
15816 return;
15817
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015818 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015820 stropt = get_tv_string_chk(&argvars[2]);
15821 if (stropt == NULL)
15822 return; /* type error */
15823 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824 switch (*stropt)
15825 {
15826 case 'a': case 'A': /* append */
15827 append = TRUE;
15828 break;
15829 case 'v': case 'c': /* character-wise selection */
15830 yank_type = MCHAR;
15831 break;
15832 case 'V': case 'l': /* line-wise selection */
15833 yank_type = MLINE;
15834 break;
15835#ifdef FEAT_VISUAL
15836 case 'b': case Ctrl_V: /* block-wise selection */
15837 yank_type = MBLOCK;
15838 if (VIM_ISDIGIT(stropt[1]))
15839 {
15840 ++stropt;
15841 block_len = getdigits(&stropt) - 1;
15842 --stropt;
15843 }
15844 break;
15845#endif
15846 }
15847 }
15848
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015849 strval = get_tv_string_chk(&argvars[1]);
15850 if (strval != NULL)
15851 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015853 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854}
15855
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015856/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020015857 * "settabvar()" function
15858 */
15859 static void
15860f_settabvar(argvars, rettv)
15861 typval_T *argvars;
15862 typval_T *rettv;
15863{
15864 tabpage_T *save_curtab;
15865 char_u *varname, *tabvarname;
15866 typval_T *varp;
15867 tabpage_T *tp;
15868
15869 rettv->vval.v_number = 0;
15870
15871 if (check_restricted() || check_secure())
15872 return;
15873
15874 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15875 varname = get_tv_string_chk(&argvars[1]);
15876 varp = &argvars[2];
15877
15878 if (tp != NULL && varname != NULL && varp != NULL)
15879 {
15880 save_curtab = curtab;
15881 goto_tabpage_tp(tp);
15882
15883 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
15884 if (tabvarname != NULL)
15885 {
15886 STRCPY(tabvarname, "t:");
15887 STRCPY(tabvarname + 2, varname);
15888 set_var(tabvarname, varp, TRUE);
15889 vim_free(tabvarname);
15890 }
15891
15892 /* Restore current tabpage */
15893 if (valid_tabpage(save_curtab))
15894 goto_tabpage_tp(save_curtab);
15895 }
15896}
15897
15898/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015899 * "settabwinvar()" function
15900 */
15901 static void
15902f_settabwinvar(argvars, rettv)
15903 typval_T *argvars;
15904 typval_T *rettv;
15905{
15906 setwinvar(argvars, rettv, 1);
15907}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015908
15909/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015910 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015913f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015914 typval_T *argvars;
15915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015916{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015917 setwinvar(argvars, rettv, 0);
15918}
15919
15920/*
15921 * "setwinvar()" and "settabwinvar()" functions
15922 */
15923 static void
15924setwinvar(argvars, rettv, off)
15925 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015926 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015927 int off;
15928{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929 win_T *win;
15930#ifdef FEAT_WINDOWS
15931 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015932 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933#endif
15934 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015935 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015936 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015937 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938
15939 if (check_restricted() || check_secure())
15940 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015941
15942#ifdef FEAT_WINDOWS
15943 if (off == 1)
15944 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15945 else
15946 tp = curtab;
15947#endif
15948 win = find_win_by_nr(&argvars[off], tp);
15949 varname = get_tv_string_chk(&argvars[off + 1]);
15950 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015951
15952 if (win != NULL && varname != NULL && varp != NULL)
15953 {
15954#ifdef FEAT_WINDOWS
15955 /* set curwin to be our win, temporarily */
15956 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015957 save_curtab = curtab;
15958 goto_tabpage_tp(tp);
15959 if (!win_valid(win))
15960 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015961 curwin = win;
15962 curbuf = curwin->w_buffer;
15963#endif
15964
15965 if (*varname == '&')
15966 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015967 long numval;
15968 char_u *strval;
15969 int error = FALSE;
15970
Bram Moolenaar071d4272004-06-13 20:20:40 +000015971 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015972 numval = get_tv_number_chk(varp, &error);
15973 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015974 if (!error && strval != NULL)
15975 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015976 }
15977 else
15978 {
15979 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15980 if (winvarname != NULL)
15981 {
15982 STRCPY(winvarname, "w:");
15983 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015984 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015985 vim_free(winvarname);
15986 }
15987 }
15988
15989#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015990 /* Restore current tabpage and window, if still valid (autocomands can
15991 * make them invalid). */
15992 if (valid_tabpage(save_curtab))
15993 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994 if (win_valid(save_curwin))
15995 {
15996 curwin = save_curwin;
15997 curbuf = curwin->w_buffer;
15998 }
15999#endif
16000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016001}
16002
16003/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016004 * "shellescape({string})" function
16005 */
16006 static void
16007f_shellescape(argvars, rettv)
16008 typval_T *argvars;
16009 typval_T *rettv;
16010{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016011 rettv->vval.v_string = vim_strsave_shellescape(
16012 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016013 rettv->v_type = VAR_STRING;
16014}
16015
16016/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016017 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018 */
16019 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016020f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016021 typval_T *argvars;
16022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016023{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016024 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016025
Bram Moolenaar0d660222005-01-07 21:51:51 +000016026 p = get_tv_string(&argvars[0]);
16027 rettv->vval.v_string = vim_strsave(p);
16028 simplify_filename(rettv->vval.v_string); /* simplify in place */
16029 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030}
16031
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016032#ifdef FEAT_FLOAT
16033/*
16034 * "sin()" function
16035 */
16036 static void
16037f_sin(argvars, rettv)
16038 typval_T *argvars;
16039 typval_T *rettv;
16040{
16041 float_T f;
16042
16043 rettv->v_type = VAR_FLOAT;
16044 if (get_float_arg(argvars, &f) == OK)
16045 rettv->vval.v_float = sin(f);
16046 else
16047 rettv->vval.v_float = 0.0;
16048}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016049
16050/*
16051 * "sinh()" function
16052 */
16053 static void
16054f_sinh(argvars, rettv)
16055 typval_T *argvars;
16056 typval_T *rettv;
16057{
16058 float_T f;
16059
16060 rettv->v_type = VAR_FLOAT;
16061 if (get_float_arg(argvars, &f) == OK)
16062 rettv->vval.v_float = sinh(f);
16063 else
16064 rettv->vval.v_float = 0.0;
16065}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016066#endif
16067
Bram Moolenaar0d660222005-01-07 21:51:51 +000016068static int
16069#ifdef __BORLANDC__
16070 _RTLENTRYF
16071#endif
16072 item_compare __ARGS((const void *s1, const void *s2));
16073static int
16074#ifdef __BORLANDC__
16075 _RTLENTRYF
16076#endif
16077 item_compare2 __ARGS((const void *s1, const void *s2));
16078
16079static int item_compare_ic;
16080static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016081static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016082#define ITEM_COMPARE_FAIL 999
16083
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016085 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016087 static int
16088#ifdef __BORLANDC__
16089_RTLENTRYF
16090#endif
16091item_compare(s1, s2)
16092 const void *s1;
16093 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016094{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016095 char_u *p1, *p2;
16096 char_u *tofree1, *tofree2;
16097 int res;
16098 char_u numbuf1[NUMBUFLEN];
16099 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016101 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16102 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016103 if (p1 == NULL)
16104 p1 = (char_u *)"";
16105 if (p2 == NULL)
16106 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016107 if (item_compare_ic)
16108 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016110 res = STRCMP(p1, p2);
16111 vim_free(tofree1);
16112 vim_free(tofree2);
16113 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114}
16115
16116 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016117#ifdef __BORLANDC__
16118_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016120item_compare2(s1, s2)
16121 const void *s1;
16122 const void *s2;
16123{
16124 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016125 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016126 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016127 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016129 /* shortcut after failure in previous call; compare all items equal */
16130 if (item_compare_func_err)
16131 return 0;
16132
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016133 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16134 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016135 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16136 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016137
16138 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016139 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016140 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016141 clear_tv(&argv[0]);
16142 clear_tv(&argv[1]);
16143
16144 if (res == FAIL)
16145 res = ITEM_COMPARE_FAIL;
16146 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016147 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16148 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016149 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016150 clear_tv(&rettv);
16151 return res;
16152}
16153
16154/*
16155 * "sort({list})" function
16156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016158f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016159 typval_T *argvars;
16160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161{
Bram Moolenaar33570922005-01-25 22:26:29 +000016162 list_T *l;
16163 listitem_T *li;
16164 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016165 long len;
16166 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167
Bram Moolenaar0d660222005-01-07 21:51:51 +000016168 if (argvars[0].v_type != VAR_LIST)
16169 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170 else
16171 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016172 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016173 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016174 return;
16175 rettv->vval.v_list = l;
16176 rettv->v_type = VAR_LIST;
16177 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178
Bram Moolenaar0d660222005-01-07 21:51:51 +000016179 len = list_len(l);
16180 if (len <= 1)
16181 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182
Bram Moolenaar0d660222005-01-07 21:51:51 +000016183 item_compare_ic = FALSE;
16184 item_compare_func = NULL;
16185 if (argvars[1].v_type != VAR_UNKNOWN)
16186 {
16187 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016188 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016189 else
16190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016191 int error = FALSE;
16192
16193 i = get_tv_number_chk(&argvars[1], &error);
16194 if (error)
16195 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016196 if (i == 1)
16197 item_compare_ic = TRUE;
16198 else
16199 item_compare_func = get_tv_string(&argvars[1]);
16200 }
16201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202
Bram Moolenaar0d660222005-01-07 21:51:51 +000016203 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016204 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016205 if (ptrs == NULL)
16206 return;
16207 i = 0;
16208 for (li = l->lv_first; li != NULL; li = li->li_next)
16209 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016211 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016212 /* test the compare function */
16213 if (item_compare_func != NULL
16214 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16215 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016216 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016218 {
16219 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016220 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016221 item_compare_func == NULL ? item_compare : item_compare2);
16222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016223 if (!item_compare_func_err)
16224 {
16225 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016226 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016227 l->lv_len = 0;
16228 for (i = 0; i < len; ++i)
16229 list_append(l, ptrs[i]);
16230 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016231 }
16232
16233 vim_free(ptrs);
16234 }
16235}
16236
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016237/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016238 * "soundfold({word})" function
16239 */
16240 static void
16241f_soundfold(argvars, rettv)
16242 typval_T *argvars;
16243 typval_T *rettv;
16244{
16245 char_u *s;
16246
16247 rettv->v_type = VAR_STRING;
16248 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016249#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016250 rettv->vval.v_string = eval_soundfold(s);
16251#else
16252 rettv->vval.v_string = vim_strsave(s);
16253#endif
16254}
16255
16256/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016257 * "spellbadword()" function
16258 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016259 static void
16260f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016261 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016262 typval_T *rettv;
16263{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016264 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016265 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016266 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016267
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016268 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016269 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016270
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016271#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016272 if (argvars[0].v_type == VAR_UNKNOWN)
16273 {
16274 /* Find the start and length of the badly spelled word. */
16275 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16276 if (len != 0)
16277 word = ml_get_cursor();
16278 }
16279 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16280 {
16281 char_u *str = get_tv_string_chk(&argvars[0]);
16282 int capcol = -1;
16283
16284 if (str != NULL)
16285 {
16286 /* Check the argument for spelling. */
16287 while (*str != NUL)
16288 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016289 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016290 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016291 {
16292 word = str;
16293 break;
16294 }
16295 str += len;
16296 }
16297 }
16298 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016299#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016300
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016301 list_append_string(rettv->vval.v_list, word, len);
16302 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016303 attr == HLF_SPB ? "bad" :
16304 attr == HLF_SPR ? "rare" :
16305 attr == HLF_SPL ? "local" :
16306 attr == HLF_SPC ? "caps" :
16307 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016308}
16309
16310/*
16311 * "spellsuggest()" function
16312 */
16313 static void
16314f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016315 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016316 typval_T *rettv;
16317{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016318#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016319 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016320 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016321 int maxcount;
16322 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016323 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016324 listitem_T *li;
16325 int need_capital = FALSE;
16326#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016327
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016328 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016329 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016330
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016331#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016332 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16333 {
16334 str = get_tv_string(&argvars[0]);
16335 if (argvars[1].v_type != VAR_UNKNOWN)
16336 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016337 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016338 if (maxcount <= 0)
16339 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016340 if (argvars[2].v_type != VAR_UNKNOWN)
16341 {
16342 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16343 if (typeerr)
16344 return;
16345 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016346 }
16347 else
16348 maxcount = 25;
16349
Bram Moolenaar4770d092006-01-12 23:22:24 +000016350 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016351
16352 for (i = 0; i < ga.ga_len; ++i)
16353 {
16354 str = ((char_u **)ga.ga_data)[i];
16355
16356 li = listitem_alloc();
16357 if (li == NULL)
16358 vim_free(str);
16359 else
16360 {
16361 li->li_tv.v_type = VAR_STRING;
16362 li->li_tv.v_lock = 0;
16363 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016364 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016365 }
16366 }
16367 ga_clear(&ga);
16368 }
16369#endif
16370}
16371
Bram Moolenaar0d660222005-01-07 21:51:51 +000016372 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016373f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016374 typval_T *argvars;
16375 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016376{
16377 char_u *str;
16378 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016379 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016380 regmatch_T regmatch;
16381 char_u patbuf[NUMBUFLEN];
16382 char_u *save_cpo;
16383 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016384 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016385 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016386 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016387
16388 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16389 save_cpo = p_cpo;
16390 p_cpo = (char_u *)"";
16391
16392 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016393 if (argvars[1].v_type != VAR_UNKNOWN)
16394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016395 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16396 if (pat == NULL)
16397 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016398 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016399 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016400 }
16401 if (pat == NULL || *pat == NUL)
16402 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016403
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016404 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016406 if (typeerr)
16407 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408
Bram Moolenaar0d660222005-01-07 21:51:51 +000016409 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16410 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016412 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016413 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016414 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016415 if (*str == NUL)
16416 match = FALSE; /* empty item at the end */
16417 else
16418 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016419 if (match)
16420 end = regmatch.startp[0];
16421 else
16422 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016423 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16424 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016425 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016426 if (list_append_string(rettv->vval.v_list, str,
16427 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016428 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016429 }
16430 if (!match)
16431 break;
16432 /* Advance to just after the match. */
16433 if (regmatch.endp[0] > str)
16434 col = 0;
16435 else
16436 {
16437 /* Don't get stuck at the same match. */
16438#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016439 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016440#else
16441 col = 1;
16442#endif
16443 }
16444 str = regmatch.endp[0];
16445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446
Bram Moolenaar0d660222005-01-07 21:51:51 +000016447 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449
Bram Moolenaar0d660222005-01-07 21:51:51 +000016450 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451}
16452
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016453#ifdef FEAT_FLOAT
16454/*
16455 * "sqrt()" function
16456 */
16457 static void
16458f_sqrt(argvars, rettv)
16459 typval_T *argvars;
16460 typval_T *rettv;
16461{
16462 float_T f;
16463
16464 rettv->v_type = VAR_FLOAT;
16465 if (get_float_arg(argvars, &f) == OK)
16466 rettv->vval.v_float = sqrt(f);
16467 else
16468 rettv->vval.v_float = 0.0;
16469}
16470
16471/*
16472 * "str2float()" function
16473 */
16474 static void
16475f_str2float(argvars, rettv)
16476 typval_T *argvars;
16477 typval_T *rettv;
16478{
16479 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16480
16481 if (*p == '+')
16482 p = skipwhite(p + 1);
16483 (void)string2float(p, &rettv->vval.v_float);
16484 rettv->v_type = VAR_FLOAT;
16485}
16486#endif
16487
Bram Moolenaar2c932302006-03-18 21:42:09 +000016488/*
16489 * "str2nr()" function
16490 */
16491 static void
16492f_str2nr(argvars, rettv)
16493 typval_T *argvars;
16494 typval_T *rettv;
16495{
16496 int base = 10;
16497 char_u *p;
16498 long n;
16499
16500 if (argvars[1].v_type != VAR_UNKNOWN)
16501 {
16502 base = get_tv_number(&argvars[1]);
16503 if (base != 8 && base != 10 && base != 16)
16504 {
16505 EMSG(_(e_invarg));
16506 return;
16507 }
16508 }
16509
16510 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016511 if (*p == '+')
16512 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016513 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16514 rettv->vval.v_number = n;
16515}
16516
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517#ifdef HAVE_STRFTIME
16518/*
16519 * "strftime({format}[, {time}])" function
16520 */
16521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016522f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016523 typval_T *argvars;
16524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525{
16526 char_u result_buf[256];
16527 struct tm *curtime;
16528 time_t seconds;
16529 char_u *p;
16530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016531 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016533 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016534 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535 seconds = time(NULL);
16536 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016537 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538 curtime = localtime(&seconds);
16539 /* MSVC returns NULL for an invalid value of seconds. */
16540 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016541 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542 else
16543 {
16544# ifdef FEAT_MBYTE
16545 vimconv_T conv;
16546 char_u *enc;
16547
16548 conv.vc_type = CONV_NONE;
16549 enc = enc_locale();
16550 convert_setup(&conv, p_enc, enc);
16551 if (conv.vc_type != CONV_NONE)
16552 p = string_convert(&conv, p, NULL);
16553# endif
16554 if (p != NULL)
16555 (void)strftime((char *)result_buf, sizeof(result_buf),
16556 (char *)p, curtime);
16557 else
16558 result_buf[0] = NUL;
16559
16560# ifdef FEAT_MBYTE
16561 if (conv.vc_type != CONV_NONE)
16562 vim_free(p);
16563 convert_setup(&conv, enc, p_enc);
16564 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016565 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566 else
16567# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016568 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569
16570# ifdef FEAT_MBYTE
16571 /* Release conversion descriptors */
16572 convert_setup(&conv, NULL, NULL);
16573 vim_free(enc);
16574# endif
16575 }
16576}
16577#endif
16578
16579/*
16580 * "stridx()" function
16581 */
16582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016583f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016584 typval_T *argvars;
16585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586{
16587 char_u buf[NUMBUFLEN];
16588 char_u *needle;
16589 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016590 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016592 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016594 needle = get_tv_string_chk(&argvars[1]);
16595 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016596 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016597 if (needle == NULL || haystack == NULL)
16598 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016599
Bram Moolenaar33570922005-01-25 22:26:29 +000016600 if (argvars[2].v_type != VAR_UNKNOWN)
16601 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016602 int error = FALSE;
16603
16604 start_idx = get_tv_number_chk(&argvars[2], &error);
16605 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016606 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016607 if (start_idx >= 0)
16608 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016609 }
16610
16611 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16612 if (pos != NULL)
16613 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614}
16615
16616/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016617 * "string()" function
16618 */
16619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016620f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016621 typval_T *argvars;
16622 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016623{
16624 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016625 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016627 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016628 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016629 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016630 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016631 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632}
16633
16634/*
16635 * "strlen()" function
16636 */
16637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016638f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016639 typval_T *argvars;
16640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016642 rettv->vval.v_number = (varnumber_T)(STRLEN(
16643 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644}
16645
16646/*
16647 * "strpart()" function
16648 */
16649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016650f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016651 typval_T *argvars;
16652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653{
16654 char_u *p;
16655 int n;
16656 int len;
16657 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016658 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016660 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661 slen = (int)STRLEN(p);
16662
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016663 n = get_tv_number_chk(&argvars[1], &error);
16664 if (error)
16665 len = 0;
16666 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016667 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668 else
16669 len = slen - n; /* default len: all bytes that are available. */
16670
16671 /*
16672 * Only return the overlap between the specified part and the actual
16673 * string.
16674 */
16675 if (n < 0)
16676 {
16677 len += n;
16678 n = 0;
16679 }
16680 else if (n > slen)
16681 n = slen;
16682 if (len < 0)
16683 len = 0;
16684 else if (n + len > slen)
16685 len = slen - n;
16686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016687 rettv->v_type = VAR_STRING;
16688 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016689}
16690
16691/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016692 * "strridx()" function
16693 */
16694 static void
16695f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016696 typval_T *argvars;
16697 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016698{
16699 char_u buf[NUMBUFLEN];
16700 char_u *needle;
16701 char_u *haystack;
16702 char_u *rest;
16703 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016704 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016706 needle = get_tv_string_chk(&argvars[1]);
16707 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016708
16709 rettv->vval.v_number = -1;
16710 if (needle == NULL || haystack == NULL)
16711 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016712
16713 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016714 if (argvars[2].v_type != VAR_UNKNOWN)
16715 {
16716 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016717 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016718 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016719 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016720 }
16721 else
16722 end_idx = haystack_len;
16723
Bram Moolenaar0d660222005-01-07 21:51:51 +000016724 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016725 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016726 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016727 lastmatch = haystack + end_idx;
16728 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016729 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016730 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016731 for (rest = haystack; *rest != '\0'; ++rest)
16732 {
16733 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016734 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016735 break;
16736 lastmatch = rest;
16737 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016738 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016739
16740 if (lastmatch == NULL)
16741 rettv->vval.v_number = -1;
16742 else
16743 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16744}
16745
16746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016747 * "strtrans()" function
16748 */
16749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016750f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016751 typval_T *argvars;
16752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016754 rettv->v_type = VAR_STRING;
16755 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756}
16757
16758/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016759 * "submatch()" function
16760 */
16761 static void
16762f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016763 typval_T *argvars;
16764 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016765{
16766 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016767 rettv->vval.v_string =
16768 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016769}
16770
16771/*
16772 * "substitute()" function
16773 */
16774 static void
16775f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016776 typval_T *argvars;
16777 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016778{
16779 char_u patbuf[NUMBUFLEN];
16780 char_u subbuf[NUMBUFLEN];
16781 char_u flagsbuf[NUMBUFLEN];
16782
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016783 char_u *str = get_tv_string_chk(&argvars[0]);
16784 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16785 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16786 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16787
Bram Moolenaar0d660222005-01-07 21:51:51 +000016788 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016789 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16790 rettv->vval.v_string = NULL;
16791 else
16792 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016793}
16794
16795/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016796 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016799f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016800 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802{
16803 int id = 0;
16804#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016805 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806 long col;
16807 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016808 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016810 lnum = get_tv_lnum(argvars); /* -1 on type error */
16811 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16812 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016814 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016815 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016816 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817#endif
16818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016819 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820}
16821
16822/*
16823 * "synIDattr(id, what [, mode])" function
16824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016826f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016827 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829{
16830 char_u *p = NULL;
16831#ifdef FEAT_SYN_HL
16832 int id;
16833 char_u *what;
16834 char_u *mode;
16835 char_u modebuf[NUMBUFLEN];
16836 int modec;
16837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016838 id = get_tv_number(&argvars[0]);
16839 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016840 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016842 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843 modec = TOLOWER_ASC(mode[0]);
16844 if (modec != 't' && modec != 'c'
16845#ifdef FEAT_GUI
16846 && modec != 'g'
16847#endif
16848 )
16849 modec = 0; /* replace invalid with current */
16850 }
16851 else
16852 {
16853#ifdef FEAT_GUI
16854 if (gui.in_use)
16855 modec = 'g';
16856 else
16857#endif
16858 if (t_colors > 1)
16859 modec = 'c';
16860 else
16861 modec = 't';
16862 }
16863
16864
16865 switch (TOLOWER_ASC(what[0]))
16866 {
16867 case 'b':
16868 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16869 p = highlight_color(id, what, modec);
16870 else /* bold */
16871 p = highlight_has_attr(id, HL_BOLD, modec);
16872 break;
16873
Bram Moolenaar12682fd2010-03-10 13:43:49 +010016874 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875 p = highlight_color(id, what, modec);
16876 break;
16877
16878 case 'i':
16879 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16880 p = highlight_has_attr(id, HL_INVERSE, modec);
16881 else /* italic */
16882 p = highlight_has_attr(id, HL_ITALIC, modec);
16883 break;
16884
16885 case 'n': /* name */
16886 p = get_highlight_name(NULL, id - 1);
16887 break;
16888
16889 case 'r': /* reverse */
16890 p = highlight_has_attr(id, HL_INVERSE, modec);
16891 break;
16892
Bram Moolenaar6f507d62008-11-28 10:16:05 +000016893 case 's':
16894 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
16895 p = highlight_color(id, what, modec);
16896 else /* standout */
16897 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898 break;
16899
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016900 case 'u':
16901 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16902 /* underline */
16903 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16904 else
16905 /* undercurl */
16906 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 break;
16908 }
16909
16910 if (p != NULL)
16911 p = vim_strsave(p);
16912#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016913 rettv->v_type = VAR_STRING;
16914 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915}
16916
16917/*
16918 * "synIDtrans(id)" function
16919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016921f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016922 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924{
16925 int id;
16926
16927#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016928 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929
16930 if (id > 0)
16931 id = syn_get_final_id(id);
16932 else
16933#endif
16934 id = 0;
16935
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016936 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937}
16938
16939/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016940 * "synstack(lnum, col)" function
16941 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016942 static void
16943f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016944 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016945 typval_T *rettv;
16946{
16947#ifdef FEAT_SYN_HL
16948 long lnum;
16949 long col;
16950 int i;
16951 int id;
16952#endif
16953
16954 rettv->v_type = VAR_LIST;
16955 rettv->vval.v_list = NULL;
16956
16957#ifdef FEAT_SYN_HL
16958 lnum = get_tv_lnum(argvars); /* -1 on type error */
16959 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16960
16961 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar6cad8bd2008-09-10 13:39:10 +000016962 && col >= 0 && (col == 0 || col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016963 && rettv_list_alloc(rettv) != FAIL)
16964 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016965 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016966 for (i = 0; ; ++i)
16967 {
16968 id = syn_get_stack_item(i);
16969 if (id < 0)
16970 break;
16971 if (list_append_number(rettv->vval.v_list, id) == FAIL)
16972 break;
16973 }
16974 }
16975#endif
16976}
16977
16978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 * "system()" function
16980 */
16981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016982f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016983 typval_T *argvars;
16984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016986 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016988 char_u *infile = NULL;
16989 char_u buf[NUMBUFLEN];
16990 int err = FALSE;
16991 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016993 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016994 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016995
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016996 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016997 {
16998 /*
16999 * Write the string to a temp file, to be used for input of the shell
17000 * command.
17001 */
17002 if ((infile = vim_tempname('i')) == NULL)
17003 {
17004 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017005 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017006 }
17007
17008 fd = mch_fopen((char *)infile, WRITEBIN);
17009 if (fd == NULL)
17010 {
17011 EMSG2(_(e_notopen), infile);
17012 goto done;
17013 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017014 p = get_tv_string_buf_chk(&argvars[1], buf);
17015 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017016 {
17017 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017018 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017019 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017020 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17021 err = TRUE;
17022 if (fclose(fd) != 0)
17023 err = TRUE;
17024 if (err)
17025 {
17026 EMSG(_("E677: Error writing temp file"));
17027 goto done;
17028 }
17029 }
17030
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017031 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17032 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017033
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034#ifdef USE_CR
17035 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017036 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037 {
17038 char_u *s;
17039
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017040 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041 {
17042 if (*s == CAR)
17043 *s = NL;
17044 }
17045 }
17046#else
17047# ifdef USE_CRNL
17048 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017049 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017050 {
17051 char_u *s, *d;
17052
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017053 d = res;
17054 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055 {
17056 if (s[0] == CAR && s[1] == NL)
17057 ++s;
17058 *d++ = *s;
17059 }
17060 *d = NUL;
17061 }
17062# endif
17063#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017064
17065done:
17066 if (infile != NULL)
17067 {
17068 mch_remove(infile);
17069 vim_free(infile);
17070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017071 rettv->v_type = VAR_STRING;
17072 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017073}
17074
17075/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017076 * "tabpagebuflist()" function
17077 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017078 static void
17079f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017080 typval_T *argvars UNUSED;
17081 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017082{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017083#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017084 tabpage_T *tp;
17085 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017086
17087 if (argvars[0].v_type == VAR_UNKNOWN)
17088 wp = firstwin;
17089 else
17090 {
17091 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17092 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017093 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017094 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017095 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017096 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017097 for (; wp != NULL; wp = wp->w_next)
17098 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017099 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017100 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017101 }
17102#endif
17103}
17104
17105
17106/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017107 * "tabpagenr()" function
17108 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017109 static void
17110f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017111 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017112 typval_T *rettv;
17113{
17114 int nr = 1;
17115#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017116 char_u *arg;
17117
17118 if (argvars[0].v_type != VAR_UNKNOWN)
17119 {
17120 arg = get_tv_string_chk(&argvars[0]);
17121 nr = 0;
17122 if (arg != NULL)
17123 {
17124 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017125 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017126 else
17127 EMSG2(_(e_invexpr2), arg);
17128 }
17129 }
17130 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017131 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017132#endif
17133 rettv->vval.v_number = nr;
17134}
17135
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017136
17137#ifdef FEAT_WINDOWS
17138static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17139
17140/*
17141 * Common code for tabpagewinnr() and winnr().
17142 */
17143 static int
17144get_winnr(tp, argvar)
17145 tabpage_T *tp;
17146 typval_T *argvar;
17147{
17148 win_T *twin;
17149 int nr = 1;
17150 win_T *wp;
17151 char_u *arg;
17152
17153 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17154 if (argvar->v_type != VAR_UNKNOWN)
17155 {
17156 arg = get_tv_string_chk(argvar);
17157 if (arg == NULL)
17158 nr = 0; /* type error; errmsg already given */
17159 else if (STRCMP(arg, "$") == 0)
17160 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17161 else if (STRCMP(arg, "#") == 0)
17162 {
17163 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17164 if (twin == NULL)
17165 nr = 0;
17166 }
17167 else
17168 {
17169 EMSG2(_(e_invexpr2), arg);
17170 nr = 0;
17171 }
17172 }
17173
17174 if (nr > 0)
17175 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17176 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017177 {
17178 if (wp == NULL)
17179 {
17180 /* didn't find it in this tabpage */
17181 nr = 0;
17182 break;
17183 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017184 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017185 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017186 return nr;
17187}
17188#endif
17189
17190/*
17191 * "tabpagewinnr()" function
17192 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017193 static void
17194f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017195 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017196 typval_T *rettv;
17197{
17198 int nr = 1;
17199#ifdef FEAT_WINDOWS
17200 tabpage_T *tp;
17201
17202 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17203 if (tp == NULL)
17204 nr = 0;
17205 else
17206 nr = get_winnr(tp, &argvars[1]);
17207#endif
17208 rettv->vval.v_number = nr;
17209}
17210
17211
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017212/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017213 * "tagfiles()" function
17214 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017215 static void
17216f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017217 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017218 typval_T *rettv;
17219{
17220 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017221 tagname_T tn;
17222 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017223
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017224 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017225 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017226
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017227 for (first = TRUE; ; first = FALSE)
17228 if (get_tagfname(&tn, first, fname) == FAIL
17229 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017230 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017231 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017232}
17233
17234/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017235 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017236 */
17237 static void
17238f_taglist(argvars, rettv)
17239 typval_T *argvars;
17240 typval_T *rettv;
17241{
17242 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017243
17244 tag_pattern = get_tv_string(&argvars[0]);
17245
17246 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017247 if (*tag_pattern == NUL)
17248 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017249
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017250 if (rettv_list_alloc(rettv) == OK)
17251 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017252}
17253
17254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017255 * "tempname()" function
17256 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017258f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017259 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261{
17262 static int x = 'A';
17263
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017264 rettv->v_type = VAR_STRING;
17265 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266
17267 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17268 * names. Skip 'I' and 'O', they are used for shell redirection. */
17269 do
17270 {
17271 if (x == 'Z')
17272 x = '0';
17273 else if (x == '9')
17274 x = 'A';
17275 else
17276 {
17277#ifdef EBCDIC
17278 if (x == 'I')
17279 x = 'J';
17280 else if (x == 'R')
17281 x = 'S';
17282 else
17283#endif
17284 ++x;
17285 }
17286 } while (x == 'I' || x == 'O');
17287}
17288
17289/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017290 * "test(list)" function: Just checking the walls...
17291 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017292 static void
17293f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017294 typval_T *argvars UNUSED;
17295 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017296{
17297 /* Used for unit testing. Change the code below to your liking. */
17298#if 0
17299 listitem_T *li;
17300 list_T *l;
17301 char_u *bad, *good;
17302
17303 if (argvars[0].v_type != VAR_LIST)
17304 return;
17305 l = argvars[0].vval.v_list;
17306 if (l == NULL)
17307 return;
17308 li = l->lv_first;
17309 if (li == NULL)
17310 return;
17311 bad = get_tv_string(&li->li_tv);
17312 li = li->li_next;
17313 if (li == NULL)
17314 return;
17315 good = get_tv_string(&li->li_tv);
17316 rettv->vval.v_number = test_edit_score(bad, good);
17317#endif
17318}
17319
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017320#ifdef FEAT_FLOAT
17321/*
17322 * "tan()" function
17323 */
17324 static void
17325f_tan(argvars, rettv)
17326 typval_T *argvars;
17327 typval_T *rettv;
17328{
17329 float_T f;
17330
17331 rettv->v_type = VAR_FLOAT;
17332 if (get_float_arg(argvars, &f) == OK)
17333 rettv->vval.v_float = tan(f);
17334 else
17335 rettv->vval.v_float = 0.0;
17336}
17337
17338/*
17339 * "tanh()" function
17340 */
17341 static void
17342f_tanh(argvars, rettv)
17343 typval_T *argvars;
17344 typval_T *rettv;
17345{
17346 float_T f;
17347
17348 rettv->v_type = VAR_FLOAT;
17349 if (get_float_arg(argvars, &f) == OK)
17350 rettv->vval.v_float = tanh(f);
17351 else
17352 rettv->vval.v_float = 0.0;
17353}
17354#endif
17355
Bram Moolenaard52d9742005-08-21 22:20:28 +000017356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 * "tolower(string)" function
17358 */
17359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017360f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017361 typval_T *argvars;
17362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363{
17364 char_u *p;
17365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017366 p = vim_strsave(get_tv_string(&argvars[0]));
17367 rettv->v_type = VAR_STRING;
17368 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369
17370 if (p != NULL)
17371 while (*p != NUL)
17372 {
17373#ifdef FEAT_MBYTE
17374 int l;
17375
17376 if (enc_utf8)
17377 {
17378 int c, lc;
17379
17380 c = utf_ptr2char(p);
17381 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017382 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 /* TODO: reallocate string when byte count changes. */
17384 if (utf_char2len(lc) == l)
17385 utf_char2bytes(lc, p);
17386 p += l;
17387 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017388 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389 p += l; /* skip multi-byte character */
17390 else
17391#endif
17392 {
17393 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17394 ++p;
17395 }
17396 }
17397}
17398
17399/*
17400 * "toupper(string)" function
17401 */
17402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017403f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017404 typval_T *argvars;
17405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017407 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017408 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409}
17410
17411/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017412 * "tr(string, fromstr, tostr)" function
17413 */
17414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017415f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017416 typval_T *argvars;
17417 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017418{
17419 char_u *instr;
17420 char_u *fromstr;
17421 char_u *tostr;
17422 char_u *p;
17423#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017424 int inlen;
17425 int fromlen;
17426 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017427 int idx;
17428 char_u *cpstr;
17429 int cplen;
17430 int first = TRUE;
17431#endif
17432 char_u buf[NUMBUFLEN];
17433 char_u buf2[NUMBUFLEN];
17434 garray_T ga;
17435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017436 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017437 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17438 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017439
17440 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017441 rettv->v_type = VAR_STRING;
17442 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017443 if (fromstr == NULL || tostr == NULL)
17444 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017445 ga_init2(&ga, (int)sizeof(char), 80);
17446
17447#ifdef FEAT_MBYTE
17448 if (!has_mbyte)
17449#endif
17450 /* not multi-byte: fromstr and tostr must be the same length */
17451 if (STRLEN(fromstr) != STRLEN(tostr))
17452 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017453#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017454error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017455#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017456 EMSG2(_(e_invarg2), fromstr);
17457 ga_clear(&ga);
17458 return;
17459 }
17460
17461 /* fromstr and tostr have to contain the same number of chars */
17462 while (*instr != NUL)
17463 {
17464#ifdef FEAT_MBYTE
17465 if (has_mbyte)
17466 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017467 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017468 cpstr = instr;
17469 cplen = inlen;
17470 idx = 0;
17471 for (p = fromstr; *p != NUL; p += fromlen)
17472 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017473 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017474 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17475 {
17476 for (p = tostr; *p != NUL; p += tolen)
17477 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017478 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017479 if (idx-- == 0)
17480 {
17481 cplen = tolen;
17482 cpstr = p;
17483 break;
17484 }
17485 }
17486 if (*p == NUL) /* tostr is shorter than fromstr */
17487 goto error;
17488 break;
17489 }
17490 ++idx;
17491 }
17492
17493 if (first && cpstr == instr)
17494 {
17495 /* Check that fromstr and tostr have the same number of
17496 * (multi-byte) characters. Done only once when a character
17497 * of instr doesn't appear in fromstr. */
17498 first = FALSE;
17499 for (p = tostr; *p != NUL; p += tolen)
17500 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017501 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017502 --idx;
17503 }
17504 if (idx != 0)
17505 goto error;
17506 }
17507
17508 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017509 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017510 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017511
17512 instr += inlen;
17513 }
17514 else
17515#endif
17516 {
17517 /* When not using multi-byte chars we can do it faster. */
17518 p = vim_strchr(fromstr, *instr);
17519 if (p != NULL)
17520 ga_append(&ga, tostr[p - fromstr]);
17521 else
17522 ga_append(&ga, *instr);
17523 ++instr;
17524 }
17525 }
17526
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017527 /* add a terminating NUL */
17528 ga_grow(&ga, 1);
17529 ga_append(&ga, NUL);
17530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017531 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017532}
17533
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017534#ifdef FEAT_FLOAT
17535/*
17536 * "trunc({float})" function
17537 */
17538 static void
17539f_trunc(argvars, rettv)
17540 typval_T *argvars;
17541 typval_T *rettv;
17542{
17543 float_T f;
17544
17545 rettv->v_type = VAR_FLOAT;
17546 if (get_float_arg(argvars, &f) == OK)
17547 /* trunc() is not in C90, use floor() or ceil() instead. */
17548 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17549 else
17550 rettv->vval.v_float = 0.0;
17551}
17552#endif
17553
Bram Moolenaar8299df92004-07-10 09:47:34 +000017554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555 * "type(expr)" function
17556 */
17557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017558f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017559 typval_T *argvars;
17560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017562 int n;
17563
17564 switch (argvars[0].v_type)
17565 {
17566 case VAR_NUMBER: n = 0; break;
17567 case VAR_STRING: n = 1; break;
17568 case VAR_FUNC: n = 2; break;
17569 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017570 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017571#ifdef FEAT_FLOAT
17572 case VAR_FLOAT: n = 5; break;
17573#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017574 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17575 }
17576 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577}
17578
17579/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017580 * "values(dict)" function
17581 */
17582 static void
17583f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017584 typval_T *argvars;
17585 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017586{
17587 dict_list(argvars, rettv, 1);
17588}
17589
17590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017591 * "virtcol(string)" function
17592 */
17593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017594f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017595 typval_T *argvars;
17596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597{
17598 colnr_T vcol = 0;
17599 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017600 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017602 fp = var2fpos(&argvars[0], FALSE, &fnum);
17603 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17604 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605 {
17606 getvvcol(curwin, fp, NULL, NULL, &vcol);
17607 ++vcol;
17608 }
17609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017610 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611}
17612
17613/*
17614 * "visualmode()" function
17615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017617f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017618 typval_T *argvars UNUSED;
17619 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620{
17621#ifdef FEAT_VISUAL
17622 char_u str[2];
17623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017624 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625 str[0] = curbuf->b_visual_mode_eval;
17626 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017627 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628
17629 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017630 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632#endif
17633}
17634
17635/*
17636 * "winbufnr(nr)" function
17637 */
17638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017639f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017640 typval_T *argvars;
17641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642{
17643 win_T *wp;
17644
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017645 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017647 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017649 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650}
17651
17652/*
17653 * "wincol()" function
17654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017656f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017657 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659{
17660 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017661 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662}
17663
17664/*
17665 * "winheight(nr)" function
17666 */
17667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017668f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017669 typval_T *argvars;
17670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671{
17672 win_T *wp;
17673
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017674 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017676 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017678 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679}
17680
17681/*
17682 * "winline()" function
17683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017685f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017686 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688{
17689 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017690 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691}
17692
17693/*
17694 * "winnr()" function
17695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017697f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017698 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700{
17701 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017702
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017704 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017706 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707}
17708
17709/*
17710 * "winrestcmd()" function
17711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017713f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017714 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716{
17717#ifdef FEAT_WINDOWS
17718 win_T *wp;
17719 int winnr = 1;
17720 garray_T ga;
17721 char_u buf[50];
17722
17723 ga_init2(&ga, (int)sizeof(char), 70);
17724 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17725 {
17726 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17727 ga_concat(&ga, buf);
17728# ifdef FEAT_VERTSPLIT
17729 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17730 ga_concat(&ga, buf);
17731# endif
17732 ++winnr;
17733 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017734 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017736 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017738 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017740 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741}
17742
17743/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017744 * "winrestview()" function
17745 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017746 static void
17747f_winrestview(argvars, rettv)
17748 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017749 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017750{
17751 dict_T *dict;
17752
17753 if (argvars[0].v_type != VAR_DICT
17754 || (dict = argvars[0].vval.v_dict) == NULL)
17755 EMSG(_(e_invarg));
17756 else
17757 {
17758 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17759 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17760#ifdef FEAT_VIRTUALEDIT
17761 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17762#endif
17763 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017764 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017765
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017766 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017767#ifdef FEAT_DIFF
17768 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17769#endif
17770 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17771 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17772
17773 check_cursor();
17774 changed_cline_bef_curs();
17775 invalidate_botline();
17776 redraw_later(VALID);
17777
17778 if (curwin->w_topline == 0)
17779 curwin->w_topline = 1;
17780 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17781 curwin->w_topline = curbuf->b_ml.ml_line_count;
17782#ifdef FEAT_DIFF
17783 check_topfill(curwin, TRUE);
17784#endif
17785 }
17786}
17787
17788/*
17789 * "winsaveview()" function
17790 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017791 static void
17792f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017793 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017794 typval_T *rettv;
17795{
17796 dict_T *dict;
17797
17798 dict = dict_alloc();
17799 if (dict == NULL)
17800 return;
17801 rettv->v_type = VAR_DICT;
17802 rettv->vval.v_dict = dict;
17803 ++dict->dv_refcount;
17804
17805 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17806 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17807#ifdef FEAT_VIRTUALEDIT
17808 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17809#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017810 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017811 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17812
17813 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17814#ifdef FEAT_DIFF
17815 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17816#endif
17817 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17818 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17819}
17820
17821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017822 * "winwidth(nr)" function
17823 */
17824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017825f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017826 typval_T *argvars;
17827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828{
17829 win_T *wp;
17830
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017831 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017833 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834 else
17835#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017836 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017838 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839#endif
17840}
17841
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017843 * "writefile()" function
17844 */
17845 static void
17846f_writefile(argvars, rettv)
17847 typval_T *argvars;
17848 typval_T *rettv;
17849{
17850 int binary = FALSE;
17851 char_u *fname;
17852 FILE *fd;
17853 listitem_T *li;
17854 char_u *s;
17855 int ret = 0;
17856 int c;
17857
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017858 if (check_restricted() || check_secure())
17859 return;
17860
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017861 if (argvars[0].v_type != VAR_LIST)
17862 {
17863 EMSG2(_(e_listarg), "writefile()");
17864 return;
17865 }
17866 if (argvars[0].vval.v_list == NULL)
17867 return;
17868
17869 if (argvars[2].v_type != VAR_UNKNOWN
17870 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17871 binary = TRUE;
17872
17873 /* Always open the file in binary mode, library functions have a mind of
17874 * their own about CR-LF conversion. */
17875 fname = get_tv_string(&argvars[1]);
17876 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17877 {
17878 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17879 ret = -1;
17880 }
17881 else
17882 {
17883 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17884 li = li->li_next)
17885 {
17886 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17887 {
17888 if (*s == '\n')
17889 c = putc(NUL, fd);
17890 else
17891 c = putc(*s, fd);
17892 if (c == EOF)
17893 {
17894 ret = -1;
17895 break;
17896 }
17897 }
17898 if (!binary || li->li_next != NULL)
17899 if (putc('\n', fd) == EOF)
17900 {
17901 ret = -1;
17902 break;
17903 }
17904 if (ret < 0)
17905 {
17906 EMSG(_(e_write));
17907 break;
17908 }
17909 }
17910 fclose(fd);
17911 }
17912
17913 rettv->vval.v_number = ret;
17914}
17915
17916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017918 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 */
17920 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017921var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017922 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017923 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017924 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017926 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017928 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017929
Bram Moolenaara5525202006-03-02 22:52:09 +000017930 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017931 if (varp->v_type == VAR_LIST)
17932 {
17933 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017934 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000017935 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017936 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017937
17938 l = varp->vval.v_list;
17939 if (l == NULL)
17940 return NULL;
17941
17942 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017943 pos.lnum = list_find_nr(l, 0L, &error);
17944 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017945 return NULL; /* invalid line number */
17946
17947 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017948 pos.col = list_find_nr(l, 1L, &error);
17949 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017950 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017951 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000017952
17953 /* We accept "$" for the column number: last column. */
17954 li = list_find(l, 1L);
17955 if (li != NULL && li->li_tv.v_type == VAR_STRING
17956 && li->li_tv.vval.v_string != NULL
17957 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
17958 pos.col = len + 1;
17959
Bram Moolenaara5525202006-03-02 22:52:09 +000017960 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000017961 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017962 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017963 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017964
Bram Moolenaara5525202006-03-02 22:52:09 +000017965#ifdef FEAT_VIRTUALEDIT
17966 /* Get the virtual offset. Defaults to zero. */
17967 pos.coladd = list_find_nr(l, 2L, &error);
17968 if (error)
17969 pos.coladd = 0;
17970#endif
17971
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017972 return &pos;
17973 }
17974
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017975 name = get_tv_string_chk(varp);
17976 if (name == NULL)
17977 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017978 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017980#ifdef FEAT_VISUAL
17981 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
17982 {
17983 if (VIsual_active)
17984 return &VIsual;
17985 return &curwin->w_cursor;
17986 }
17987#endif
17988 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017990 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
17992 return NULL;
17993 return pp;
17994 }
Bram Moolenaara5525202006-03-02 22:52:09 +000017995
17996#ifdef FEAT_VIRTUALEDIT
17997 pos.coladd = 0;
17998#endif
17999
Bram Moolenaar477933c2007-07-17 14:32:23 +000018000 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018001 {
18002 pos.col = 0;
18003 if (name[1] == '0') /* "w0": first visible line */
18004 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018005 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018006 pos.lnum = curwin->w_topline;
18007 return &pos;
18008 }
18009 else if (name[1] == '$') /* "w$": last visible line */
18010 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018011 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018012 pos.lnum = curwin->w_botline - 1;
18013 return &pos;
18014 }
18015 }
18016 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018018 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019 {
18020 pos.lnum = curbuf->b_ml.ml_line_count;
18021 pos.col = 0;
18022 }
18023 else
18024 {
18025 pos.lnum = curwin->w_cursor.lnum;
18026 pos.col = (colnr_T)STRLEN(ml_get_curline());
18027 }
18028 return &pos;
18029 }
18030 return NULL;
18031}
18032
18033/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018034 * Convert list in "arg" into a position and optional file number.
18035 * When "fnump" is NULL there is no file number, only 3 items.
18036 * Note that the column is passed on as-is, the caller may want to decrement
18037 * it to use 1 for the first column.
18038 * Return FAIL when conversion is not possible, doesn't check the position for
18039 * validity.
18040 */
18041 static int
18042list2fpos(arg, posp, fnump)
18043 typval_T *arg;
18044 pos_T *posp;
18045 int *fnump;
18046{
18047 list_T *l = arg->vval.v_list;
18048 long i = 0;
18049 long n;
18050
Bram Moolenaarbde35262006-07-23 20:12:24 +000018051 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18052 * when "fnump" isn't NULL and "coladd" is optional. */
18053 if (arg->v_type != VAR_LIST
18054 || l == NULL
18055 || l->lv_len < (fnump == NULL ? 2 : 3)
18056 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018057 return FAIL;
18058
18059 if (fnump != NULL)
18060 {
18061 n = list_find_nr(l, i++, NULL); /* fnum */
18062 if (n < 0)
18063 return FAIL;
18064 if (n == 0)
18065 n = curbuf->b_fnum; /* current buffer */
18066 *fnump = n;
18067 }
18068
18069 n = list_find_nr(l, i++, NULL); /* lnum */
18070 if (n < 0)
18071 return FAIL;
18072 posp->lnum = n;
18073
18074 n = list_find_nr(l, i++, NULL); /* col */
18075 if (n < 0)
18076 return FAIL;
18077 posp->col = n;
18078
18079#ifdef FEAT_VIRTUALEDIT
18080 n = list_find_nr(l, i, NULL);
18081 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018082 posp->coladd = 0;
18083 else
18084 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018085#endif
18086
18087 return OK;
18088}
18089
18090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091 * Get the length of an environment variable name.
18092 * Advance "arg" to the first character after the name.
18093 * Return 0 for error.
18094 */
18095 static int
18096get_env_len(arg)
18097 char_u **arg;
18098{
18099 char_u *p;
18100 int len;
18101
18102 for (p = *arg; vim_isIDc(*p); ++p)
18103 ;
18104 if (p == *arg) /* no name found */
18105 return 0;
18106
18107 len = (int)(p - *arg);
18108 *arg = p;
18109 return len;
18110}
18111
18112/*
18113 * Get the length of the name of a function or internal variable.
18114 * "arg" is advanced to the first non-white character after the name.
18115 * Return 0 if something is wrong.
18116 */
18117 static int
18118get_id_len(arg)
18119 char_u **arg;
18120{
18121 char_u *p;
18122 int len;
18123
18124 /* Find the end of the name. */
18125 for (p = *arg; eval_isnamec(*p); ++p)
18126 ;
18127 if (p == *arg) /* no name found */
18128 return 0;
18129
18130 len = (int)(p - *arg);
18131 *arg = skipwhite(p);
18132
18133 return len;
18134}
18135
18136/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018137 * Get the length of the name of a variable or function.
18138 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018140 * Return -1 if curly braces expansion failed.
18141 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142 * If the name contains 'magic' {}'s, expand them and return the
18143 * expanded name in an allocated string via 'alias' - caller must free.
18144 */
18145 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018146get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 char_u **arg;
18148 char_u **alias;
18149 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018150 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151{
18152 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153 char_u *p;
18154 char_u *expr_start;
18155 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156
18157 *alias = NULL; /* default to no alias */
18158
18159 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18160 && (*arg)[2] == (int)KE_SNR)
18161 {
18162 /* hard coded <SNR>, already translated */
18163 *arg += 3;
18164 return get_id_len(arg) + 3;
18165 }
18166 len = eval_fname_script(*arg);
18167 if (len > 0)
18168 {
18169 /* literal "<SID>", "s:" or "<SNR>" */
18170 *arg += len;
18171 }
18172
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018174 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018176 p = find_name_end(*arg, &expr_start, &expr_end,
18177 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178 if (expr_start != NULL)
18179 {
18180 char_u *temp_string;
18181
18182 if (!evaluate)
18183 {
18184 len += (int)(p - *arg);
18185 *arg = skipwhite(p);
18186 return len;
18187 }
18188
18189 /*
18190 * Include any <SID> etc in the expanded string:
18191 * Thus the -len here.
18192 */
18193 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18194 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018195 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196 *alias = temp_string;
18197 *arg = skipwhite(p);
18198 return (int)STRLEN(temp_string);
18199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200
18201 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018202 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203 EMSG2(_(e_invexpr2), *arg);
18204
18205 return len;
18206}
18207
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018208/*
18209 * Find the end of a variable or function name, taking care of magic braces.
18210 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18211 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018212 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018213 * Return a pointer to just after the name. Equal to "arg" if there is no
18214 * valid name.
18215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018217find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218 char_u *arg;
18219 char_u **expr_start;
18220 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018221 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018223 int mb_nest = 0;
18224 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225 char_u *p;
18226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018227 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018229 *expr_start = NULL;
18230 *expr_end = NULL;
18231 }
18232
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018233 /* Quick check for valid starting character. */
18234 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18235 return arg;
18236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018237 for (p = arg; *p != NUL
18238 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018239 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018240 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018241 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018242 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018243 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018244 if (*p == '\'')
18245 {
18246 /* skip over 'string' to avoid counting [ and ] inside it. */
18247 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18248 ;
18249 if (*p == NUL)
18250 break;
18251 }
18252 else if (*p == '"')
18253 {
18254 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18255 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18256 if (*p == '\\' && p[1] != NUL)
18257 ++p;
18258 if (*p == NUL)
18259 break;
18260 }
18261
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018262 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018264 if (*p == '[')
18265 ++br_nest;
18266 else if (*p == ']')
18267 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018270 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018272 if (*p == '{')
18273 {
18274 mb_nest++;
18275 if (expr_start != NULL && *expr_start == NULL)
18276 *expr_start = p;
18277 }
18278 else if (*p == '}')
18279 {
18280 mb_nest--;
18281 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18282 *expr_end = p;
18283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018285 }
18286
18287 return p;
18288}
18289
18290/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018291 * Expands out the 'magic' {}'s in a variable/function name.
18292 * Note that this can call itself recursively, to deal with
18293 * constructs like foo{bar}{baz}{bam}
18294 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18295 * "in_start" ^
18296 * "expr_start" ^
18297 * "expr_end" ^
18298 * "in_end" ^
18299 *
18300 * Returns a new allocated string, which the caller must free.
18301 * Returns NULL for failure.
18302 */
18303 static char_u *
18304make_expanded_name(in_start, expr_start, expr_end, in_end)
18305 char_u *in_start;
18306 char_u *expr_start;
18307 char_u *expr_end;
18308 char_u *in_end;
18309{
18310 char_u c1;
18311 char_u *retval = NULL;
18312 char_u *temp_result;
18313 char_u *nextcmd = NULL;
18314
18315 if (expr_end == NULL || in_end == NULL)
18316 return NULL;
18317 *expr_start = NUL;
18318 *expr_end = NUL;
18319 c1 = *in_end;
18320 *in_end = NUL;
18321
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018322 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018323 if (temp_result != NULL && nextcmd == NULL)
18324 {
18325 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18326 + (in_end - expr_end) + 1));
18327 if (retval != NULL)
18328 {
18329 STRCPY(retval, in_start);
18330 STRCAT(retval, temp_result);
18331 STRCAT(retval, expr_end + 1);
18332 }
18333 }
18334 vim_free(temp_result);
18335
18336 *in_end = c1; /* put char back for error messages */
18337 *expr_start = '{';
18338 *expr_end = '}';
18339
18340 if (retval != NULL)
18341 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018342 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018343 if (expr_start != NULL)
18344 {
18345 /* Further expansion! */
18346 temp_result = make_expanded_name(retval, expr_start,
18347 expr_end, temp_result);
18348 vim_free(retval);
18349 retval = temp_result;
18350 }
18351 }
18352
18353 return retval;
18354}
18355
18356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018358 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359 */
18360 static int
18361eval_isnamec(c)
18362 int c;
18363{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018364 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18365}
18366
18367/*
18368 * Return TRUE if character "c" can be used as the first character in a
18369 * variable or function name (excluding '{' and '}').
18370 */
18371 static int
18372eval_isnamec1(c)
18373 int c;
18374{
18375 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018376}
18377
18378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379 * Set number v: variable to "val".
18380 */
18381 void
18382set_vim_var_nr(idx, val)
18383 int idx;
18384 long val;
18385{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018386 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387}
18388
18389/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018390 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391 */
18392 long
18393get_vim_var_nr(idx)
18394 int idx;
18395{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018396 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397}
18398
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018399/*
18400 * Get string v: variable value. Uses a static buffer, can only be used once.
18401 */
18402 char_u *
18403get_vim_var_str(idx)
18404 int idx;
18405{
18406 return get_tv_string(&vimvars[idx].vv_tv);
18407}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018408
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018410 * Get List v: variable value. Caller must take care of reference count when
18411 * needed.
18412 */
18413 list_T *
18414get_vim_var_list(idx)
18415 int idx;
18416{
18417 return vimvars[idx].vv_list;
18418}
18419
18420/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018421 * Set v:char to character "c".
18422 */
18423 void
18424set_vim_var_char(c)
18425 int c;
18426{
18427#ifdef FEAT_MBYTE
18428 char_u buf[MB_MAXBYTES];
18429#else
18430 char_u buf[2];
18431#endif
18432
18433#ifdef FEAT_MBYTE
18434 if (has_mbyte)
18435 buf[(*mb_char2bytes)(c, buf)] = NUL;
18436 else
18437#endif
18438 {
18439 buf[0] = c;
18440 buf[1] = NUL;
18441 }
18442 set_vim_var_string(VV_CHAR, buf, -1);
18443}
18444
18445/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018446 * Set v:count to "count" and v:count1 to "count1".
18447 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448 */
18449 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018450set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451 long count;
18452 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018453 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018454{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018455 if (set_prevcount)
18456 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018457 vimvars[VV_COUNT].vv_nr = count;
18458 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459}
18460
18461/*
18462 * Set string v: variable to a copy of "val".
18463 */
18464 void
18465set_vim_var_string(idx, val, len)
18466 int idx;
18467 char_u *val;
18468 int len; /* length of "val" to use or -1 (whole string) */
18469{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018470 /* Need to do this (at least) once, since we can't initialize a union.
18471 * Will always be invoked when "v:progname" is set. */
18472 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18473
Bram Moolenaare9a41262005-01-15 22:18:47 +000018474 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018476 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018478 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018480 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481}
18482
18483/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018484 * Set List v: variable to "val".
18485 */
18486 void
18487set_vim_var_list(idx, val)
18488 int idx;
18489 list_T *val;
18490{
18491 list_unref(vimvars[idx].vv_list);
18492 vimvars[idx].vv_list = val;
18493 if (val != NULL)
18494 ++val->lv_refcount;
18495}
18496
18497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 * Set v:register if needed.
18499 */
18500 void
18501set_reg_var(c)
18502 int c;
18503{
18504 char_u regname;
18505
18506 if (c == 0 || c == ' ')
18507 regname = '"';
18508 else
18509 regname = c;
18510 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018511 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512 set_vim_var_string(VV_REG, &regname, 1);
18513}
18514
18515/*
18516 * Get or set v:exception. If "oldval" == NULL, return the current value.
18517 * Otherwise, restore the value to "oldval" and return NULL.
18518 * Must always be called in pairs to save and restore v:exception! Does not
18519 * take care of memory allocations.
18520 */
18521 char_u *
18522v_exception(oldval)
18523 char_u *oldval;
18524{
18525 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018526 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527
Bram Moolenaare9a41262005-01-15 22:18:47 +000018528 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 return NULL;
18530}
18531
18532/*
18533 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18534 * Otherwise, restore the value to "oldval" and return NULL.
18535 * Must always be called in pairs to save and restore v:throwpoint! Does not
18536 * take care of memory allocations.
18537 */
18538 char_u *
18539v_throwpoint(oldval)
18540 char_u *oldval;
18541{
18542 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018543 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544
Bram Moolenaare9a41262005-01-15 22:18:47 +000018545 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546 return NULL;
18547}
18548
18549#if defined(FEAT_AUTOCMD) || defined(PROTO)
18550/*
18551 * Set v:cmdarg.
18552 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18553 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18554 * Must always be called in pairs!
18555 */
18556 char_u *
18557set_cmdarg(eap, oldarg)
18558 exarg_T *eap;
18559 char_u *oldarg;
18560{
18561 char_u *oldval;
18562 char_u *newval;
18563 unsigned len;
18564
Bram Moolenaare9a41262005-01-15 22:18:47 +000018565 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018566 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018568 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018569 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018570 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571 }
18572
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018573 if (eap->force_bin == FORCE_BIN)
18574 len = 6;
18575 else if (eap->force_bin == FORCE_NOBIN)
18576 len = 8;
18577 else
18578 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018579
18580 if (eap->read_edit)
18581 len += 7;
18582
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018583 if (eap->force_ff != 0)
18584 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18585# ifdef FEAT_MBYTE
18586 if (eap->force_enc != 0)
18587 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018588 if (eap->bad_char != 0)
18589 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018590# endif
18591
18592 newval = alloc(len + 1);
18593 if (newval == NULL)
18594 return NULL;
18595
18596 if (eap->force_bin == FORCE_BIN)
18597 sprintf((char *)newval, " ++bin");
18598 else if (eap->force_bin == FORCE_NOBIN)
18599 sprintf((char *)newval, " ++nobin");
18600 else
18601 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018602
18603 if (eap->read_edit)
18604 STRCAT(newval, " ++edit");
18605
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018606 if (eap->force_ff != 0)
18607 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18608 eap->cmd + eap->force_ff);
18609# ifdef FEAT_MBYTE
18610 if (eap->force_enc != 0)
18611 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18612 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018613 if (eap->bad_char == BAD_KEEP)
18614 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18615 else if (eap->bad_char == BAD_DROP)
18616 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18617 else if (eap->bad_char != 0)
18618 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018619# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018620 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018621 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622}
18623#endif
18624
18625/*
18626 * Get the value of internal variable "name".
18627 * Return OK or FAIL.
18628 */
18629 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018630get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631 char_u *name;
18632 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018633 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018634 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635{
18636 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018637 typval_T *tv = NULL;
18638 typval_T atv;
18639 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641
18642 /* truncate the name, so that we can use strcmp() */
18643 cc = name[len];
18644 name[len] = NUL;
18645
18646 /*
18647 * Check for "b:changedtick".
18648 */
18649 if (STRCMP(name, "b:changedtick") == 0)
18650 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018651 atv.v_type = VAR_NUMBER;
18652 atv.vval.v_number = curbuf->b_changedtick;
18653 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654 }
18655
18656 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 * Check for user-defined variables.
18658 */
18659 else
18660 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018661 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018663 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 }
18665
Bram Moolenaare9a41262005-01-15 22:18:47 +000018666 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018668 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018669 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 ret = FAIL;
18671 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018672 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018673 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674
18675 name[len] = cc;
18676
18677 return ret;
18678}
18679
18680/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018681 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18682 * Also handle function call with Funcref variable: func(expr)
18683 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18684 */
18685 static int
18686handle_subscript(arg, rettv, evaluate, verbose)
18687 char_u **arg;
18688 typval_T *rettv;
18689 int evaluate; /* do more than finding the end */
18690 int verbose; /* give error messages */
18691{
18692 int ret = OK;
18693 dict_T *selfdict = NULL;
18694 char_u *s;
18695 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018696 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018697
18698 while (ret == OK
18699 && (**arg == '['
18700 || (**arg == '.' && rettv->v_type == VAR_DICT)
18701 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18702 && !vim_iswhite(*(*arg - 1)))
18703 {
18704 if (**arg == '(')
18705 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018706 /* need to copy the funcref so that we can clear rettv */
18707 functv = *rettv;
18708 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018709
18710 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018711 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018712 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018713 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18714 &len, evaluate, selfdict);
18715
18716 /* Clear the funcref afterwards, so that deleting it while
18717 * evaluating the arguments is possible (see test55). */
18718 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018719
18720 /* Stop the expression evaluation when immediately aborting on
18721 * error, or when an interrupt occurred or an exception was thrown
18722 * but not caught. */
18723 if (aborting())
18724 {
18725 if (ret == OK)
18726 clear_tv(rettv);
18727 ret = FAIL;
18728 }
18729 dict_unref(selfdict);
18730 selfdict = NULL;
18731 }
18732 else /* **arg == '[' || **arg == '.' */
18733 {
18734 dict_unref(selfdict);
18735 if (rettv->v_type == VAR_DICT)
18736 {
18737 selfdict = rettv->vval.v_dict;
18738 if (selfdict != NULL)
18739 ++selfdict->dv_refcount;
18740 }
18741 else
18742 selfdict = NULL;
18743 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18744 {
18745 clear_tv(rettv);
18746 ret = FAIL;
18747 }
18748 }
18749 }
18750 dict_unref(selfdict);
18751 return ret;
18752}
18753
18754/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018755 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018756 * value).
18757 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018758 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018759alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018760{
Bram Moolenaar33570922005-01-25 22:26:29 +000018761 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018762}
18763
18764/*
18765 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 * The string "s" must have been allocated, it is consumed.
18767 * Return NULL for out of memory, the variable otherwise.
18768 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018769 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018770alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771 char_u *s;
18772{
Bram Moolenaar33570922005-01-25 22:26:29 +000018773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018775 rettv = alloc_tv();
18776 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018778 rettv->v_type = VAR_STRING;
18779 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780 }
18781 else
18782 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018783 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784}
18785
18786/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018787 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018789 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018790free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018791 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792{
18793 if (varp != NULL)
18794 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018795 switch (varp->v_type)
18796 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018797 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018798 func_unref(varp->vval.v_string);
18799 /*FALLTHROUGH*/
18800 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018801 vim_free(varp->vval.v_string);
18802 break;
18803 case VAR_LIST:
18804 list_unref(varp->vval.v_list);
18805 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018806 case VAR_DICT:
18807 dict_unref(varp->vval.v_dict);
18808 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018809 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018810#ifdef FEAT_FLOAT
18811 case VAR_FLOAT:
18812#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018813 case VAR_UNKNOWN:
18814 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018815 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018816 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018817 break;
18818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819 vim_free(varp);
18820 }
18821}
18822
18823/*
18824 * Free the memory for a variable value and set the value to NULL or 0.
18825 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018826 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018827clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018828 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829{
18830 if (varp != NULL)
18831 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018832 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018834 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018835 func_unref(varp->vval.v_string);
18836 /*FALLTHROUGH*/
18837 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018838 vim_free(varp->vval.v_string);
18839 varp->vval.v_string = NULL;
18840 break;
18841 case VAR_LIST:
18842 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018843 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018844 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018845 case VAR_DICT:
18846 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018847 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018848 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018849 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018850 varp->vval.v_number = 0;
18851 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018852#ifdef FEAT_FLOAT
18853 case VAR_FLOAT:
18854 varp->vval.v_float = 0.0;
18855 break;
18856#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018857 case VAR_UNKNOWN:
18858 break;
18859 default:
18860 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018862 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863 }
18864}
18865
18866/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018867 * Set the value of a variable to NULL without freeing items.
18868 */
18869 static void
18870init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018871 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018872{
18873 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018874 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018875}
18876
18877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878 * Get the number value of a variable.
18879 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018880 * For incompatible types, return 0.
18881 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18882 * caller of incompatible types: it sets *denote to TRUE if "denote"
18883 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884 */
18885 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018886get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018887 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018889 int error = FALSE;
18890
18891 return get_tv_number_chk(varp, &error); /* return 0L on error */
18892}
18893
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018894 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018895get_tv_number_chk(varp, denote)
18896 typval_T *varp;
18897 int *denote;
18898{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018899 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018900
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018901 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018903 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018904 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018905#ifdef FEAT_FLOAT
18906 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018907 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018908 break;
18909#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018910 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018911 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018912 break;
18913 case VAR_STRING:
18914 if (varp->vval.v_string != NULL)
18915 vim_str2nr(varp->vval.v_string, NULL, NULL,
18916 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018917 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018918 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018919 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018920 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018921 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018922 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018923 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018924 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018925 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018926 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018928 if (denote == NULL) /* useful for values that must be unsigned */
18929 n = -1;
18930 else
18931 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018932 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933}
18934
18935/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018936 * Get the lnum from the first argument.
18937 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018938 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939 */
18940 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018941get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000018942 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943{
Bram Moolenaar33570922005-01-25 22:26:29 +000018944 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945 linenr_T lnum;
18946
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018947 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948 if (lnum == 0) /* no valid number, try using line() */
18949 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018950 rettv.v_type = VAR_NUMBER;
18951 f_line(argvars, &rettv);
18952 lnum = rettv.vval.v_number;
18953 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954 }
18955 return lnum;
18956}
18957
18958/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018959 * Get the lnum from the first argument.
18960 * Also accepts "$", then "buf" is used.
18961 * Returns 0 on error.
18962 */
18963 static linenr_T
18964get_tv_lnum_buf(argvars, buf)
18965 typval_T *argvars;
18966 buf_T *buf;
18967{
18968 if (argvars[0].v_type == VAR_STRING
18969 && argvars[0].vval.v_string != NULL
18970 && argvars[0].vval.v_string[0] == '$'
18971 && buf != NULL)
18972 return buf->b_ml.ml_line_count;
18973 return get_tv_number_chk(&argvars[0], NULL);
18974}
18975
18976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977 * Get the string value of a variable.
18978 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000018979 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18980 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 * If the String variable has never been set, return an empty string.
18982 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018983 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18984 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985 */
18986 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018987get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018988 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989{
18990 static char_u mybuf[NUMBUFLEN];
18991
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018992 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993}
18994
18995 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018996get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000018997 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018998 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019000 char_u *res = get_tv_string_buf_chk(varp, buf);
19001
19002 return res != NULL ? res : (char_u *)"";
19003}
19004
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019005 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019006get_tv_string_chk(varp)
19007 typval_T *varp;
19008{
19009 static char_u mybuf[NUMBUFLEN];
19010
19011 return get_tv_string_buf_chk(varp, mybuf);
19012}
19013
19014 static char_u *
19015get_tv_string_buf_chk(varp, buf)
19016 typval_T *varp;
19017 char_u *buf;
19018{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019019 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019021 case VAR_NUMBER:
19022 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19023 return buf;
19024 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019025 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019026 break;
19027 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019028 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019029 break;
19030 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019031 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019032 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019033#ifdef FEAT_FLOAT
19034 case VAR_FLOAT:
19035 EMSG(_("E806: using Float as a String"));
19036 break;
19037#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019038 case VAR_STRING:
19039 if (varp->vval.v_string != NULL)
19040 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019041 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019042 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019043 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019044 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019046 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047}
19048
19049/*
19050 * Find variable "name" in the list of variables.
19051 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019052 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019053 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019054 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019056 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019057find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019059 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019062 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063
Bram Moolenaara7043832005-01-21 11:56:39 +000019064 ht = find_var_ht(name, &varname);
19065 if (htp != NULL)
19066 *htp = ht;
19067 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019068 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019069 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070}
19071
19072/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019073 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019074 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019076 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019077find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019078 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019079 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019080 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019081{
Bram Moolenaar33570922005-01-25 22:26:29 +000019082 hashitem_T *hi;
19083
19084 if (*varname == NUL)
19085 {
19086 /* Must be something like "s:", otherwise "ht" would be NULL. */
19087 switch (varname[-2])
19088 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019089 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019090 case 'g': return &globvars_var;
19091 case 'v': return &vimvars_var;
19092 case 'b': return &curbuf->b_bufvar;
19093 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019094#ifdef FEAT_WINDOWS
19095 case 't': return &curtab->tp_winvar;
19096#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019097 case 'l': return current_funccal == NULL
19098 ? NULL : &current_funccal->l_vars_var;
19099 case 'a': return current_funccal == NULL
19100 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019101 }
19102 return NULL;
19103 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019104
19105 hi = hash_find(ht, varname);
19106 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019107 {
19108 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019109 * worked find the variable again. Don't auto-load a script if it was
19110 * loaded already, otherwise it would be loaded every time when
19111 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019112 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019113 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019114 hi = hash_find(ht, varname);
19115 if (HASHITEM_EMPTY(hi))
19116 return NULL;
19117 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019118 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019119}
19120
19121/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019122 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019123 * Set "varname" to the start of name without ':'.
19124 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019125 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019126find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019127 char_u *name;
19128 char_u **varname;
19129{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019130 hashitem_T *hi;
19131
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132 if (name[1] != ':')
19133 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019134 /* The name must not start with a colon or #. */
19135 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136 return NULL;
19137 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019138
19139 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019140 hi = hash_find(&compat_hashtab, name);
19141 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019142 return &compat_hashtab;
19143
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019145 return &globvarht; /* global variable */
19146 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147 }
19148 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019149 if (*name == 'g') /* global variable */
19150 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019151 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19152 */
19153 if (vim_strchr(name + 2, ':') != NULL
19154 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019155 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019157 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019159 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019160#ifdef FEAT_WINDOWS
19161 if (*name == 't') /* tab page variable */
19162 return &curtab->tp_vars.dv_hashtab;
19163#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019164 if (*name == 'v') /* v: variable */
19165 return &vimvarht;
19166 if (*name == 'a' && current_funccal != NULL) /* function argument */
19167 return &current_funccal->l_avars.dv_hashtab;
19168 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19169 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170 if (*name == 's' /* script variable */
19171 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19172 return &SCRIPT_VARS(current_SID);
19173 return NULL;
19174}
19175
19176/*
19177 * Get the string value of a (global/local) variable.
19178 * Returns NULL when it doesn't exist.
19179 */
19180 char_u *
19181get_var_value(name)
19182 char_u *name;
19183{
Bram Moolenaar33570922005-01-25 22:26:29 +000019184 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185
Bram Moolenaara7043832005-01-21 11:56:39 +000019186 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187 if (v == NULL)
19188 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019189 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019190}
19191
19192/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019193 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019194 * sourcing this script and when executing functions defined in the script.
19195 */
19196 void
19197new_script_vars(id)
19198 scid_T id;
19199{
Bram Moolenaara7043832005-01-21 11:56:39 +000019200 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019201 hashtab_T *ht;
19202 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019203
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19205 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019206 /* Re-allocating ga_data means that an ht_array pointing to
19207 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019208 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019209 for (i = 1; i <= ga_scripts.ga_len; ++i)
19210 {
19211 ht = &SCRIPT_VARS(i);
19212 if (ht->ht_mask == HT_INIT_SIZE - 1)
19213 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019214 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019215 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019216 }
19217
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218 while (ga_scripts.ga_len < id)
19219 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019220 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
19221 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019222 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019224 }
19225 }
19226}
19227
19228/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019229 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19230 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231 */
19232 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019233init_var_dict(dict, dict_var)
19234 dict_T *dict;
19235 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019236{
Bram Moolenaar33570922005-01-25 22:26:29 +000019237 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019238 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019239 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019240 dict_var->di_tv.vval.v_dict = dict;
19241 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019242 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019243 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19244 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245}
19246
19247/*
19248 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019249 * Frees all allocated variables and the value they contain.
19250 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251 */
19252 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019253vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019254 hashtab_T *ht;
19255{
19256 vars_clear_ext(ht, TRUE);
19257}
19258
19259/*
19260 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19261 */
19262 static void
19263vars_clear_ext(ht, free_val)
19264 hashtab_T *ht;
19265 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266{
Bram Moolenaara7043832005-01-21 11:56:39 +000019267 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019268 hashitem_T *hi;
19269 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270
Bram Moolenaar33570922005-01-25 22:26:29 +000019271 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019272 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019273 for (hi = ht->ht_array; todo > 0; ++hi)
19274 {
19275 if (!HASHITEM_EMPTY(hi))
19276 {
19277 --todo;
19278
Bram Moolenaar33570922005-01-25 22:26:29 +000019279 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019280 * ht_array might change then. hash_clear() takes care of it
19281 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019282 v = HI2DI(hi);
19283 if (free_val)
19284 clear_tv(&v->di_tv);
19285 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19286 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019287 }
19288 }
19289 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019290 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291}
19292
Bram Moolenaara7043832005-01-21 11:56:39 +000019293/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019294 * Delete a variable from hashtab "ht" at item "hi".
19295 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019297 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019298delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019299 hashtab_T *ht;
19300 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301{
Bram Moolenaar33570922005-01-25 22:26:29 +000019302 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019303
19304 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019305 clear_tv(&di->di_tv);
19306 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307}
19308
19309/*
19310 * List the value of one internal variable.
19311 */
19312 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019313list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019314 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019316 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019318 char_u *tofree;
19319 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019320 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019321
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019322 current_copyID += COPYID_INC;
19323 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019324 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019325 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019326 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327}
19328
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019330list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019331 char_u *prefix;
19332 char_u *name;
19333 int type;
19334 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019335 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336{
Bram Moolenaar31859182007-08-14 20:41:13 +000019337 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19338 msg_start();
19339 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019340 if (name != NULL) /* "a:" vars don't have a name stored */
19341 msg_puts(name);
19342 msg_putchar(' ');
19343 msg_advance(22);
19344 if (type == VAR_NUMBER)
19345 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019346 else if (type == VAR_FUNC)
19347 msg_putchar('*');
19348 else if (type == VAR_LIST)
19349 {
19350 msg_putchar('[');
19351 if (*string == '[')
19352 ++string;
19353 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019354 else if (type == VAR_DICT)
19355 {
19356 msg_putchar('{');
19357 if (*string == '{')
19358 ++string;
19359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 else
19361 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019362
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019364
19365 if (type == VAR_FUNC)
19366 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019367 if (*first)
19368 {
19369 msg_clr_eos();
19370 *first = FALSE;
19371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019372}
19373
19374/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019375 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 * If the variable already exists, the value is updated.
19377 * Otherwise the variable is created.
19378 */
19379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019380set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019382 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019383 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384{
Bram Moolenaar33570922005-01-25 22:26:29 +000019385 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019387 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019388 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019390 ht = find_var_ht(name, &varname);
19391 if (ht == NULL || *varname == NUL)
19392 {
19393 EMSG2(_(e_illvar), name);
19394 return;
19395 }
19396 v = find_var_in_ht(ht, varname, TRUE);
19397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019398 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019399 {
19400 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19401 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19402 ? name[2] : name[0]))
19403 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019404 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019405 return;
19406 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019407 /* Don't allow hiding a function. When "v" is not NULL we migth be
19408 * assigning another function to the same var, the type is checked
19409 * below. */
19410 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019411 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019412 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019413 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019414 return;
19415 }
19416 }
19417
Bram Moolenaar33570922005-01-25 22:26:29 +000019418 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019420 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019421 if (var_check_ro(v->di_flags, name)
19422 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019423 return;
19424 if (v->di_tv.v_type != tv->v_type
19425 && !((v->di_tv.v_type == VAR_STRING
19426 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019427 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019428 || tv->v_type == VAR_NUMBER))
19429#ifdef FEAT_FLOAT
19430 && !((v->di_tv.v_type == VAR_NUMBER
19431 || v->di_tv.v_type == VAR_FLOAT)
19432 && (tv->v_type == VAR_NUMBER
19433 || tv->v_type == VAR_FLOAT))
19434#endif
19435 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019436 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019437 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019438 return;
19439 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019440
19441 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019442 * Handle setting internal v: variables separately: we don't change
19443 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019444 */
19445 if (ht == &vimvarht)
19446 {
19447 if (v->di_tv.v_type == VAR_STRING)
19448 {
19449 vim_free(v->di_tv.vval.v_string);
19450 if (copy || tv->v_type != VAR_STRING)
19451 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19452 else
19453 {
19454 /* Take over the string to avoid an extra alloc/free. */
19455 v->di_tv.vval.v_string = tv->vval.v_string;
19456 tv->vval.v_string = NULL;
19457 }
19458 }
19459 else if (v->di_tv.v_type != VAR_NUMBER)
19460 EMSG2(_(e_intern2), "set_var()");
19461 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019462 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019463 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019464 if (STRCMP(varname, "searchforward") == 0)
19465 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19466 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019467 return;
19468 }
19469
19470 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471 }
19472 else /* add a new variable */
19473 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019474 /* Can't add "v:" variable. */
19475 if (ht == &vimvarht)
19476 {
19477 EMSG2(_(e_illvar), name);
19478 return;
19479 }
19480
Bram Moolenaar92124a32005-06-17 22:03:40 +000019481 /* Make sure the variable name is valid. */
19482 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019483 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19484 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019485 {
19486 EMSG2(_(e_illvar), varname);
19487 return;
19488 }
19489
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019490 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19491 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019492 if (v == NULL)
19493 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019494 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019495 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019497 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498 return;
19499 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019500 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019502
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019503 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019504 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019505 else
19506 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019507 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019508 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019509 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511}
19512
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019513/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019514 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019515 * Also give an error message.
19516 */
19517 static int
19518var_check_ro(flags, name)
19519 int flags;
19520 char_u *name;
19521{
19522 if (flags & DI_FLAGS_RO)
19523 {
19524 EMSG2(_(e_readonlyvar), name);
19525 return TRUE;
19526 }
19527 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19528 {
19529 EMSG2(_(e_readonlysbx), name);
19530 return TRUE;
19531 }
19532 return FALSE;
19533}
19534
19535/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019536 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19537 * Also give an error message.
19538 */
19539 static int
19540var_check_fixed(flags, name)
19541 int flags;
19542 char_u *name;
19543{
19544 if (flags & DI_FLAGS_FIX)
19545 {
19546 EMSG2(_("E795: Cannot delete variable %s"), name);
19547 return TRUE;
19548 }
19549 return FALSE;
19550}
19551
19552/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019553 * Return TRUE if typeval "tv" is set to be locked (immutable).
19554 * Also give an error message, using "name".
19555 */
19556 static int
19557tv_check_lock(lock, name)
19558 int lock;
19559 char_u *name;
19560{
19561 if (lock & VAR_LOCKED)
19562 {
19563 EMSG2(_("E741: Value is locked: %s"),
19564 name == NULL ? (char_u *)_("Unknown") : name);
19565 return TRUE;
19566 }
19567 if (lock & VAR_FIXED)
19568 {
19569 EMSG2(_("E742: Cannot change value of %s"),
19570 name == NULL ? (char_u *)_("Unknown") : name);
19571 return TRUE;
19572 }
19573 return FALSE;
19574}
19575
19576/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019577 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019578 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019579 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019580 * It is OK for "from" and "to" to point to the same item. This is used to
19581 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019582 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019583 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019584copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019585 typval_T *from;
19586 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019588 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019589 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019590 switch (from->v_type)
19591 {
19592 case VAR_NUMBER:
19593 to->vval.v_number = from->vval.v_number;
19594 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019595#ifdef FEAT_FLOAT
19596 case VAR_FLOAT:
19597 to->vval.v_float = from->vval.v_float;
19598 break;
19599#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019600 case VAR_STRING:
19601 case VAR_FUNC:
19602 if (from->vval.v_string == NULL)
19603 to->vval.v_string = NULL;
19604 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019605 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019606 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019607 if (from->v_type == VAR_FUNC)
19608 func_ref(to->vval.v_string);
19609 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019610 break;
19611 case VAR_LIST:
19612 if (from->vval.v_list == NULL)
19613 to->vval.v_list = NULL;
19614 else
19615 {
19616 to->vval.v_list = from->vval.v_list;
19617 ++to->vval.v_list->lv_refcount;
19618 }
19619 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019620 case VAR_DICT:
19621 if (from->vval.v_dict == NULL)
19622 to->vval.v_dict = NULL;
19623 else
19624 {
19625 to->vval.v_dict = from->vval.v_dict;
19626 ++to->vval.v_dict->dv_refcount;
19627 }
19628 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019629 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019630 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019631 break;
19632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633}
19634
19635/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019636 * Make a copy of an item.
19637 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019638 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19639 * reference to an already copied list/dict can be used.
19640 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019641 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019642 static int
19643item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019644 typval_T *from;
19645 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019646 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019647 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019648{
19649 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019650 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019651
Bram Moolenaar33570922005-01-25 22:26:29 +000019652 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019653 {
19654 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019655 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019656 }
19657 ++recurse;
19658
19659 switch (from->v_type)
19660 {
19661 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019662#ifdef FEAT_FLOAT
19663 case VAR_FLOAT:
19664#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019665 case VAR_STRING:
19666 case VAR_FUNC:
19667 copy_tv(from, to);
19668 break;
19669 case VAR_LIST:
19670 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019671 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019672 if (from->vval.v_list == NULL)
19673 to->vval.v_list = NULL;
19674 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19675 {
19676 /* use the copy made earlier */
19677 to->vval.v_list = from->vval.v_list->lv_copylist;
19678 ++to->vval.v_list->lv_refcount;
19679 }
19680 else
19681 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19682 if (to->vval.v_list == NULL)
19683 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019684 break;
19685 case VAR_DICT:
19686 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019687 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019688 if (from->vval.v_dict == NULL)
19689 to->vval.v_dict = NULL;
19690 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19691 {
19692 /* use the copy made earlier */
19693 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19694 ++to->vval.v_dict->dv_refcount;
19695 }
19696 else
19697 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19698 if (to->vval.v_dict == NULL)
19699 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019700 break;
19701 default:
19702 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019703 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019704 }
19705 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019706 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019707}
19708
19709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710 * ":echo expr1 ..." print each argument separated with a space, add a
19711 * newline at the end.
19712 * ":echon expr1 ..." print each argument plain.
19713 */
19714 void
19715ex_echo(eap)
19716 exarg_T *eap;
19717{
19718 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019719 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019720 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721 char_u *p;
19722 int needclr = TRUE;
19723 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019724 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725
19726 if (eap->skip)
19727 ++emsg_skip;
19728 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19729 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019730 /* If eval1() causes an error message the text from the command may
19731 * still need to be cleared. E.g., "echo 22,44". */
19732 need_clr_eos = needclr;
19733
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019735 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736 {
19737 /*
19738 * Report the invalid expression unless the expression evaluation
19739 * has been cancelled due to an aborting error, an interrupt, or an
19740 * exception.
19741 */
19742 if (!aborting())
19743 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019744 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745 break;
19746 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019747 need_clr_eos = FALSE;
19748
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 if (!eap->skip)
19750 {
19751 if (atstart)
19752 {
19753 atstart = FALSE;
19754 /* Call msg_start() after eval1(), evaluating the expression
19755 * may cause a message to appear. */
19756 if (eap->cmdidx == CMD_echo)
19757 msg_start();
19758 }
19759 else if (eap->cmdidx == CMD_echo)
19760 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019761 current_copyID += COPYID_INC;
19762 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019763 if (p != NULL)
19764 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019765 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019766 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019768 if (*p != TAB && needclr)
19769 {
19770 /* remove any text still there from the command */
19771 msg_clr_eos();
19772 needclr = FALSE;
19773 }
19774 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 }
19776 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019777 {
19778#ifdef FEAT_MBYTE
19779 if (has_mbyte)
19780 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019781 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019782
19783 (void)msg_outtrans_len_attr(p, i, echo_attr);
19784 p += i - 1;
19785 }
19786 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019788 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019791 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019793 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794 arg = skipwhite(arg);
19795 }
19796 eap->nextcmd = check_nextcmd(arg);
19797
19798 if (eap->skip)
19799 --emsg_skip;
19800 else
19801 {
19802 /* remove text that may still be there from the command */
19803 if (needclr)
19804 msg_clr_eos();
19805 if (eap->cmdidx == CMD_echo)
19806 msg_end();
19807 }
19808}
19809
19810/*
19811 * ":echohl {name}".
19812 */
19813 void
19814ex_echohl(eap)
19815 exarg_T *eap;
19816{
19817 int id;
19818
19819 id = syn_name2id(eap->arg);
19820 if (id == 0)
19821 echo_attr = 0;
19822 else
19823 echo_attr = syn_id2attr(id);
19824}
19825
19826/*
19827 * ":execute expr1 ..." execute the result of an expression.
19828 * ":echomsg expr1 ..." Print a message
19829 * ":echoerr expr1 ..." Print an error
19830 * Each gets spaces around each argument and a newline at the end for
19831 * echo commands
19832 */
19833 void
19834ex_execute(eap)
19835 exarg_T *eap;
19836{
19837 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019838 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839 int ret = OK;
19840 char_u *p;
19841 garray_T ga;
19842 int len;
19843 int save_did_emsg;
19844
19845 ga_init2(&ga, 1, 80);
19846
19847 if (eap->skip)
19848 ++emsg_skip;
19849 while (*arg != NUL && *arg != '|' && *arg != '\n')
19850 {
19851 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019852 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019853 {
19854 /*
19855 * Report the invalid expression unless the expression evaluation
19856 * has been cancelled due to an aborting error, an interrupt, or an
19857 * exception.
19858 */
19859 if (!aborting())
19860 EMSG2(_(e_invexpr2), p);
19861 ret = FAIL;
19862 break;
19863 }
19864
19865 if (!eap->skip)
19866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019867 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019868 len = (int)STRLEN(p);
19869 if (ga_grow(&ga, len + 2) == FAIL)
19870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019871 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872 ret = FAIL;
19873 break;
19874 }
19875 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878 ga.ga_len += len;
19879 }
19880
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019881 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019882 arg = skipwhite(arg);
19883 }
19884
19885 if (ret != FAIL && ga.ga_data != NULL)
19886 {
19887 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019889 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019890 out_flush();
19891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892 else if (eap->cmdidx == CMD_echoerr)
19893 {
19894 /* We don't want to abort following commands, restore did_emsg. */
19895 save_did_emsg = did_emsg;
19896 EMSG((char_u *)ga.ga_data);
19897 if (!force_abort)
19898 did_emsg = save_did_emsg;
19899 }
19900 else if (eap->cmdidx == CMD_execute)
19901 do_cmdline((char_u *)ga.ga_data,
19902 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19903 }
19904
19905 ga_clear(&ga);
19906
19907 if (eap->skip)
19908 --emsg_skip;
19909
19910 eap->nextcmd = check_nextcmd(arg);
19911}
19912
19913/*
19914 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19915 * "arg" points to the "&" or '+' when called, to "option" when returning.
19916 * Returns NULL when no option name found. Otherwise pointer to the char
19917 * after the option name.
19918 */
19919 static char_u *
19920find_option_end(arg, opt_flags)
19921 char_u **arg;
19922 int *opt_flags;
19923{
19924 char_u *p = *arg;
19925
19926 ++p;
19927 if (*p == 'g' && p[1] == ':')
19928 {
19929 *opt_flags = OPT_GLOBAL;
19930 p += 2;
19931 }
19932 else if (*p == 'l' && p[1] == ':')
19933 {
19934 *opt_flags = OPT_LOCAL;
19935 p += 2;
19936 }
19937 else
19938 *opt_flags = 0;
19939
19940 if (!ASCII_ISALPHA(*p))
19941 return NULL;
19942 *arg = p;
19943
19944 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
19945 p += 4; /* termcap option */
19946 else
19947 while (ASCII_ISALPHA(*p))
19948 ++p;
19949 return p;
19950}
19951
19952/*
19953 * ":function"
19954 */
19955 void
19956ex_function(eap)
19957 exarg_T *eap;
19958{
19959 char_u *theline;
19960 int j;
19961 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963 char_u *name = NULL;
19964 char_u *p;
19965 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019966 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 garray_T newargs;
19968 garray_T newlines;
19969 int varargs = FALSE;
19970 int mustend = FALSE;
19971 int flags = 0;
19972 ufunc_T *fp;
19973 int indent;
19974 int nesting;
19975 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019976 dictitem_T *v;
19977 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019978 static int func_nr = 0; /* number for nameless function */
19979 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019980 hashtab_T *ht;
19981 int todo;
19982 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019983 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984
19985 /*
19986 * ":function" without argument: list functions.
19987 */
19988 if (ends_excmd(*eap->arg))
19989 {
19990 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019991 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019992 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000019993 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019994 {
19995 if (!HASHITEM_EMPTY(hi))
19996 {
19997 --todo;
19998 fp = HI2UF(hi);
19999 if (!isdigit(*fp->uf_name))
20000 list_func_head(fp, FALSE);
20001 }
20002 }
20003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004 eap->nextcmd = check_nextcmd(eap->arg);
20005 return;
20006 }
20007
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020008 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020009 * ":function /pat": list functions matching pattern.
20010 */
20011 if (*eap->arg == '/')
20012 {
20013 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20014 if (!eap->skip)
20015 {
20016 regmatch_T regmatch;
20017
20018 c = *p;
20019 *p = NUL;
20020 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20021 *p = c;
20022 if (regmatch.regprog != NULL)
20023 {
20024 regmatch.rm_ic = p_ic;
20025
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020026 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020027 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20028 {
20029 if (!HASHITEM_EMPTY(hi))
20030 {
20031 --todo;
20032 fp = HI2UF(hi);
20033 if (!isdigit(*fp->uf_name)
20034 && vim_regexec(&regmatch, fp->uf_name, 0))
20035 list_func_head(fp, FALSE);
20036 }
20037 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020038 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020039 }
20040 }
20041 if (*p == '/')
20042 ++p;
20043 eap->nextcmd = check_nextcmd(p);
20044 return;
20045 }
20046
20047 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020048 * Get the function name. There are these situations:
20049 * func normal function name
20050 * "name" == func, "fudi.fd_dict" == NULL
20051 * dict.func new dictionary entry
20052 * "name" == NULL, "fudi.fd_dict" set,
20053 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20054 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020055 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020056 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20057 * dict.func existing dict entry that's not a Funcref
20058 * "name" == NULL, "fudi.fd_dict" set,
20059 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020062 name = trans_function_name(&p, eap->skip, 0, &fudi);
20063 paren = (vim_strchr(p, '(') != NULL);
20064 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065 {
20066 /*
20067 * Return on an invalid expression in braces, unless the expression
20068 * evaluation has been cancelled due to an aborting error, an
20069 * interrupt, or an exception.
20070 */
20071 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020072 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020073 if (!eap->skip && fudi.fd_newkey != NULL)
20074 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020075 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078 else
20079 eap->skip = TRUE;
20080 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020081
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082 /* An error in a function call during evaluation of an expression in magic
20083 * braces should not cause the function not to be defined. */
20084 saved_did_emsg = did_emsg;
20085 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020086
20087 /*
20088 * ":function func" with only function name: list function.
20089 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020090 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 {
20092 if (!ends_excmd(*skipwhite(p)))
20093 {
20094 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020095 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 }
20097 eap->nextcmd = check_nextcmd(p);
20098 if (eap->nextcmd != NULL)
20099 *p = NUL;
20100 if (!eap->skip && !got_int)
20101 {
20102 fp = find_func(name);
20103 if (fp != NULL)
20104 {
20105 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020106 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020108 if (FUNCLINE(fp, j) == NULL)
20109 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110 msg_putchar('\n');
20111 msg_outnum((long)(j + 1));
20112 if (j < 9)
20113 msg_putchar(' ');
20114 if (j < 99)
20115 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020116 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 out_flush(); /* show a line at a time */
20118 ui_breakcheck();
20119 }
20120 if (!got_int)
20121 {
20122 msg_putchar('\n');
20123 msg_puts((char_u *)" endfunction");
20124 }
20125 }
20126 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020127 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020129 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 }
20131
20132 /*
20133 * ":function name(arg1, arg2)" Define function.
20134 */
20135 p = skipwhite(p);
20136 if (*p != '(')
20137 {
20138 if (!eap->skip)
20139 {
20140 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020141 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 }
20143 /* attempt to continue by skipping some text */
20144 if (vim_strchr(p, '(') != NULL)
20145 p = vim_strchr(p, '(');
20146 }
20147 p = skipwhite(p + 1);
20148
20149 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20150 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20151
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020152 if (!eap->skip)
20153 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020154 /* Check the name of the function. Unless it's a dictionary function
20155 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020156 if (name != NULL)
20157 arg = name;
20158 else
20159 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020160 if (arg != NULL && (fudi.fd_di == NULL
20161 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020162 {
20163 if (*arg == K_SPECIAL)
20164 j = 3;
20165 else
20166 j = 0;
20167 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20168 : eval_isnamec(arg[j])))
20169 ++j;
20170 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020171 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020172 }
20173 }
20174
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175 /*
20176 * Isolate the arguments: "arg1, arg2, ...)"
20177 */
20178 while (*p != ')')
20179 {
20180 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20181 {
20182 varargs = TRUE;
20183 p += 3;
20184 mustend = TRUE;
20185 }
20186 else
20187 {
20188 arg = p;
20189 while (ASCII_ISALNUM(*p) || *p == '_')
20190 ++p;
20191 if (arg == p || isdigit(*arg)
20192 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20193 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20194 {
20195 if (!eap->skip)
20196 EMSG2(_("E125: Illegal argument: %s"), arg);
20197 break;
20198 }
20199 if (ga_grow(&newargs, 1) == FAIL)
20200 goto erret;
20201 c = *p;
20202 *p = NUL;
20203 arg = vim_strsave(arg);
20204 if (arg == NULL)
20205 goto erret;
20206 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20207 *p = c;
20208 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209 if (*p == ',')
20210 ++p;
20211 else
20212 mustend = TRUE;
20213 }
20214 p = skipwhite(p);
20215 if (mustend && *p != ')')
20216 {
20217 if (!eap->skip)
20218 EMSG2(_(e_invarg2), eap->arg);
20219 break;
20220 }
20221 }
20222 ++p; /* skip the ')' */
20223
Bram Moolenaare9a41262005-01-15 22:18:47 +000020224 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 for (;;)
20226 {
20227 p = skipwhite(p);
20228 if (STRNCMP(p, "range", 5) == 0)
20229 {
20230 flags |= FC_RANGE;
20231 p += 5;
20232 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020233 else if (STRNCMP(p, "dict", 4) == 0)
20234 {
20235 flags |= FC_DICT;
20236 p += 4;
20237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 else if (STRNCMP(p, "abort", 5) == 0)
20239 {
20240 flags |= FC_ABORT;
20241 p += 5;
20242 }
20243 else
20244 break;
20245 }
20246
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020247 /* When there is a line break use what follows for the function body.
20248 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20249 if (*p == '\n')
20250 line_arg = p + 1;
20251 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252 EMSG(_(e_trailing));
20253
20254 /*
20255 * Read the body of the function, until ":endfunction" is found.
20256 */
20257 if (KeyTyped)
20258 {
20259 /* Check if the function already exists, don't let the user type the
20260 * whole function before telling him it doesn't work! For a script we
20261 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020262 if (!eap->skip && !eap->forceit)
20263 {
20264 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20265 EMSG(_(e_funcdict));
20266 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020267 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020270 if (!eap->skip && did_emsg)
20271 goto erret;
20272
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273 msg_putchar('\n'); /* don't overwrite the function name */
20274 cmdline_row = msg_row;
20275 }
20276
20277 indent = 2;
20278 nesting = 0;
20279 for (;;)
20280 {
20281 msg_scroll = TRUE;
20282 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020283 sourcing_lnum_off = sourcing_lnum;
20284
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020285 if (line_arg != NULL)
20286 {
20287 /* Use eap->arg, split up in parts by line breaks. */
20288 theline = line_arg;
20289 p = vim_strchr(theline, '\n');
20290 if (p == NULL)
20291 line_arg += STRLEN(line_arg);
20292 else
20293 {
20294 *p = NUL;
20295 line_arg = p + 1;
20296 }
20297 }
20298 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299 theline = getcmdline(':', 0L, indent);
20300 else
20301 theline = eap->getline(':', eap->cookie, indent);
20302 if (KeyTyped)
20303 lines_left = Rows - 1;
20304 if (theline == NULL)
20305 {
20306 EMSG(_("E126: Missing :endfunction"));
20307 goto erret;
20308 }
20309
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020310 /* Detect line continuation: sourcing_lnum increased more than one. */
20311 if (sourcing_lnum > sourcing_lnum_off + 1)
20312 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20313 else
20314 sourcing_lnum_off = 0;
20315
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 if (skip_until != NULL)
20317 {
20318 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20319 * don't check for ":endfunc". */
20320 if (STRCMP(theline, skip_until) == 0)
20321 {
20322 vim_free(skip_until);
20323 skip_until = NULL;
20324 }
20325 }
20326 else
20327 {
20328 /* skip ':' and blanks*/
20329 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20330 ;
20331
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020332 /* Check for "endfunction". */
20333 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020335 if (line_arg == NULL)
20336 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337 break;
20338 }
20339
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020340 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341 * at "end". */
20342 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20343 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020344 else if (STRNCMP(p, "if", 2) == 0
20345 || STRNCMP(p, "wh", 2) == 0
20346 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 || STRNCMP(p, "try", 3) == 0)
20348 indent += 2;
20349
20350 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020351 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020353 if (*p == '!')
20354 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355 p += eval_fname_script(p);
20356 if (ASCII_ISALPHA(*p))
20357 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020358 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359 if (*skipwhite(p) == '(')
20360 {
20361 ++nesting;
20362 indent += 2;
20363 }
20364 }
20365 }
20366
20367 /* Check for ":append" or ":insert". */
20368 p = skip_range(p, NULL);
20369 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20370 || (p[0] == 'i'
20371 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20372 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20373 skip_until = vim_strsave((char_u *)".");
20374
20375 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20376 arg = skipwhite(skiptowhite(p));
20377 if (arg[0] == '<' && arg[1] =='<'
20378 && ((p[0] == 'p' && p[1] == 'y'
20379 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20380 || (p[0] == 'p' && p[1] == 'e'
20381 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20382 || (p[0] == 't' && p[1] == 'c'
20383 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20384 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20385 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020386 || (p[0] == 'm' && p[1] == 'z'
20387 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388 ))
20389 {
20390 /* ":python <<" continues until a dot, like ":append" */
20391 p = skipwhite(arg + 2);
20392 if (*p == NUL)
20393 skip_until = vim_strsave((char_u *)".");
20394 else
20395 skip_until = vim_strsave(p);
20396 }
20397 }
20398
20399 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020400 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020401 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020402 if (line_arg == NULL)
20403 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020405 }
20406
20407 /* Copy the line to newly allocated memory. get_one_sourceline()
20408 * allocates 250 bytes per line, this saves 80% on average. The cost
20409 * is an extra alloc/free. */
20410 p = vim_strsave(theline);
20411 if (p != NULL)
20412 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020413 if (line_arg == NULL)
20414 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020415 theline = p;
20416 }
20417
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020418 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20419
20420 /* Add NULL lines for continuation lines, so that the line count is
20421 * equal to the index in the growarray. */
20422 while (sourcing_lnum_off-- > 0)
20423 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020424
20425 /* Check for end of eap->arg. */
20426 if (line_arg != NULL && *line_arg == NUL)
20427 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428 }
20429
20430 /* Don't define the function when skipping commands or when an error was
20431 * detected. */
20432 if (eap->skip || did_emsg)
20433 goto erret;
20434
20435 /*
20436 * If there are no errors, add the function
20437 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020438 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020439 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020440 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020441 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020442 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020443 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020444 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020445 goto erret;
20446 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020447
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020448 fp = find_func(name);
20449 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020451 if (!eap->forceit)
20452 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020453 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020454 goto erret;
20455 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020456 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020457 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020458 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020459 name);
20460 goto erret;
20461 }
20462 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020463 ga_clear_strings(&(fp->uf_args));
20464 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020465 vim_free(name);
20466 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468 }
20469 else
20470 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020471 char numbuf[20];
20472
20473 fp = NULL;
20474 if (fudi.fd_newkey == NULL && !eap->forceit)
20475 {
20476 EMSG(_(e_funcdict));
20477 goto erret;
20478 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020479 if (fudi.fd_di == NULL)
20480 {
20481 /* Can't add a function to a locked dictionary */
20482 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20483 goto erret;
20484 }
20485 /* Can't change an existing function if it is locked */
20486 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20487 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020488
20489 /* Give the function a sequential number. Can only be used with a
20490 * Funcref! */
20491 vim_free(name);
20492 sprintf(numbuf, "%d", ++func_nr);
20493 name = vim_strsave((char_u *)numbuf);
20494 if (name == NULL)
20495 goto erret;
20496 }
20497
20498 if (fp == NULL)
20499 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020500 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020501 {
20502 int slen, plen;
20503 char_u *scriptname;
20504
20505 /* Check that the autoload name matches the script name. */
20506 j = FAIL;
20507 if (sourcing_name != NULL)
20508 {
20509 scriptname = autoload_name(name);
20510 if (scriptname != NULL)
20511 {
20512 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020513 plen = (int)STRLEN(p);
20514 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020515 if (slen > plen && fnamecmp(p,
20516 sourcing_name + slen - plen) == 0)
20517 j = OK;
20518 vim_free(scriptname);
20519 }
20520 }
20521 if (j == FAIL)
20522 {
20523 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20524 goto erret;
20525 }
20526 }
20527
20528 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020529 if (fp == NULL)
20530 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020531
20532 if (fudi.fd_dict != NULL)
20533 {
20534 if (fudi.fd_di == NULL)
20535 {
20536 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020537 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020538 if (fudi.fd_di == NULL)
20539 {
20540 vim_free(fp);
20541 goto erret;
20542 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020543 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20544 {
20545 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020546 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020547 goto erret;
20548 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020549 }
20550 else
20551 /* overwrite existing dict entry */
20552 clear_tv(&fudi.fd_di->di_tv);
20553 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020554 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020555 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020556 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020557
20558 /* behave like "dict" was used */
20559 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020560 }
20561
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020563 STRCPY(fp->uf_name, name);
20564 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020566 fp->uf_args = newargs;
20567 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020568#ifdef FEAT_PROFILE
20569 fp->uf_tml_count = NULL;
20570 fp->uf_tml_total = NULL;
20571 fp->uf_tml_self = NULL;
20572 fp->uf_profiling = FALSE;
20573 if (prof_def_func())
20574 func_do_profile(fp);
20575#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020576 fp->uf_varargs = varargs;
20577 fp->uf_flags = flags;
20578 fp->uf_calls = 0;
20579 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020580 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581
20582erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583 ga_clear_strings(&newargs);
20584 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020585ret_free:
20586 vim_free(skip_until);
20587 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590}
20591
20592/*
20593 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020594 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020596 * flags:
20597 * TFN_INT: internal function name OK
20598 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599 * Advances "pp" to just after the function name (if no error).
20600 */
20601 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020602trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603 char_u **pp;
20604 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020605 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020606 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020608 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609 char_u *start;
20610 char_u *end;
20611 int lead;
20612 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020614 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020615
20616 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020617 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020619
20620 /* Check for hard coded <SNR>: already translated function ID (from a user
20621 * command). */
20622 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20623 && (*pp)[2] == (int)KE_SNR)
20624 {
20625 *pp += 3;
20626 len = get_id_len(pp) + 3;
20627 return vim_strnsave(start, len);
20628 }
20629
20630 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20631 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020632 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020633 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020634 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020635
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020636 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20637 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020638 if (end == start)
20639 {
20640 if (!skip)
20641 EMSG(_("E129: Function name required"));
20642 goto theend;
20643 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020644 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020645 {
20646 /*
20647 * Report an invalid expression in braces, unless the expression
20648 * evaluation has been cancelled due to an aborting error, an
20649 * interrupt, or an exception.
20650 */
20651 if (!aborting())
20652 {
20653 if (end != NULL)
20654 EMSG2(_(e_invarg2), start);
20655 }
20656 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020657 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020658 goto theend;
20659 }
20660
20661 if (lv.ll_tv != NULL)
20662 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020663 if (fdp != NULL)
20664 {
20665 fdp->fd_dict = lv.ll_dict;
20666 fdp->fd_newkey = lv.ll_newkey;
20667 lv.ll_newkey = NULL;
20668 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020669 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020670 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20671 {
20672 name = vim_strsave(lv.ll_tv->vval.v_string);
20673 *pp = end;
20674 }
20675 else
20676 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020677 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20678 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020679 EMSG(_(e_funcref));
20680 else
20681 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020682 name = NULL;
20683 }
20684 goto theend;
20685 }
20686
20687 if (lv.ll_name == NULL)
20688 {
20689 /* Error found, but continue after the function name. */
20690 *pp = end;
20691 goto theend;
20692 }
20693
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020694 /* Check if the name is a Funcref. If so, use the value. */
20695 if (lv.ll_exp_name != NULL)
20696 {
20697 len = (int)STRLEN(lv.ll_exp_name);
20698 name = deref_func_name(lv.ll_exp_name, &len);
20699 if (name == lv.ll_exp_name)
20700 name = NULL;
20701 }
20702 else
20703 {
20704 len = (int)(end - *pp);
20705 name = deref_func_name(*pp, &len);
20706 if (name == *pp)
20707 name = NULL;
20708 }
20709 if (name != NULL)
20710 {
20711 name = vim_strsave(name);
20712 *pp = end;
20713 goto theend;
20714 }
20715
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020716 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020717 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020718 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020719 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20720 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20721 {
20722 /* When there was "s:" already or the name expanded to get a
20723 * leading "s:" then remove it. */
20724 lv.ll_name += 2;
20725 len -= 2;
20726 lead = 2;
20727 }
20728 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020729 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020730 {
20731 if (lead == 2) /* skip over "s:" */
20732 lv.ll_name += 2;
20733 len = (int)(end - lv.ll_name);
20734 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020735
20736 /*
20737 * Copy the function name to allocated memory.
20738 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20739 * Accept <SNR>123_name() outside a script.
20740 */
20741 if (skip)
20742 lead = 0; /* do nothing */
20743 else if (lead > 0)
20744 {
20745 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020746 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20747 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020748 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020749 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020750 if (current_SID <= 0)
20751 {
20752 EMSG(_(e_usingsid));
20753 goto theend;
20754 }
20755 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20756 lead += (int)STRLEN(sid_buf);
20757 }
20758 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020759 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020760 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020761 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020762 goto theend;
20763 }
20764 name = alloc((unsigned)(len + lead + 1));
20765 if (name != NULL)
20766 {
20767 if (lead > 0)
20768 {
20769 name[0] = K_SPECIAL;
20770 name[1] = KS_EXTRA;
20771 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020772 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020773 STRCPY(name + 3, sid_buf);
20774 }
20775 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20776 name[len + lead] = NUL;
20777 }
20778 *pp = end;
20779
20780theend:
20781 clear_lval(&lv);
20782 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783}
20784
20785/*
20786 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20787 * Return 2 if "p" starts with "s:".
20788 * Return 0 otherwise.
20789 */
20790 static int
20791eval_fname_script(p)
20792 char_u *p;
20793{
20794 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20795 || STRNICMP(p + 1, "SNR>", 4) == 0))
20796 return 5;
20797 if (p[0] == 's' && p[1] == ':')
20798 return 2;
20799 return 0;
20800}
20801
20802/*
20803 * Return TRUE if "p" starts with "<SID>" or "s:".
20804 * Only works if eval_fname_script() returned non-zero for "p"!
20805 */
20806 static int
20807eval_fname_sid(p)
20808 char_u *p;
20809{
20810 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20811}
20812
20813/*
20814 * List the head of the function: "name(arg1, arg2)".
20815 */
20816 static void
20817list_func_head(fp, indent)
20818 ufunc_T *fp;
20819 int indent;
20820{
20821 int j;
20822
20823 msg_start();
20824 if (indent)
20825 MSG_PUTS(" ");
20826 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020827 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828 {
20829 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020830 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 }
20832 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020833 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020835 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836 {
20837 if (j)
20838 MSG_PUTS(", ");
20839 msg_puts(FUNCARG(fp, j));
20840 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020841 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842 {
20843 if (j)
20844 MSG_PUTS(", ");
20845 MSG_PUTS("...");
20846 }
20847 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020848 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020849 if (p_verbose > 0)
20850 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851}
20852
20853/*
20854 * Find a function by name, return pointer to it in ufuncs.
20855 * Return NULL for unknown function.
20856 */
20857 static ufunc_T *
20858find_func(name)
20859 char_u *name;
20860{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020861 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020863 hi = hash_find(&func_hashtab, name);
20864 if (!HASHITEM_EMPTY(hi))
20865 return HI2UF(hi);
20866 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020867}
20868
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020869#if defined(EXITFREE) || defined(PROTO)
20870 void
20871free_all_functions()
20872{
20873 hashitem_T *hi;
20874
20875 /* Need to start all over every time, because func_free() may change the
20876 * hash table. */
20877 while (func_hashtab.ht_used > 0)
20878 for (hi = func_hashtab.ht_array; ; ++hi)
20879 if (!HASHITEM_EMPTY(hi))
20880 {
20881 func_free(HI2UF(hi));
20882 break;
20883 }
20884}
20885#endif
20886
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020887/*
20888 * Return TRUE if a function "name" exists.
20889 */
20890 static int
20891function_exists(name)
20892 char_u *name;
20893{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020894 char_u *nm = name;
20895 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020896 int n = FALSE;
20897
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020898 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020899 nm = skipwhite(nm);
20900
20901 /* Only accept "funcname", "funcname ", "funcname (..." and
20902 * "funcname(...", not "funcname!...". */
20903 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020904 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020905 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020906 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020907 else
20908 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020909 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020910 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020911 return n;
20912}
20913
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020914/*
20915 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020916 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020917 */
20918 static int
20919builtin_function(name)
20920 char_u *name;
20921{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020922 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20923 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020924}
20925
Bram Moolenaar05159a02005-02-26 23:04:13 +000020926#if defined(FEAT_PROFILE) || defined(PROTO)
20927/*
20928 * Start profiling function "fp".
20929 */
20930 static void
20931func_do_profile(fp)
20932 ufunc_T *fp;
20933{
20934 fp->uf_tm_count = 0;
20935 profile_zero(&fp->uf_tm_self);
20936 profile_zero(&fp->uf_tm_total);
20937 if (fp->uf_tml_count == NULL)
20938 fp->uf_tml_count = (int *)alloc_clear((unsigned)
20939 (sizeof(int) * fp->uf_lines.ga_len));
20940 if (fp->uf_tml_total == NULL)
20941 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
20942 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20943 if (fp->uf_tml_self == NULL)
20944 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
20945 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20946 fp->uf_tml_idx = -1;
20947 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
20948 || fp->uf_tml_self == NULL)
20949 return; /* out of memory */
20950
20951 fp->uf_profiling = TRUE;
20952}
20953
20954/*
20955 * Dump the profiling results for all functions in file "fd".
20956 */
20957 void
20958func_dump_profile(fd)
20959 FILE *fd;
20960{
20961 hashitem_T *hi;
20962 int todo;
20963 ufunc_T *fp;
20964 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000020965 ufunc_T **sorttab;
20966 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020967
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020968 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020969 if (todo == 0)
20970 return; /* nothing to dump */
20971
Bram Moolenaar73830342005-02-28 22:48:19 +000020972 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
20973
Bram Moolenaar05159a02005-02-26 23:04:13 +000020974 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
20975 {
20976 if (!HASHITEM_EMPTY(hi))
20977 {
20978 --todo;
20979 fp = HI2UF(hi);
20980 if (fp->uf_profiling)
20981 {
Bram Moolenaar73830342005-02-28 22:48:19 +000020982 if (sorttab != NULL)
20983 sorttab[st_len++] = fp;
20984
Bram Moolenaar05159a02005-02-26 23:04:13 +000020985 if (fp->uf_name[0] == K_SPECIAL)
20986 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
20987 else
20988 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
20989 if (fp->uf_tm_count == 1)
20990 fprintf(fd, "Called 1 time\n");
20991 else
20992 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
20993 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
20994 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
20995 fprintf(fd, "\n");
20996 fprintf(fd, "count total (s) self (s)\n");
20997
20998 for (i = 0; i < fp->uf_lines.ga_len; ++i)
20999 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021000 if (FUNCLINE(fp, i) == NULL)
21001 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021002 prof_func_line(fd, fp->uf_tml_count[i],
21003 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021004 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21005 }
21006 fprintf(fd, "\n");
21007 }
21008 }
21009 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021010
21011 if (sorttab != NULL && st_len > 0)
21012 {
21013 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21014 prof_total_cmp);
21015 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21016 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21017 prof_self_cmp);
21018 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21019 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021020
21021 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021022}
Bram Moolenaar73830342005-02-28 22:48:19 +000021023
21024 static void
21025prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21026 FILE *fd;
21027 ufunc_T **sorttab;
21028 int st_len;
21029 char *title;
21030 int prefer_self; /* when equal print only self time */
21031{
21032 int i;
21033 ufunc_T *fp;
21034
21035 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21036 fprintf(fd, "count total (s) self (s) function\n");
21037 for (i = 0; i < 20 && i < st_len; ++i)
21038 {
21039 fp = sorttab[i];
21040 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21041 prefer_self);
21042 if (fp->uf_name[0] == K_SPECIAL)
21043 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21044 else
21045 fprintf(fd, " %s()\n", fp->uf_name);
21046 }
21047 fprintf(fd, "\n");
21048}
21049
21050/*
21051 * Print the count and times for one function or function line.
21052 */
21053 static void
21054prof_func_line(fd, count, total, self, prefer_self)
21055 FILE *fd;
21056 int count;
21057 proftime_T *total;
21058 proftime_T *self;
21059 int prefer_self; /* when equal print only self time */
21060{
21061 if (count > 0)
21062 {
21063 fprintf(fd, "%5d ", count);
21064 if (prefer_self && profile_equal(total, self))
21065 fprintf(fd, " ");
21066 else
21067 fprintf(fd, "%s ", profile_msg(total));
21068 if (!prefer_self && profile_equal(total, self))
21069 fprintf(fd, " ");
21070 else
21071 fprintf(fd, "%s ", profile_msg(self));
21072 }
21073 else
21074 fprintf(fd, " ");
21075}
21076
21077/*
21078 * Compare function for total time sorting.
21079 */
21080 static int
21081#ifdef __BORLANDC__
21082_RTLENTRYF
21083#endif
21084prof_total_cmp(s1, s2)
21085 const void *s1;
21086 const void *s2;
21087{
21088 ufunc_T *p1, *p2;
21089
21090 p1 = *(ufunc_T **)s1;
21091 p2 = *(ufunc_T **)s2;
21092 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21093}
21094
21095/*
21096 * Compare function for self time sorting.
21097 */
21098 static int
21099#ifdef __BORLANDC__
21100_RTLENTRYF
21101#endif
21102prof_self_cmp(s1, s2)
21103 const void *s1;
21104 const void *s2;
21105{
21106 ufunc_T *p1, *p2;
21107
21108 p1 = *(ufunc_T **)s1;
21109 p2 = *(ufunc_T **)s2;
21110 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21111}
21112
Bram Moolenaar05159a02005-02-26 23:04:13 +000021113#endif
21114
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021115/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021116 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021117 * Return TRUE if a package was loaded.
21118 */
21119 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021120script_autoload(name, reload)
21121 char_u *name;
21122 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021123{
21124 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021125 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021126 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021127 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021128
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021129 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021130 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021131 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021132 return FALSE;
21133
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021134 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021135
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021136 /* Find the name in the list of previously loaded package names. Skip
21137 * "autoload/", it's always the same. */
21138 for (i = 0; i < ga_loaded.ga_len; ++i)
21139 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21140 break;
21141 if (!reload && i < ga_loaded.ga_len)
21142 ret = FALSE; /* was loaded already */
21143 else
21144 {
21145 /* Remember the name if it wasn't loaded already. */
21146 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21147 {
21148 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21149 tofree = NULL;
21150 }
21151
21152 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021153 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021154 ret = TRUE;
21155 }
21156
21157 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021158 return ret;
21159}
21160
21161/*
21162 * Return the autoload script name for a function or variable name.
21163 * Returns NULL when out of memory.
21164 */
21165 static char_u *
21166autoload_name(name)
21167 char_u *name;
21168{
21169 char_u *p;
21170 char_u *scriptname;
21171
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021172 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021173 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21174 if (scriptname == NULL)
21175 return FALSE;
21176 STRCPY(scriptname, "autoload/");
21177 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021178 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021179 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021180 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021181 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021182 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021183}
21184
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21186
21187/*
21188 * Function given to ExpandGeneric() to obtain the list of user defined
21189 * function names.
21190 */
21191 char_u *
21192get_user_func_name(xp, idx)
21193 expand_T *xp;
21194 int idx;
21195{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021196 static long_u done;
21197 static hashitem_T *hi;
21198 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199
21200 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021202 done = 0;
21203 hi = func_hashtab.ht_array;
21204 }
21205 if (done < func_hashtab.ht_used)
21206 {
21207 if (done++ > 0)
21208 ++hi;
21209 while (HASHITEM_EMPTY(hi))
21210 ++hi;
21211 fp = HI2UF(hi);
21212
21213 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21214 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215
21216 cat_func_name(IObuff, fp);
21217 if (xp->xp_context != EXPAND_USER_FUNC)
21218 {
21219 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021220 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221 STRCAT(IObuff, ")");
21222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223 return IObuff;
21224 }
21225 return NULL;
21226}
21227
21228#endif /* FEAT_CMDL_COMPL */
21229
21230/*
21231 * Copy the function name of "fp" to buffer "buf".
21232 * "buf" must be able to hold the function name plus three bytes.
21233 * Takes care of script-local function names.
21234 */
21235 static void
21236cat_func_name(buf, fp)
21237 char_u *buf;
21238 ufunc_T *fp;
21239{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021240 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241 {
21242 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021243 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 }
21245 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021246 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247}
21248
21249/*
21250 * ":delfunction {name}"
21251 */
21252 void
21253ex_delfunction(eap)
21254 exarg_T *eap;
21255{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021256 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257 char_u *p;
21258 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021259 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260
21261 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021262 name = trans_function_name(&p, eap->skip, 0, &fudi);
21263 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021264 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021265 {
21266 if (fudi.fd_dict != NULL && !eap->skip)
21267 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270 if (!ends_excmd(*skipwhite(p)))
21271 {
21272 vim_free(name);
21273 EMSG(_(e_trailing));
21274 return;
21275 }
21276 eap->nextcmd = check_nextcmd(p);
21277 if (eap->nextcmd != NULL)
21278 *p = NUL;
21279
21280 if (!eap->skip)
21281 fp = find_func(name);
21282 vim_free(name);
21283
21284 if (!eap->skip)
21285 {
21286 if (fp == NULL)
21287 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021288 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289 return;
21290 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021291 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292 {
21293 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21294 return;
21295 }
21296
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021297 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021299 /* Delete the dict item that refers to the function, it will
21300 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021301 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021303 else
21304 func_free(fp);
21305 }
21306}
21307
21308/*
21309 * Free a function and remove it from the list of functions.
21310 */
21311 static void
21312func_free(fp)
21313 ufunc_T *fp;
21314{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021315 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021316
21317 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021318 ga_clear_strings(&(fp->uf_args));
21319 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021320#ifdef FEAT_PROFILE
21321 vim_free(fp->uf_tml_count);
21322 vim_free(fp->uf_tml_total);
21323 vim_free(fp->uf_tml_self);
21324#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021325
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021326 /* remove the function from the function hashtable */
21327 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21328 if (HASHITEM_EMPTY(hi))
21329 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021330 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021331 hash_remove(&func_hashtab, hi);
21332
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021333 vim_free(fp);
21334}
21335
21336/*
21337 * Unreference a Function: decrement the reference count and free it when it
21338 * becomes zero. Only for numbered functions.
21339 */
21340 static void
21341func_unref(name)
21342 char_u *name;
21343{
21344 ufunc_T *fp;
21345
21346 if (name != NULL && isdigit(*name))
21347 {
21348 fp = find_func(name);
21349 if (fp == NULL)
21350 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021351 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021352 {
21353 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021354 * when "uf_calls" becomes zero. */
21355 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021356 func_free(fp);
21357 }
21358 }
21359}
21360
21361/*
21362 * Count a reference to a Function.
21363 */
21364 static void
21365func_ref(name)
21366 char_u *name;
21367{
21368 ufunc_T *fp;
21369
21370 if (name != NULL && isdigit(*name))
21371 {
21372 fp = find_func(name);
21373 if (fp == NULL)
21374 EMSG2(_(e_intern2), "func_ref()");
21375 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021376 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021377 }
21378}
21379
21380/*
21381 * Call a user function.
21382 */
21383 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021384call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 ufunc_T *fp; /* pointer to function */
21386 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021387 typval_T *argvars; /* arguments */
21388 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021389 linenr_T firstline; /* first line of range */
21390 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021391 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392{
Bram Moolenaar33570922005-01-25 22:26:29 +000021393 char_u *save_sourcing_name;
21394 linenr_T save_sourcing_lnum;
21395 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021396 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021397 int save_did_emsg;
21398 static int depth = 0;
21399 dictitem_T *v;
21400 int fixvar_idx = 0; /* index in fixvar[] */
21401 int i;
21402 int ai;
21403 char_u numbuf[NUMBUFLEN];
21404 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021405#ifdef FEAT_PROFILE
21406 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021407 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409
21410 /* If depth of calling is getting too high, don't execute the function */
21411 if (depth >= p_mfd)
21412 {
21413 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021414 rettv->v_type = VAR_NUMBER;
21415 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 return;
21417 }
21418 ++depth;
21419
21420 line_breakcheck(); /* check for CTRL-C hit */
21421
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021422 fc = (funccall_T *)alloc(sizeof(funccall_T));
21423 fc->caller = current_funccal;
21424 current_funccal = fc;
21425 fc->func = fp;
21426 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021427 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021428 fc->linenr = 0;
21429 fc->returned = FALSE;
21430 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021432 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21433 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434
Bram Moolenaar33570922005-01-25 22:26:29 +000021435 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021436 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021437 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21438 * each argument variable and saves a lot of time.
21439 */
21440 /*
21441 * Init l: variables.
21442 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021443 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021444 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021445 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021446 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21447 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021448 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021449 name = v->di_key;
21450 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021451 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021452 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021453 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021454 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021455 v->di_tv.vval.v_dict = selfdict;
21456 ++selfdict->dv_refcount;
21457 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021458
Bram Moolenaar33570922005-01-25 22:26:29 +000021459 /*
21460 * Init a: variables.
21461 * Set a:0 to "argcount".
21462 * Set a:000 to a list with room for the "..." arguments.
21463 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021464 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21465 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021466 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021467 /* Use "name" to avoid a warning from some compiler that checks the
21468 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021469 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021470 name = v->di_key;
21471 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021472 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021473 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021474 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021475 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021476 v->di_tv.vval.v_list = &fc->l_varlist;
21477 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21478 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21479 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021480
21481 /*
21482 * Set a:firstline to "firstline" and a:lastline to "lastline".
21483 * Set a:name to named arguments.
21484 * Set a:N to the "..." arguments.
21485 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021486 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021487 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021488 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021489 (varnumber_T)lastline);
21490 for (i = 0; i < argcount; ++i)
21491 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021492 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021493 if (ai < 0)
21494 /* named argument a:name */
21495 name = FUNCARG(fp, i);
21496 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021497 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021498 /* "..." argument a:1, a:2, etc. */
21499 sprintf((char *)numbuf, "%d", ai + 1);
21500 name = numbuf;
21501 }
21502 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21503 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021504 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021505 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21506 }
21507 else
21508 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021509 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21510 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021511 if (v == NULL)
21512 break;
21513 v->di_flags = DI_FLAGS_RO;
21514 }
21515 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021516 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021517
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021518 /* Note: the values are copied directly to avoid alloc/free.
21519 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021520 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021521 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021522
21523 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21524 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021525 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21526 fc->l_listitems[ai].li_tv = argvars[i];
21527 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021528 }
21529 }
21530
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531 /* Don't redraw while executing the function. */
21532 ++RedrawingDisabled;
21533 save_sourcing_name = sourcing_name;
21534 save_sourcing_lnum = sourcing_lnum;
21535 sourcing_lnum = 1;
21536 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021537 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538 if (sourcing_name != NULL)
21539 {
21540 if (save_sourcing_name != NULL
21541 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21542 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21543 else
21544 STRCPY(sourcing_name, "function ");
21545 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21546
21547 if (p_verbose >= 12)
21548 {
21549 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021550 verbose_enter_scroll();
21551
Bram Moolenaar555b2802005-05-19 21:08:39 +000021552 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553 if (p_verbose >= 14)
21554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021555 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021556 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021557 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021558 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559
21560 msg_puts((char_u *)"(");
21561 for (i = 0; i < argcount; ++i)
21562 {
21563 if (i > 0)
21564 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021565 if (argvars[i].v_type == VAR_NUMBER)
21566 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567 else
21568 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021569 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21570 if (s != NULL)
21571 {
21572 trunc_string(s, buf, MSG_BUF_CLEN);
21573 msg_puts(buf);
21574 vim_free(tofree);
21575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576 }
21577 }
21578 msg_puts((char_u *)")");
21579 }
21580 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021581
21582 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583 --no_wait_return;
21584 }
21585 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021586#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021587 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021588 {
21589 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21590 func_do_profile(fp);
21591 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021592 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021593 {
21594 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021595 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021596 profile_zero(&fp->uf_tm_children);
21597 }
21598 script_prof_save(&wait_start);
21599 }
21600#endif
21601
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021603 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 save_did_emsg = did_emsg;
21605 did_emsg = FALSE;
21606
21607 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021608 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21610
21611 --RedrawingDisabled;
21612
21613 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021614 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021616 clear_tv(rettv);
21617 rettv->v_type = VAR_NUMBER;
21618 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619 }
21620
Bram Moolenaar05159a02005-02-26 23:04:13 +000021621#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021622 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021623 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021624 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021625 profile_end(&call_start);
21626 profile_sub_wait(&wait_start, &call_start);
21627 profile_add(&fp->uf_tm_total, &call_start);
21628 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021629 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021630 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021631 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21632 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021633 }
21634 }
21635#endif
21636
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637 /* when being verbose, mention the return value */
21638 if (p_verbose >= 12)
21639 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021641 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021644 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021645 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021646 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021647 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021648 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021650 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021651 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021652 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021653 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021654
Bram Moolenaar555b2802005-05-19 21:08:39 +000021655 /* The value may be very long. Skip the middle part, so that we
21656 * have some idea how it starts and ends. smsg() would always
21657 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021658 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021659 if (s != NULL)
21660 {
21661 trunc_string(s, buf, MSG_BUF_CLEN);
21662 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21663 vim_free(tofree);
21664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665 }
21666 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021667
21668 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669 --no_wait_return;
21670 }
21671
21672 vim_free(sourcing_name);
21673 sourcing_name = save_sourcing_name;
21674 sourcing_lnum = save_sourcing_lnum;
21675 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021676#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021677 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021678 script_prof_restore(&wait_start);
21679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680
21681 if (p_verbose >= 12 && sourcing_name != NULL)
21682 {
21683 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021684 verbose_enter_scroll();
21685
Bram Moolenaar555b2802005-05-19 21:08:39 +000021686 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021687 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021688
21689 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690 --no_wait_return;
21691 }
21692
21693 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021694 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021695 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021696
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021697 /* If the a:000 list and the l: and a: dicts are not referenced we can
21698 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021699 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21700 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21701 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21702 {
21703 free_funccal(fc, FALSE);
21704 }
21705 else
21706 {
21707 hashitem_T *hi;
21708 listitem_T *li;
21709 int todo;
21710
21711 /* "fc" is still in use. This can happen when returning "a:000" or
21712 * assigning "l:" to a global variable.
21713 * Link "fc" in the list for garbage collection later. */
21714 fc->caller = previous_funccal;
21715 previous_funccal = fc;
21716
21717 /* Make a copy of the a: variables, since we didn't do that above. */
21718 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21719 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21720 {
21721 if (!HASHITEM_EMPTY(hi))
21722 {
21723 --todo;
21724 v = HI2DI(hi);
21725 copy_tv(&v->di_tv, &v->di_tv);
21726 }
21727 }
21728
21729 /* Make a copy of the a:000 items, since we didn't do that above. */
21730 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21731 copy_tv(&li->li_tv, &li->li_tv);
21732 }
21733}
21734
21735/*
21736 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021737 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021738 */
21739 static int
21740can_free_funccal(fc, copyID)
21741 funccall_T *fc;
21742 int copyID;
21743{
21744 return (fc->l_varlist.lv_copyID != copyID
21745 && fc->l_vars.dv_copyID != copyID
21746 && fc->l_avars.dv_copyID != copyID);
21747}
21748
21749/*
21750 * Free "fc" and what it contains.
21751 */
21752 static void
21753free_funccal(fc, free_val)
21754 funccall_T *fc;
21755 int free_val; /* a: vars were allocated */
21756{
21757 listitem_T *li;
21758
21759 /* The a: variables typevals may not have been allocated, only free the
21760 * allocated variables. */
21761 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21762
21763 /* free all l: variables */
21764 vars_clear(&fc->l_vars.dv_hashtab);
21765
21766 /* Free the a:000 variables if they were allocated. */
21767 if (free_val)
21768 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21769 clear_tv(&li->li_tv);
21770
21771 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021772}
21773
21774/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021775 * Add a number variable "name" to dict "dp" with value "nr".
21776 */
21777 static void
21778add_nr_var(dp, v, name, nr)
21779 dict_T *dp;
21780 dictitem_T *v;
21781 char *name;
21782 varnumber_T nr;
21783{
21784 STRCPY(v->di_key, name);
21785 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21786 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21787 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021788 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021789 v->di_tv.vval.v_number = nr;
21790}
21791
21792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021793 * ":return [expr]"
21794 */
21795 void
21796ex_return(eap)
21797 exarg_T *eap;
21798{
21799 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021800 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021801 int returning = FALSE;
21802
21803 if (current_funccal == NULL)
21804 {
21805 EMSG(_("E133: :return not inside a function"));
21806 return;
21807 }
21808
21809 if (eap->skip)
21810 ++emsg_skip;
21811
21812 eap->nextcmd = NULL;
21813 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021814 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815 {
21816 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021817 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021819 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820 }
21821 /* It's safer to return also on error. */
21822 else if (!eap->skip)
21823 {
21824 /*
21825 * Return unless the expression evaluation has been cancelled due to an
21826 * aborting error, an interrupt, or an exception.
21827 */
21828 if (!aborting())
21829 returning = do_return(eap, FALSE, TRUE, NULL);
21830 }
21831
21832 /* When skipping or the return gets pending, advance to the next command
21833 * in this line (!returning). Otherwise, ignore the rest of the line.
21834 * Following lines will be ignored by get_func_line(). */
21835 if (returning)
21836 eap->nextcmd = NULL;
21837 else if (eap->nextcmd == NULL) /* no argument */
21838 eap->nextcmd = check_nextcmd(arg);
21839
21840 if (eap->skip)
21841 --emsg_skip;
21842}
21843
21844/*
21845 * Return from a function. Possibly makes the return pending. Also called
21846 * for a pending return at the ":endtry" or after returning from an extra
21847 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021848 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021849 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 * FALSE when the return gets pending.
21851 */
21852 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021853do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854 exarg_T *eap;
21855 int reanimate;
21856 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021857 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021858{
21859 int idx;
21860 struct condstack *cstack = eap->cstack;
21861
21862 if (reanimate)
21863 /* Undo the return. */
21864 current_funccal->returned = FALSE;
21865
21866 /*
21867 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21868 * not in its finally clause (which then is to be executed next) is found.
21869 * In this case, make the ":return" pending for execution at the ":endtry".
21870 * Otherwise, return normally.
21871 */
21872 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21873 if (idx >= 0)
21874 {
21875 cstack->cs_pending[idx] = CSTP_RETURN;
21876
21877 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021878 /* A pending return again gets pending. "rettv" points to an
21879 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021881 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021882 else
21883 {
21884 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021885 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021887 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021889 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021890 {
21891 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021892 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021893 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894 else
21895 EMSG(_(e_outofmem));
21896 }
21897 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021898 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899
21900 if (reanimate)
21901 {
21902 /* The pending return value could be overwritten by a ":return"
21903 * without argument in a finally clause; reset the default
21904 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021905 current_funccal->rettv->v_type = VAR_NUMBER;
21906 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907 }
21908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021909 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021910 }
21911 else
21912 {
21913 current_funccal->returned = TRUE;
21914
21915 /* If the return is carried out now, store the return value. For
21916 * a return immediately after reanimation, the value is already
21917 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021918 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021920 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021921 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021923 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924 }
21925 }
21926
21927 return idx < 0;
21928}
21929
21930/*
21931 * Free the variable with a pending return value.
21932 */
21933 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021934discard_pending_return(rettv)
21935 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936{
Bram Moolenaar33570922005-01-25 22:26:29 +000021937 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938}
21939
21940/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021941 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000021942 * is an allocated string. Used by report_pending() for verbose messages.
21943 */
21944 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021945get_return_cmd(rettv)
21946 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021948 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021949 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021950 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021952 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021953 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021954 if (s == NULL)
21955 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021956
21957 STRCPY(IObuff, ":return ");
21958 STRNCPY(IObuff + 8, s, IOSIZE - 8);
21959 if (STRLEN(s) + 8 >= IOSIZE)
21960 STRCPY(IObuff + IOSIZE - 4, "...");
21961 vim_free(tofree);
21962 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963}
21964
21965/*
21966 * Get next function line.
21967 * Called by do_cmdline() to get the next line.
21968 * Returns allocated string, or NULL for end of function.
21969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970 char_u *
21971get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000021972 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021973 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000021974 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021975{
Bram Moolenaar33570922005-01-25 22:26:29 +000021976 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021977 ufunc_T *fp = fcp->func;
21978 char_u *retval;
21979 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980
21981 /* If breakpoints have been added/deleted need to check for it. */
21982 if (fcp->dbg_tick != debug_tick)
21983 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021984 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 sourcing_lnum);
21986 fcp->dbg_tick = debug_tick;
21987 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021988#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021989 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021990 func_line_end(cookie);
21991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992
Bram Moolenaar05159a02005-02-26 23:04:13 +000021993 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021994 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
21995 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996 retval = NULL;
21997 else
21998 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021999 /* Skip NULL lines (continuation lines). */
22000 while (fcp->linenr < gap->ga_len
22001 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22002 ++fcp->linenr;
22003 if (fcp->linenr >= gap->ga_len)
22004 retval = NULL;
22005 else
22006 {
22007 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22008 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022009#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022010 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022011 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022012#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 }
22015
22016 /* Did we encounter a breakpoint? */
22017 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22018 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022019 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022021 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022 sourcing_lnum);
22023 fcp->dbg_tick = debug_tick;
22024 }
22025
22026 return retval;
22027}
22028
Bram Moolenaar05159a02005-02-26 23:04:13 +000022029#if defined(FEAT_PROFILE) || defined(PROTO)
22030/*
22031 * Called when starting to read a function line.
22032 * "sourcing_lnum" must be correct!
22033 * When skipping lines it may not actually be executed, but we won't find out
22034 * until later and we need to store the time now.
22035 */
22036 void
22037func_line_start(cookie)
22038 void *cookie;
22039{
22040 funccall_T *fcp = (funccall_T *)cookie;
22041 ufunc_T *fp = fcp->func;
22042
22043 if (fp->uf_profiling && sourcing_lnum >= 1
22044 && sourcing_lnum <= fp->uf_lines.ga_len)
22045 {
22046 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022047 /* Skip continuation lines. */
22048 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22049 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022050 fp->uf_tml_execed = FALSE;
22051 profile_start(&fp->uf_tml_start);
22052 profile_zero(&fp->uf_tml_children);
22053 profile_get_wait(&fp->uf_tml_wait);
22054 }
22055}
22056
22057/*
22058 * Called when actually executing a function line.
22059 */
22060 void
22061func_line_exec(cookie)
22062 void *cookie;
22063{
22064 funccall_T *fcp = (funccall_T *)cookie;
22065 ufunc_T *fp = fcp->func;
22066
22067 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22068 fp->uf_tml_execed = TRUE;
22069}
22070
22071/*
22072 * Called when done with a function line.
22073 */
22074 void
22075func_line_end(cookie)
22076 void *cookie;
22077{
22078 funccall_T *fcp = (funccall_T *)cookie;
22079 ufunc_T *fp = fcp->func;
22080
22081 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22082 {
22083 if (fp->uf_tml_execed)
22084 {
22085 ++fp->uf_tml_count[fp->uf_tml_idx];
22086 profile_end(&fp->uf_tml_start);
22087 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022088 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022089 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22090 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022091 }
22092 fp->uf_tml_idx = -1;
22093 }
22094}
22095#endif
22096
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097/*
22098 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022099 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100 */
22101 int
22102func_has_ended(cookie)
22103 void *cookie;
22104{
Bram Moolenaar33570922005-01-25 22:26:29 +000022105 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022106
22107 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22108 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022109 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110 || fcp->returned);
22111}
22112
22113/*
22114 * return TRUE if cookie indicates a function which "abort"s on errors.
22115 */
22116 int
22117func_has_abort(cookie)
22118 void *cookie;
22119{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022120 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022121}
22122
22123#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22124typedef enum
22125{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022126 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22127 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22128 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129} var_flavour_T;
22130
22131static var_flavour_T var_flavour __ARGS((char_u *varname));
22132
22133 static var_flavour_T
22134var_flavour(varname)
22135 char_u *varname;
22136{
22137 char_u *p = varname;
22138
22139 if (ASCII_ISUPPER(*p))
22140 {
22141 while (*(++p))
22142 if (ASCII_ISLOWER(*p))
22143 return VAR_FLAVOUR_SESSION;
22144 return VAR_FLAVOUR_VIMINFO;
22145 }
22146 else
22147 return VAR_FLAVOUR_DEFAULT;
22148}
22149#endif
22150
22151#if defined(FEAT_VIMINFO) || defined(PROTO)
22152/*
22153 * Restore global vars that start with a capital from the viminfo file
22154 */
22155 int
22156read_viminfo_varlist(virp, writing)
22157 vir_T *virp;
22158 int writing;
22159{
22160 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022161 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022162 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163
22164 if (!writing && (find_viminfo_parameter('!') != NULL))
22165 {
22166 tab = vim_strchr(virp->vir_line + 1, '\t');
22167 if (tab != NULL)
22168 {
22169 *tab++ = '\0'; /* isolate the variable name */
22170 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022171 type = VAR_STRING;
22172#ifdef FEAT_FLOAT
22173 else if (*tab == 'F')
22174 type = VAR_FLOAT;
22175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022176
22177 tab = vim_strchr(tab, '\t');
22178 if (tab != NULL)
22179 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022180 tv.v_type = type;
22181 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022182 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022183 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022184#ifdef FEAT_FLOAT
22185 else if (type == VAR_FLOAT)
22186 (void)string2float(tab + 1, &tv.vval.v_float);
22187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022189 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022190 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022191 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022192 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022193 }
22194 }
22195 }
22196
22197 return viminfo_readline(virp);
22198}
22199
22200/*
22201 * Write global vars that start with a capital to the viminfo file
22202 */
22203 void
22204write_viminfo_varlist(fp)
22205 FILE *fp;
22206{
Bram Moolenaar33570922005-01-25 22:26:29 +000022207 hashitem_T *hi;
22208 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022209 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022210 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022211 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022212 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022213 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022214
22215 if (find_viminfo_parameter('!') == NULL)
22216 return;
22217
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022218 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022219
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022220 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022221 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022223 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022225 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022226 this_var = HI2DI(hi);
22227 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022228 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022229 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022230 {
22231 case VAR_STRING: s = "STR"; break;
22232 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022233#ifdef FEAT_FLOAT
22234 case VAR_FLOAT: s = "FLO"; break;
22235#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022236 default: continue;
22237 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022238 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022239 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022240 if (p != NULL)
22241 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022242 vim_free(tofree);
22243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022244 }
22245 }
22246}
22247#endif
22248
22249#if defined(FEAT_SESSION) || defined(PROTO)
22250 int
22251store_session_globals(fd)
22252 FILE *fd;
22253{
Bram Moolenaar33570922005-01-25 22:26:29 +000022254 hashitem_T *hi;
22255 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022256 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022257 char_u *p, *t;
22258
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022259 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022260 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022261 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022262 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022264 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022265 this_var = HI2DI(hi);
22266 if ((this_var->di_tv.v_type == VAR_NUMBER
22267 || this_var->di_tv.v_type == VAR_STRING)
22268 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022269 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022270 /* Escape special characters with a backslash. Turn a LF and
22271 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022272 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022273 (char_u *)"\\\"\n\r");
22274 if (p == NULL) /* out of memory */
22275 break;
22276 for (t = p; *t != NUL; ++t)
22277 if (*t == '\n')
22278 *t = 'n';
22279 else if (*t == '\r')
22280 *t = 'r';
22281 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022282 this_var->di_key,
22283 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22284 : ' ',
22285 p,
22286 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22287 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022288 || put_eol(fd) == FAIL)
22289 {
22290 vim_free(p);
22291 return FAIL;
22292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293 vim_free(p);
22294 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022295#ifdef FEAT_FLOAT
22296 else if (this_var->di_tv.v_type == VAR_FLOAT
22297 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22298 {
22299 float_T f = this_var->di_tv.vval.v_float;
22300 int sign = ' ';
22301
22302 if (f < 0)
22303 {
22304 f = -f;
22305 sign = '-';
22306 }
22307 if ((fprintf(fd, "let %s = %c&%f",
22308 this_var->di_key, sign, f) < 0)
22309 || put_eol(fd) == FAIL)
22310 return FAIL;
22311 }
22312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022313 }
22314 }
22315 return OK;
22316}
22317#endif
22318
Bram Moolenaar661b1822005-07-28 22:36:45 +000022319/*
22320 * Display script name where an item was last set.
22321 * Should only be invoked when 'verbose' is non-zero.
22322 */
22323 void
22324last_set_msg(scriptID)
22325 scid_T scriptID;
22326{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022327 char_u *p;
22328
Bram Moolenaar661b1822005-07-28 22:36:45 +000022329 if (scriptID != 0)
22330 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022331 p = home_replace_save(NULL, get_scriptname(scriptID));
22332 if (p != NULL)
22333 {
22334 verbose_enter();
22335 MSG_PUTS(_("\n\tLast set from "));
22336 MSG_PUTS(p);
22337 vim_free(p);
22338 verbose_leave();
22339 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022340 }
22341}
22342
Bram Moolenaard812df62008-11-09 12:46:09 +000022343/*
22344 * List v:oldfiles in a nice way.
22345 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022346 void
22347ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022348 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022349{
22350 list_T *l = vimvars[VV_OLDFILES].vv_list;
22351 listitem_T *li;
22352 int nr = 0;
22353
22354 if (l == NULL)
22355 msg((char_u *)_("No old files"));
22356 else
22357 {
22358 msg_start();
22359 msg_scroll = TRUE;
22360 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22361 {
22362 msg_outnum((long)++nr);
22363 MSG_PUTS(": ");
22364 msg_outtrans(get_tv_string(&li->li_tv));
22365 msg_putchar('\n');
22366 out_flush(); /* output one line at a time */
22367 ui_breakcheck();
22368 }
22369 /* Assume "got_int" was set to truncate the listing. */
22370 got_int = FALSE;
22371
22372#ifdef FEAT_BROWSE_CMD
22373 if (cmdmod.browse)
22374 {
22375 quit_more = FALSE;
22376 nr = prompt_for_number(FALSE);
22377 msg_starthere();
22378 if (nr > 0)
22379 {
22380 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22381 (long)nr);
22382
22383 if (p != NULL)
22384 {
22385 p = expand_env_save(p);
22386 eap->arg = p;
22387 eap->cmdidx = CMD_edit;
22388 cmdmod.browse = FALSE;
22389 do_exedit(eap, NULL);
22390 vim_free(p);
22391 }
22392 }
22393 }
22394#endif
22395 }
22396}
22397
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398#endif /* FEAT_EVAL */
22399
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022401#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402
22403#ifdef WIN3264
22404/*
22405 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22406 */
22407static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22408static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22409static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22410
22411/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022412 * Get the short path (8.3) for the filename in "fnamep".
22413 * Only works for a valid file name.
22414 * When the path gets longer "fnamep" is changed and the allocated buffer
22415 * is put in "bufp".
22416 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22417 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418 */
22419 static int
22420get_short_pathname(fnamep, bufp, fnamelen)
22421 char_u **fnamep;
22422 char_u **bufp;
22423 int *fnamelen;
22424{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022425 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 char_u *newbuf;
22427
22428 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429 l = GetShortPathName(*fnamep, *fnamep, len);
22430 if (l > len - 1)
22431 {
22432 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022433 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434 newbuf = vim_strnsave(*fnamep, l);
22435 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022436 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437
22438 vim_free(*bufp);
22439 *fnamep = *bufp = newbuf;
22440
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022441 /* Really should always succeed, as the buffer is big enough. */
22442 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443 }
22444
22445 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022446 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447}
22448
22449/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022450 * Get the short path (8.3) for the filename in "fname". The converted
22451 * path is returned in "bufp".
22452 *
22453 * Some of the directories specified in "fname" may not exist. This function
22454 * will shorten the existing directories at the beginning of the path and then
22455 * append the remaining non-existing path.
22456 *
22457 * fname - Pointer to the filename to shorten. On return, contains the
22458 * pointer to the shortened pathname
22459 * bufp - Pointer to an allocated buffer for the filename.
22460 * fnamelen - Length of the filename pointed to by fname
22461 *
22462 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 */
22464 static int
22465shortpath_for_invalid_fname(fname, bufp, fnamelen)
22466 char_u **fname;
22467 char_u **bufp;
22468 int *fnamelen;
22469{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022470 char_u *short_fname, *save_fname, *pbuf_unused;
22471 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022473 int old_len, len;
22474 int new_len, sfx_len;
22475 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022476
22477 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022478 old_len = *fnamelen;
22479 save_fname = vim_strnsave(*fname, old_len);
22480 pbuf_unused = NULL;
22481 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022482
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022483 endp = save_fname + old_len - 1; /* Find the end of the copy */
22484 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022486 /*
22487 * Try shortening the supplied path till it succeeds by removing one
22488 * directory at a time from the tail of the path.
22489 */
22490 len = 0;
22491 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022493 /* go back one path-separator */
22494 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22495 --endp;
22496 if (endp <= save_fname)
22497 break; /* processed the complete path */
22498
22499 /*
22500 * Replace the path separator with a NUL and try to shorten the
22501 * resulting path.
22502 */
22503 ch = *endp;
22504 *endp = 0;
22505 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022506 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022507 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22508 {
22509 retval = FAIL;
22510 goto theend;
22511 }
22512 *endp = ch; /* preserve the string */
22513
22514 if (len > 0)
22515 break; /* successfully shortened the path */
22516
22517 /* failed to shorten the path. Skip the path separator */
22518 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022519 }
22520
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022521 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022523 /*
22524 * Succeeded in shortening the path. Now concatenate the shortened
22525 * path with the remaining path at the tail.
22526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022528 /* Compute the length of the new path. */
22529 sfx_len = (int)(save_endp - endp) + 1;
22530 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022532 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022533 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022534 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022536 /* There is not enough space in the currently allocated string,
22537 * copy it to a buffer big enough. */
22538 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022539 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022540 {
22541 retval = FAIL;
22542 goto theend;
22543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544 }
22545 else
22546 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022547 /* Transfer short_fname to the main buffer (it's big enough),
22548 * unless get_short_pathname() did its work in-place. */
22549 *fname = *bufp = save_fname;
22550 if (short_fname != save_fname)
22551 vim_strncpy(save_fname, short_fname, len);
22552 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022553 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022554
22555 /* concat the not-shortened part of the path */
22556 vim_strncpy(*fname + len, endp, sfx_len);
22557 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022559
22560theend:
22561 vim_free(pbuf_unused);
22562 vim_free(save_fname);
22563
22564 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565}
22566
22567/*
22568 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022569 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570 */
22571 static int
22572shortpath_for_partial(fnamep, bufp, fnamelen)
22573 char_u **fnamep;
22574 char_u **bufp;
22575 int *fnamelen;
22576{
22577 int sepcount, len, tflen;
22578 char_u *p;
22579 char_u *pbuf, *tfname;
22580 int hasTilde;
22581
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022582 /* Count up the path separators from the RHS.. so we know which part
22583 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022585 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586 if (vim_ispathsep(*p))
22587 ++sepcount;
22588
22589 /* Need full path first (use expand_env() to remove a "~/") */
22590 hasTilde = (**fnamep == '~');
22591 if (hasTilde)
22592 pbuf = tfname = expand_env_save(*fnamep);
22593 else
22594 pbuf = tfname = FullName_save(*fnamep, FALSE);
22595
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022596 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022598 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22599 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022600
22601 if (len == 0)
22602 {
22603 /* Don't have a valid filename, so shorten the rest of the
22604 * path if we can. This CAN give us invalid 8.3 filenames, but
22605 * there's not a lot of point in guessing what it might be.
22606 */
22607 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022608 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22609 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 }
22611
22612 /* Count the paths backward to find the beginning of the desired string. */
22613 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022614 {
22615#ifdef FEAT_MBYTE
22616 if (has_mbyte)
22617 p -= mb_head_off(tfname, p);
22618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022619 if (vim_ispathsep(*p))
22620 {
22621 if (sepcount == 0 || (hasTilde && sepcount == 1))
22622 break;
22623 else
22624 sepcount --;
22625 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627 if (hasTilde)
22628 {
22629 --p;
22630 if (p >= tfname)
22631 *p = '~';
22632 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022633 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 }
22635 else
22636 ++p;
22637
22638 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22639 vim_free(*bufp);
22640 *fnamelen = (int)STRLEN(p);
22641 *bufp = pbuf;
22642 *fnamep = p;
22643
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022644 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645}
22646#endif /* WIN3264 */
22647
22648/*
22649 * Adjust a filename, according to a string of modifiers.
22650 * *fnamep must be NUL terminated when called. When returning, the length is
22651 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022652 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653 * When there is an error, *fnamep is set to NULL.
22654 */
22655 int
22656modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22657 char_u *src; /* string with modifiers */
22658 int *usedlen; /* characters after src that are used */
22659 char_u **fnamep; /* file name so far */
22660 char_u **bufp; /* buffer for allocated file name or NULL */
22661 int *fnamelen; /* length of fnamep */
22662{
22663 int valid = 0;
22664 char_u *tail;
22665 char_u *s, *p, *pbuf;
22666 char_u dirname[MAXPATHL];
22667 int c;
22668 int has_fullname = 0;
22669#ifdef WIN3264
22670 int has_shortname = 0;
22671#endif
22672
22673repeat:
22674 /* ":p" - full path/file_name */
22675 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22676 {
22677 has_fullname = 1;
22678
22679 valid |= VALID_PATH;
22680 *usedlen += 2;
22681
22682 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22683 if ((*fnamep)[0] == '~'
22684#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22685 && ((*fnamep)[1] == '/'
22686# ifdef BACKSLASH_IN_FILENAME
22687 || (*fnamep)[1] == '\\'
22688# endif
22689 || (*fnamep)[1] == NUL)
22690
22691#endif
22692 )
22693 {
22694 *fnamep = expand_env_save(*fnamep);
22695 vim_free(*bufp); /* free any allocated file name */
22696 *bufp = *fnamep;
22697 if (*fnamep == NULL)
22698 return -1;
22699 }
22700
22701 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022702 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703 {
22704 if (vim_ispathsep(*p)
22705 && p[1] == '.'
22706 && (p[2] == NUL
22707 || vim_ispathsep(p[2])
22708 || (p[2] == '.'
22709 && (p[3] == NUL || vim_ispathsep(p[3])))))
22710 break;
22711 }
22712
22713 /* FullName_save() is slow, don't use it when not needed. */
22714 if (*p != NUL || !vim_isAbsName(*fnamep))
22715 {
22716 *fnamep = FullName_save(*fnamep, *p != NUL);
22717 vim_free(*bufp); /* free any allocated file name */
22718 *bufp = *fnamep;
22719 if (*fnamep == NULL)
22720 return -1;
22721 }
22722
22723 /* Append a path separator to a directory. */
22724 if (mch_isdir(*fnamep))
22725 {
22726 /* Make room for one or two extra characters. */
22727 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22728 vim_free(*bufp); /* free any allocated file name */
22729 *bufp = *fnamep;
22730 if (*fnamep == NULL)
22731 return -1;
22732 add_pathsep(*fnamep);
22733 }
22734 }
22735
22736 /* ":." - path relative to the current directory */
22737 /* ":~" - path relative to the home directory */
22738 /* ":8" - shortname path - postponed till after */
22739 while (src[*usedlen] == ':'
22740 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22741 {
22742 *usedlen += 2;
22743 if (c == '8')
22744 {
22745#ifdef WIN3264
22746 has_shortname = 1; /* Postpone this. */
22747#endif
22748 continue;
22749 }
22750 pbuf = NULL;
22751 /* Need full path first (use expand_env() to remove a "~/") */
22752 if (!has_fullname)
22753 {
22754 if (c == '.' && **fnamep == '~')
22755 p = pbuf = expand_env_save(*fnamep);
22756 else
22757 p = pbuf = FullName_save(*fnamep, FALSE);
22758 }
22759 else
22760 p = *fnamep;
22761
22762 has_fullname = 0;
22763
22764 if (p != NULL)
22765 {
22766 if (c == '.')
22767 {
22768 mch_dirname(dirname, MAXPATHL);
22769 s = shorten_fname(p, dirname);
22770 if (s != NULL)
22771 {
22772 *fnamep = s;
22773 if (pbuf != NULL)
22774 {
22775 vim_free(*bufp); /* free any allocated file name */
22776 *bufp = pbuf;
22777 pbuf = NULL;
22778 }
22779 }
22780 }
22781 else
22782 {
22783 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22784 /* Only replace it when it starts with '~' */
22785 if (*dirname == '~')
22786 {
22787 s = vim_strsave(dirname);
22788 if (s != NULL)
22789 {
22790 *fnamep = s;
22791 vim_free(*bufp);
22792 *bufp = s;
22793 }
22794 }
22795 }
22796 vim_free(pbuf);
22797 }
22798 }
22799
22800 tail = gettail(*fnamep);
22801 *fnamelen = (int)STRLEN(*fnamep);
22802
22803 /* ":h" - head, remove "/file_name", can be repeated */
22804 /* Don't remove the first "/" or "c:\" */
22805 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22806 {
22807 valid |= VALID_HEAD;
22808 *usedlen += 2;
22809 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022810 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022811 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812 *fnamelen = (int)(tail - *fnamep);
22813#ifdef VMS
22814 if (*fnamelen > 0)
22815 *fnamelen += 1; /* the path separator is part of the path */
22816#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022817 if (*fnamelen == 0)
22818 {
22819 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22820 p = vim_strsave((char_u *)".");
22821 if (p == NULL)
22822 return -1;
22823 vim_free(*bufp);
22824 *bufp = *fnamep = tail = p;
22825 *fnamelen = 1;
22826 }
22827 else
22828 {
22829 while (tail > s && !after_pathsep(s, tail))
22830 mb_ptr_back(*fnamep, tail);
22831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022832 }
22833
22834 /* ":8" - shortname */
22835 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22836 {
22837 *usedlen += 2;
22838#ifdef WIN3264
22839 has_shortname = 1;
22840#endif
22841 }
22842
22843#ifdef WIN3264
22844 /* Check shortname after we have done 'heads' and before we do 'tails'
22845 */
22846 if (has_shortname)
22847 {
22848 pbuf = NULL;
22849 /* Copy the string if it is shortened by :h */
22850 if (*fnamelen < (int)STRLEN(*fnamep))
22851 {
22852 p = vim_strnsave(*fnamep, *fnamelen);
22853 if (p == 0)
22854 return -1;
22855 vim_free(*bufp);
22856 *bufp = *fnamep = p;
22857 }
22858
22859 /* Split into two implementations - makes it easier. First is where
22860 * there isn't a full name already, second is where there is.
22861 */
22862 if (!has_fullname && !vim_isAbsName(*fnamep))
22863 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022864 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865 return -1;
22866 }
22867 else
22868 {
22869 int l;
22870
22871 /* Simple case, already have the full-name
22872 * Nearly always shorter, so try first time. */
22873 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022874 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 return -1;
22876
22877 if (l == 0)
22878 {
22879 /* Couldn't find the filename.. search the paths.
22880 */
22881 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022882 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022883 return -1;
22884 }
22885 *fnamelen = l;
22886 }
22887 }
22888#endif /* WIN3264 */
22889
22890 /* ":t" - tail, just the basename */
22891 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22892 {
22893 *usedlen += 2;
22894 *fnamelen -= (int)(tail - *fnamep);
22895 *fnamep = tail;
22896 }
22897
22898 /* ":e" - extension, can be repeated */
22899 /* ":r" - root, without extension, can be repeated */
22900 while (src[*usedlen] == ':'
22901 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22902 {
22903 /* find a '.' in the tail:
22904 * - for second :e: before the current fname
22905 * - otherwise: The last '.'
22906 */
22907 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22908 s = *fnamep - 2;
22909 else
22910 s = *fnamep + *fnamelen - 1;
22911 for ( ; s > tail; --s)
22912 if (s[0] == '.')
22913 break;
22914 if (src[*usedlen + 1] == 'e') /* :e */
22915 {
22916 if (s > tail)
22917 {
22918 *fnamelen += (int)(*fnamep - (s + 1));
22919 *fnamep = s + 1;
22920#ifdef VMS
22921 /* cut version from the extension */
22922 s = *fnamep + *fnamelen - 1;
22923 for ( ; s > *fnamep; --s)
22924 if (s[0] == ';')
22925 break;
22926 if (s > *fnamep)
22927 *fnamelen = s - *fnamep;
22928#endif
22929 }
22930 else if (*fnamep <= tail)
22931 *fnamelen = 0;
22932 }
22933 else /* :r */
22934 {
22935 if (s > tail) /* remove one extension */
22936 *fnamelen = (int)(s - *fnamep);
22937 }
22938 *usedlen += 2;
22939 }
22940
22941 /* ":s?pat?foo?" - substitute */
22942 /* ":gs?pat?foo?" - global substitute */
22943 if (src[*usedlen] == ':'
22944 && (src[*usedlen + 1] == 's'
22945 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
22946 {
22947 char_u *str;
22948 char_u *pat;
22949 char_u *sub;
22950 int sep;
22951 char_u *flags;
22952 int didit = FALSE;
22953
22954 flags = (char_u *)"";
22955 s = src + *usedlen + 2;
22956 if (src[*usedlen + 1] == 'g')
22957 {
22958 flags = (char_u *)"g";
22959 ++s;
22960 }
22961
22962 sep = *s++;
22963 if (sep)
22964 {
22965 /* find end of pattern */
22966 p = vim_strchr(s, sep);
22967 if (p != NULL)
22968 {
22969 pat = vim_strnsave(s, (int)(p - s));
22970 if (pat != NULL)
22971 {
22972 s = p + 1;
22973 /* find end of substitution */
22974 p = vim_strchr(s, sep);
22975 if (p != NULL)
22976 {
22977 sub = vim_strnsave(s, (int)(p - s));
22978 str = vim_strnsave(*fnamep, *fnamelen);
22979 if (sub != NULL && str != NULL)
22980 {
22981 *usedlen = (int)(p + 1 - src);
22982 s = do_string_sub(str, pat, sub, flags);
22983 if (s != NULL)
22984 {
22985 *fnamep = s;
22986 *fnamelen = (int)STRLEN(s);
22987 vim_free(*bufp);
22988 *bufp = s;
22989 didit = TRUE;
22990 }
22991 }
22992 vim_free(sub);
22993 vim_free(str);
22994 }
22995 vim_free(pat);
22996 }
22997 }
22998 /* after using ":s", repeat all the modifiers */
22999 if (didit)
23000 goto repeat;
23001 }
23002 }
23003
23004 return valid;
23005}
23006
23007/*
23008 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23009 * "flags" can be "g" to do a global substitute.
23010 * Returns an allocated string, NULL for error.
23011 */
23012 char_u *
23013do_string_sub(str, pat, sub, flags)
23014 char_u *str;
23015 char_u *pat;
23016 char_u *sub;
23017 char_u *flags;
23018{
23019 int sublen;
23020 regmatch_T regmatch;
23021 int i;
23022 int do_all;
23023 char_u *tail;
23024 garray_T ga;
23025 char_u *ret;
23026 char_u *save_cpo;
23027
23028 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23029 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023030 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023031
23032 ga_init2(&ga, 1, 200);
23033
23034 do_all = (flags[0] == 'g');
23035
23036 regmatch.rm_ic = p_ic;
23037 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23038 if (regmatch.regprog != NULL)
23039 {
23040 tail = str;
23041 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23042 {
23043 /*
23044 * Get some space for a temporary buffer to do the substitution
23045 * into. It will contain:
23046 * - The text up to where the match is.
23047 * - The substituted text.
23048 * - The text after the match.
23049 */
23050 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23051 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23052 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23053 {
23054 ga_clear(&ga);
23055 break;
23056 }
23057
23058 /* copy the text up to where the match is */
23059 i = (int)(regmatch.startp[0] - tail);
23060 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23061 /* add the substituted text */
23062 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23063 + ga.ga_len + i, TRUE, TRUE, FALSE);
23064 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 /* avoid getting stuck on a match with an empty string */
23066 if (tail == regmatch.endp[0])
23067 {
23068 if (*tail == NUL)
23069 break;
23070 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23071 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023072 }
23073 else
23074 {
23075 tail = regmatch.endp[0];
23076 if (*tail == NUL)
23077 break;
23078 }
23079 if (!do_all)
23080 break;
23081 }
23082
23083 if (ga.ga_data != NULL)
23084 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23085
23086 vim_free(regmatch.regprog);
23087 }
23088
23089 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23090 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023091 if (p_cpo == empty_option)
23092 p_cpo = save_cpo;
23093 else
23094 /* Darn, evaluating {sub} expression changed the value. */
23095 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096
23097 return ret;
23098}
23099
23100#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */