blob: 6ad71d5ae709d7811f3937c8e665977b66148aec [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarc236c162008-07-13 17:41:49 +000013#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019#if defined(FEAT_EVAL) || defined(PROTO)
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef AMIGA
22# include <time.h> /* for strftime() */
23#endif
24
25#ifdef MACOS
26# include <time.h> /* for time_t */
27#endif
28
Bram Moolenaar8c8de832008-06-24 22:58:06 +000029#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
30# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000035#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
36 be freed. */
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038/*
Bram Moolenaar33570922005-01-25 22:26:29 +000039 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
40 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000041 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
42 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
43 * HI2DI() converts a hashitem pointer to a dictitem pointer.
44 */
Bram Moolenaar33570922005-01-25 22:26:29 +000045static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000047#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000048#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000049
50/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000051 * Structure returned by get_lval() and used by set_var_lval().
52 * For a plain name:
53 * "name" points to the variable name.
54 * "exp_name" is NULL.
55 * "tv" is NULL
56 * For a magic braces name:
57 * "name" points to the expanded variable name.
58 * "exp_name" is non-NULL, to be freed later.
59 * "tv" is NULL
60 * For an index in a list:
61 * "name" points to the (expanded) variable name.
62 * "exp_name" NULL or non-NULL, to be freed later.
63 * "tv" points to the (first) list item value
64 * "li" points to the (first) list item
65 * "range", "n1", "n2" and "empty2" indicate what items are used.
66 * For an existing Dict item:
67 * "name" points to the (expanded) variable name.
68 * "exp_name" NULL or non-NULL, to be freed later.
69 * "tv" points to the dict item value
70 * "newkey" is NULL
71 * For a non-existing Dict item:
72 * "name" points to the (expanded) variable name.
73 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000074 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000075 * "newkey" is the key for the new item.
76 */
77typedef struct lval_S
78{
79 char_u *ll_name; /* start of variable name (can be NULL) */
80 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000082 isn't NULL it's the Dict to which to add
83 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000084 listitem_T *ll_li; /* The list item or NULL. */
85 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000086 int ll_range; /* TRUE when a [i:j] range was used */
87 long ll_n1; /* First index for list */
88 long ll_n2; /* Second index for list range */
89 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000090 dict_T *ll_dict; /* The Dictionary or NULL */
91 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000092 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000093} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000094
Bram Moolenaar8c711452005-01-14 21:53:12 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000114
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000115/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000116 * All user-defined global variables are stored in dictionary "globvardict".
117 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119static dict_T globvardict;
120static dictitem_T globvars_var;
121#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124 * Old Vim variables such as "v:version" are also available without the "v:".
125 * Also in functions. We need a special hashtable for them.
126 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000127static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000128
129/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000130 * When recursively copying lists and dicts we need to remember which ones we
131 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000132 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133 */
134static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000135#define COPYID_INC 2
136#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137
138/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000139 * Array to hold the hashtab with variables local to each sourced script.
140 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000142typedef struct
143{
144 dictitem_T sv_var;
145 dict_T sv_dict;
146} scriptvar_T;
147
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200148static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
149#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
150#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152static int echo_attr = 0; /* attributes used for ":echo" */
153
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000154/* Values for trans_function_name() argument: */
155#define TFN_INT 1 /* internal function name OK */
156#define TFN_QUIET 2 /* no error messages */
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158/*
159 * Structure to hold info for a user function.
160 */
161typedef struct ufunc ufunc_T;
162
163struct ufunc
164{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000165 int uf_varargs; /* variable nr of arguments */
166 int uf_flags;
167 int uf_calls; /* nr of active calls */
168 garray_T uf_args; /* arguments */
169 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000170#ifdef FEAT_PROFILE
171 int uf_profiling; /* TRUE when func is being profiled */
172 /* profiling the function as a whole */
173 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000174 proftime_T uf_tm_total; /* time spent in function + children */
175 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000176 proftime_T uf_tm_children; /* time spent in children this call */
177 /* profiling the function per line */
178 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000179 proftime_T *uf_tml_total; /* time spent in a line + children */
180 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000181 proftime_T uf_tml_start; /* start time for current line */
182 proftime_T uf_tml_children; /* time spent in children for this line */
183 proftime_T uf_tml_wait; /* start wait time for current line */
184 int uf_tml_idx; /* index of line being timed; -1 if none */
185 int uf_tml_execed; /* line being timed was executed */
186#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000187 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000189 int uf_refcount; /* for numbered function: reference count */
190 char_u uf_name[1]; /* name of function (actually longer); can
191 start with <SNR>123_ (<SNR> is K_SPECIAL
192 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193};
194
195/* function flags */
196#define FC_ABORT 1 /* abort function on error */
197#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000198#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199
200/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000203static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000205/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000206static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
207
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208/* list heads for garbage collection */
209static dict_T *first_dict = NULL; /* list of all dicts */
210static list_T *first_list = NULL; /* list of all lists */
211
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000212/* From user function to hashitem and back. */
213static ufunc_T dumuf;
214#define UF2HIKEY(fp) ((fp)->uf_name)
215#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
216#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
217
218#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
219#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220
Bram Moolenaar33570922005-01-25 22:26:29 +0000221#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
222#define VAR_SHORT_LEN 20 /* short variable name length */
223#define FIXVAR_CNT 12 /* number of fixed variables */
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000226typedef struct funccall_S funccall_T;
227
228struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229{
230 ufunc_T *func; /* function being called */
231 int linenr; /* next line to be executed */
232 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000233 struct /* fixed variables for arguments */
234 {
235 dictitem_T var; /* variable (without room for name) */
236 char_u room[VAR_SHORT_LEN]; /* room for the name */
237 } fixvar[FIXVAR_CNT];
238 dict_T l_vars; /* l: local function variables */
239 dictitem_T l_vars_var; /* variable for l: scope */
240 dict_T l_avars; /* a: argument variables */
241 dictitem_T l_avars_var; /* variable for a: scope */
242 list_T l_varlist; /* list for a:000 */
243 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
244 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 linenr_T breakpoint; /* next line with breakpoint or zero */
246 int dbg_tick; /* debug_tick when breakpoint was set */
247 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000248#ifdef FEAT_PROFILE
249 proftime_T prof_child; /* time spent in a child */
250#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000251 funccall_T *caller; /* calling function or NULL */
252};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253
254/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000255 * Info used by a ":for" loop.
256 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000257typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258{
259 int fi_semicolon; /* TRUE if ending in '; var]' */
260 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261 listwatch_T fi_lw; /* keep an eye on the item used. */
262 list_T *fi_list; /* list being used */
263} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000266 * Struct used by trans_function_name()
267 */
268typedef struct
269{
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000271 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000272 dictitem_T *fd_di; /* Dictionary item used */
273} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000274
Bram Moolenaara7043832005-01-21 11:56:39 +0000275
276/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 * Array to hold the value of v: variables.
278 * The value is in a dictitem, so that it can also be used in the v: scope.
279 * The reason to use this table anyway is for very quick access to the
280 * variables with the VV_ defines.
281 */
282#include "version.h"
283
284/* values for vv_flags: */
285#define VV_COMPAT 1 /* compatible, also used without "v:" */
286#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000287#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000288
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000289#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000290
291static struct vimvar
292{
293 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294 dictitem_T vv_di; /* value and name for key */
295 char vv_filler[16]; /* space for LONGEST name below!!! */
296 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
297} vimvars[VV_LEN] =
298{
299 /*
300 * The order here must match the VV_ defines in vim.h!
301 * Initializing a union does not work, leave tv.vval empty to get zero's.
302 */
303 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
304 {VV_NAME("count1", VAR_NUMBER), VV_RO},
305 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
306 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
307 {VV_NAME("warningmsg", VAR_STRING), 0},
308 {VV_NAME("statusmsg", VAR_STRING), 0},
309 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
311 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
313 {VV_NAME("termresponse", VAR_STRING), VV_RO},
314 {VV_NAME("fname", VAR_STRING), VV_RO},
315 {VV_NAME("lang", VAR_STRING), VV_RO},
316 {VV_NAME("lc_time", VAR_STRING), VV_RO},
317 {VV_NAME("ctype", VAR_STRING), VV_RO},
318 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
319 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
320 {VV_NAME("fname_in", VAR_STRING), VV_RO},
321 {VV_NAME("fname_out", VAR_STRING), VV_RO},
322 {VV_NAME("fname_new", VAR_STRING), VV_RO},
323 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
324 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
325 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
326 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
327 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
328 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
329 {VV_NAME("progname", VAR_STRING), VV_RO},
330 {VV_NAME("servername", VAR_STRING), VV_RO},
331 {VV_NAME("dying", VAR_NUMBER), VV_RO},
332 {VV_NAME("exception", VAR_STRING), VV_RO},
333 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
334 {VV_NAME("register", VAR_STRING), VV_RO},
335 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
336 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000337 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
338 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000339 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000340 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
341 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000342 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
343 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
344 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
345 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
346 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000347 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000348 {VV_NAME("swapname", VAR_STRING), VV_RO},
349 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000350 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000351 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000352 {VV_NAME("mouse_win", VAR_NUMBER), 0},
353 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
354 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000355 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000356 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000357 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000358};
359
360/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000361#define vv_type vv_di.di_tv.v_type
362#define vv_nr vv_di.di_tv.vval.v_number
363#define vv_float vv_di.di_tv.vval.v_float
364#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000365#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000367
368/*
369 * The v: variables are stored in dictionary "vimvardict".
370 * "vimvars_var" is the variable that is used for the "l:" scope.
371 */
372static dict_T vimvardict;
373static dictitem_T vimvars_var;
374#define vimvarht vimvardict.dv_hashtab
375
Bram Moolenaara40058a2005-07-11 22:42:07 +0000376static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
377static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
378#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
379static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
380#endif
381static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
382static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
383static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000384static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
385static void list_glob_vars __ARGS((int *first));
386static void list_buf_vars __ARGS((int *first));
387static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000388#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000389static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_vim_vars __ARGS((int *first));
392static void list_script_vars __ARGS((int *first));
393static void list_func_vars __ARGS((int *first));
394static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000395static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
396static int check_changedtick __ARGS((char_u *arg));
397static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
398static void clear_lval __ARGS((lval_T *lp));
399static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
400static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
401static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
402static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
403static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
404static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
405static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
406static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
407static void item_lock __ARGS((typval_T *tv, int deep, int lock));
408static int tv_islocked __ARGS((typval_T *tv));
409
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
411static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000416static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
417static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000418
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000419static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000424static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static listitem_T *listitem_alloc __ARGS((void));
426static void listitem_free __ARGS((listitem_T *item));
427static void listitem_remove __ARGS((list_T *l, listitem_T *item));
428static long list_len __ARGS((list_T *l));
429static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
430static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
431static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000433static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000436static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
438static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
439static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000440static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static char_u *list2string __ARGS((typval_T *tv, int copyID));
443static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000444static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000445static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
446static void set_ref_in_list __ARGS((list_T *l, int copyID));
447static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000449static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000450static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
451static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000452static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static long dict_len __ARGS((dict_T *d));
454static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000455static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000457static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
458static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
461static int string2float __ARGS((char_u *text, float_T *value));
462#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
464static int find_internal_func __ARGS((char_u *name));
465static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
466static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +0200467static int call_func __ARGS((char_u *func_name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000468static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000469static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#ifdef FEAT_FLOAT
472static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200473static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000475static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000480#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200481static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200483static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#ifdef FEAT_FLOAT
497static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
498#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000499static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000500static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000502static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000504#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000505static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000506static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
508#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#ifdef FEAT_FLOAT
512static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200513static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
518static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200529#ifdef FEAT_FLOAT
530static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
531#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000532static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000534static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000540#ifdef FEAT_FLOAT
541static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200543static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000544#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000545static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000546static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000554static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000556static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000562static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000570static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000571static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000572static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000573static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200576static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000577static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000585static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000599static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000605static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000617#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200618static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000619static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000626static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000627static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000629static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000633#ifdef vim_mkdir
634static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
635#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100637#ifdef FEAT_MZSCHEME
638static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000642static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000643#ifdef FEAT_FLOAT
644static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
645#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000646static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000647static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000648static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000649static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000650static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000651static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000663#ifdef FEAT_FLOAT
664static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
665#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000667static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000669static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000676static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000677static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000678static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000679static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200681static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000682static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000684static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000686#ifdef FEAT_FLOAT
687static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200688static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000691static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000692static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
693static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000695#ifdef FEAT_FLOAT
696static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
697static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
698#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000699static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700#ifdef HAVE_STRFTIME
701static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
702#endif
703static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000714static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000716static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000717static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000718static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000719static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000720static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000722static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200723#ifdef FEAT_FLOAT
724static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
726#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000730#ifdef FEAT_FLOAT
731static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
732#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200734static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000744static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000747static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000748
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000749static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000750static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static int get_env_len __ARGS((char_u **arg));
752static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000753static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000754static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
755#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
756#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
757 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000758static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000760static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000761static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
762static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static typval_T *alloc_tv __ARGS((void));
764static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static void init_tv __ARGS((typval_T *varp));
766static long get_tv_number __ARGS((typval_T *varp));
767static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000768static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static char_u *get_tv_string __ARGS((typval_T *varp));
770static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000771static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000772static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000773static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
775static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
776static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000777static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
778static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
780static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000781static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000782static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000783static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
785static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
786static int eval_fname_script __ARGS((char_u *p));
787static int eval_fname_sid __ARGS((char_u *p));
788static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000789static ufunc_T *find_func __ARGS((char_u *name));
790static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000791static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000792#ifdef FEAT_PROFILE
793static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000794static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
795static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
796static int
797# ifdef __BORLANDC__
798 _RTLENTRYF
799# endif
800 prof_total_cmp __ARGS((const void *s1, const void *s2));
801static int
802# ifdef __BORLANDC__
803 _RTLENTRYF
804# endif
805 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000807static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000808static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000809static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000810static void func_free __ARGS((ufunc_T *fp));
811static void func_unref __ARGS((char_u *name));
812static void func_ref __ARGS((char_u *name));
813static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000814static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
815static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000816static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000817static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
818static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000819static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000820static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000821static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200823
824#ifdef EBCDIC
825static int compare_func_name __ARGS((const void *s1, const void *s2));
826static void sortFunctions __ARGS(());
827#endif
828
829
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000830/* Character used as separated in autoload function/variable names. */
831#define AUTOLOAD_CHAR '#'
832
Bram Moolenaar33570922005-01-25 22:26:29 +0000833/*
834 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000835 */
836 void
837eval_init()
838{
Bram Moolenaar33570922005-01-25 22:26:29 +0000839 int i;
840 struct vimvar *p;
841
842 init_var_dict(&globvardict, &globvars_var);
843 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000844 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000845 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000846
847 for (i = 0; i < VV_LEN; ++i)
848 {
849 p = &vimvars[i];
850 STRCPY(p->vv_di.di_key, p->vv_name);
851 if (p->vv_flags & VV_RO)
852 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
853 else if (p->vv_flags & VV_RO_SBX)
854 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
855 else
856 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000857
858 /* add to v: scope dict, unless the value is not always available */
859 if (p->vv_type != VAR_UNKNOWN)
860 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000861 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000862 /* add to compat scope dict */
863 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000864 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000865 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200866
867#ifdef EBCDIC
868 /*
869 * Sort the function table, to enable binary sort.
870 */
871 sortFunctions();
872#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000873}
874
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000875#if defined(EXITFREE) || defined(PROTO)
876 void
877eval_clear()
878{
879 int i;
880 struct vimvar *p;
881
882 for (i = 0; i < VV_LEN; ++i)
883 {
884 p = &vimvars[i];
885 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000886 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000887 vim_free(p->vv_str);
888 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000889 }
890 else if (p->vv_di.di_tv.v_type == VAR_LIST)
891 {
892 list_unref(p->vv_list);
893 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000894 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000895 }
896 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000897 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000898 hash_clear(&compat_hashtab);
899
Bram Moolenaard9fba312005-06-26 22:34:35 +0000900 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000901
902 /* global variables */
903 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000904
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000905 /* autoloaded script names */
906 ga_clear_strings(&ga_loaded);
907
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200908 /* script-local variables */
909 for (i = 1; i <= ga_scripts.ga_len; ++i)
910 {
911 vars_clear(&SCRIPT_VARS(i));
912 vim_free(SCRIPT_SV(i));
913 }
914 ga_clear(&ga_scripts);
915
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000916 /* unreferenced lists and dicts */
917 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000918
919 /* functions */
920 free_all_functions();
921 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000922}
923#endif
924
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 * Return the name of the executed function.
927 */
928 char_u *
929func_name(cookie)
930 void *cookie;
931{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000932 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933}
934
935/*
936 * Return the address holding the next breakpoint line for a funccall cookie.
937 */
938 linenr_T *
939func_breakpoint(cookie)
940 void *cookie;
941{
Bram Moolenaar33570922005-01-25 22:26:29 +0000942 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943}
944
945/*
946 * Return the address holding the debug tick for a funccall cookie.
947 */
948 int *
949func_dbg_tick(cookie)
950 void *cookie;
951{
Bram Moolenaar33570922005-01-25 22:26:29 +0000952 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953}
954
955/*
956 * Return the nesting level for a funccall cookie.
957 */
958 int
959func_level(cookie)
960 void *cookie;
961{
Bram Moolenaar33570922005-01-25 22:26:29 +0000962 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000966funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000968/* pointer to list of previously used funccal, still around because some
969 * item in it is still being used. */
970funccall_T *previous_funccal = NULL;
971
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972/*
973 * Return TRUE when a function was ended by a ":return" command.
974 */
975 int
976current_func_returned()
977{
978 return current_funccal->returned;
979}
980
981
982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 * Set an internal variable to a string value. Creates the variable if it does
984 * not already exist.
985 */
986 void
987set_internal_string_var(name, value)
988 char_u *name;
989 char_u *value;
990{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000991 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000992 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994 val = vim_strsave(value);
995 if (val != NULL)
996 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000997 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000998 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001000 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001001 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 }
1003 }
1004}
1005
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001006static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001007static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001008static char_u *redir_endp = NULL;
1009static char_u *redir_varname = NULL;
1010
1011/*
1012 * Start recording command output to a variable
1013 * Returns OK if successfully completed the setup. FAIL otherwise.
1014 */
1015 int
1016var_redir_start(name, append)
1017 char_u *name;
1018 int append; /* append to an existing variable */
1019{
1020 int save_emsg;
1021 int err;
1022 typval_T tv;
1023
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001024 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001025 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001026 {
1027 EMSG(_(e_invarg));
1028 return FAIL;
1029 }
1030
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001031 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001032 redir_varname = vim_strsave(name);
1033 if (redir_varname == NULL)
1034 return FAIL;
1035
1036 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1037 if (redir_lval == NULL)
1038 {
1039 var_redir_stop();
1040 return FAIL;
1041 }
1042
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001043 /* The output is stored in growarray "redir_ga" until redirection ends. */
1044 ga_init2(&redir_ga, (int)sizeof(char), 500);
1045
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001047 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1048 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1050 {
1051 if (redir_endp != NULL && *redir_endp != NUL)
1052 /* Trailing characters are present after the variable name */
1053 EMSG(_(e_trailing));
1054 else
1055 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001056 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 var_redir_stop();
1058 return FAIL;
1059 }
1060
1061 /* check if we can write to the variable: set it to or append an empty
1062 * string */
1063 save_emsg = did_emsg;
1064 did_emsg = FALSE;
1065 tv.v_type = VAR_STRING;
1066 tv.vval.v_string = (char_u *)"";
1067 if (append)
1068 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1069 else
1070 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1071 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001072 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001073 if (err)
1074 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001075 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 var_redir_stop();
1077 return FAIL;
1078 }
1079 if (redir_lval->ll_newkey != NULL)
1080 {
1081 /* Dictionary item was created, don't do it again. */
1082 vim_free(redir_lval->ll_newkey);
1083 redir_lval->ll_newkey = NULL;
1084 }
1085
1086 return OK;
1087}
1088
1089/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001090 * Append "value[value_len]" to the variable set by var_redir_start().
1091 * The actual appending is postponed until redirection ends, because the value
1092 * appended may in fact be the string we write to, changing it may cause freed
1093 * memory to be used:
1094 * :redir => foo
1095 * :let foo
1096 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097 */
1098 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001099var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001101 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001103 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104
1105 if (redir_lval == NULL)
1106 return;
1107
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001108 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001109 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001111 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001113 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 {
1115 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001116 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001117 }
1118 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120}
1121
1122/*
1123 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001124 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 */
1126 void
1127var_redir_stop()
1128{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001129 typval_T tv;
1130
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 if (redir_lval != NULL)
1132 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001133 /* If there was no error: assign the text to the variable. */
1134 if (redir_endp != NULL)
1135 {
1136 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1137 tv.v_type = VAR_STRING;
1138 tv.vval.v_string = redir_ga.ga_data;
1139 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1140 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001141
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001142 /* free the collected output */
1143 vim_free(redir_ga.ga_data);
1144 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001145
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146 clear_lval(redir_lval);
1147 vim_free(redir_lval);
1148 redir_lval = NULL;
1149 }
1150 vim_free(redir_varname);
1151 redir_varname = NULL;
1152}
1153
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154# if defined(FEAT_MBYTE) || defined(PROTO)
1155 int
1156eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1157 char_u *enc_from;
1158 char_u *enc_to;
1159 char_u *fname_from;
1160 char_u *fname_to;
1161{
1162 int err = FALSE;
1163
1164 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1165 set_vim_var_string(VV_CC_TO, enc_to, -1);
1166 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1167 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1168 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1169 err = TRUE;
1170 set_vim_var_string(VV_CC_FROM, NULL, -1);
1171 set_vim_var_string(VV_CC_TO, NULL, -1);
1172 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1173 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1174
1175 if (err)
1176 return FAIL;
1177 return OK;
1178}
1179# endif
1180
1181# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1182 int
1183eval_printexpr(fname, args)
1184 char_u *fname;
1185 char_u *args;
1186{
1187 int err = FALSE;
1188
1189 set_vim_var_string(VV_FNAME_IN, fname, -1);
1190 set_vim_var_string(VV_CMDARG, args, -1);
1191 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1192 err = TRUE;
1193 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1194 set_vim_var_string(VV_CMDARG, NULL, -1);
1195
1196 if (err)
1197 {
1198 mch_remove(fname);
1199 return FAIL;
1200 }
1201 return OK;
1202}
1203# endif
1204
1205# if defined(FEAT_DIFF) || defined(PROTO)
1206 void
1207eval_diff(origfile, newfile, outfile)
1208 char_u *origfile;
1209 char_u *newfile;
1210 char_u *outfile;
1211{
1212 int err = FALSE;
1213
1214 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1215 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1216 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1217 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1218 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1219 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1220 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1221}
1222
1223 void
1224eval_patch(origfile, difffile, outfile)
1225 char_u *origfile;
1226 char_u *difffile;
1227 char_u *outfile;
1228{
1229 int err;
1230
1231 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1232 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1233 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1234 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1235 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1236 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1237 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1238}
1239# endif
1240
1241/*
1242 * Top level evaluation function, returning a boolean.
1243 * Sets "error" to TRUE if there was an error.
1244 * Return TRUE or FALSE.
1245 */
1246 int
1247eval_to_bool(arg, error, nextcmd, skip)
1248 char_u *arg;
1249 int *error;
1250 char_u **nextcmd;
1251 int skip; /* only parse, don't execute */
1252{
Bram Moolenaar33570922005-01-25 22:26:29 +00001253 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 int retval = FALSE;
1255
1256 if (skip)
1257 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001258 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 else
1261 {
1262 *error = FALSE;
1263 if (!skip)
1264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001265 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001266 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 }
1268 }
1269 if (skip)
1270 --emsg_skip;
1271
1272 return retval;
1273}
1274
1275/*
1276 * Top level evaluation function, returning a string. If "skip" is TRUE,
1277 * only parsing to "nextcmd" is done, without reporting errors. Return
1278 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1279 */
1280 char_u *
1281eval_to_string_skip(arg, nextcmd, skip)
1282 char_u *arg;
1283 char_u **nextcmd;
1284 int skip; /* only parse, don't execute */
1285{
Bram Moolenaar33570922005-01-25 22:26:29 +00001286 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 char_u *retval;
1288
1289 if (skip)
1290 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001291 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 retval = NULL;
1293 else
1294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001295 retval = vim_strsave(get_tv_string(&tv));
1296 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 }
1298 if (skip)
1299 --emsg_skip;
1300
1301 return retval;
1302}
1303
1304/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001305 * Skip over an expression at "*pp".
1306 * Return FAIL for an error, OK otherwise.
1307 */
1308 int
1309skip_expr(pp)
1310 char_u **pp;
1311{
Bram Moolenaar33570922005-01-25 22:26:29 +00001312 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001313
1314 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001315 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001316}
1317
1318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001320 * When "convert" is TRUE convert a List into a sequence of lines and convert
1321 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 * Return pointer to allocated memory, or NULL for failure.
1323 */
1324 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001325eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 char_u *arg;
1327 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001328 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329{
Bram Moolenaar33570922005-01-25 22:26:29 +00001330 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001332 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001333#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001337 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 retval = NULL;
1339 else
1340 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001341 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001342 {
1343 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001344 if (tv.vval.v_list != NULL)
1345 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001346 ga_append(&ga, NUL);
1347 retval = (char_u *)ga.ga_data;
1348 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349#ifdef FEAT_FLOAT
1350 else if (convert && tv.v_type == VAR_FLOAT)
1351 {
1352 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1353 retval = vim_strsave(numbuf);
1354 }
1355#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 else
1357 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001358 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 }
1360
1361 return retval;
1362}
1363
1364/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001365 * Call eval_to_string() without using current local variables and using
1366 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 */
1368 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001369eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 char_u *arg;
1371 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001372 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373{
1374 char_u *retval;
1375 void *save_funccalp;
1376
1377 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001378 if (use_sandbox)
1379 ++sandbox;
1380 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001382 if (use_sandbox)
1383 --sandbox;
1384 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 restore_funccal(save_funccalp);
1386 return retval;
1387}
1388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389/*
1390 * Top level evaluation function, returning a number.
1391 * Evaluates "expr" silently.
1392 * Returns -1 for an error.
1393 */
1394 int
1395eval_to_number(expr)
1396 char_u *expr;
1397{
Bram Moolenaar33570922005-01-25 22:26:29 +00001398 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001400 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401
1402 ++emsg_off;
1403
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001404 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 retval = -1;
1406 else
1407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001408 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001409 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 }
1411 --emsg_off;
1412
1413 return retval;
1414}
1415
Bram Moolenaara40058a2005-07-11 22:42:07 +00001416/*
1417 * Prepare v: variable "idx" to be used.
1418 * Save the current typeval in "save_tv".
1419 * When not used yet add the variable to the v: hashtable.
1420 */
1421 static void
1422prepare_vimvar(idx, save_tv)
1423 int idx;
1424 typval_T *save_tv;
1425{
1426 *save_tv = vimvars[idx].vv_tv;
1427 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1428 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1429}
1430
1431/*
1432 * Restore v: variable "idx" to typeval "save_tv".
1433 * When no longer defined, remove the variable from the v: hashtable.
1434 */
1435 static void
1436restore_vimvar(idx, save_tv)
1437 int idx;
1438 typval_T *save_tv;
1439{
1440 hashitem_T *hi;
1441
Bram Moolenaara40058a2005-07-11 22:42:07 +00001442 vimvars[idx].vv_tv = *save_tv;
1443 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1444 {
1445 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1446 if (HASHITEM_EMPTY(hi))
1447 EMSG2(_(e_intern2), "restore_vimvar()");
1448 else
1449 hash_remove(&vimvarht, hi);
1450 }
1451}
1452
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001453#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001454/*
1455 * Evaluate an expression to a list with suggestions.
1456 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001457 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001458 */
1459 list_T *
1460eval_spell_expr(badword, expr)
1461 char_u *badword;
1462 char_u *expr;
1463{
1464 typval_T save_val;
1465 typval_T rettv;
1466 list_T *list = NULL;
1467 char_u *p = skipwhite(expr);
1468
1469 /* Set "v:val" to the bad word. */
1470 prepare_vimvar(VV_VAL, &save_val);
1471 vimvars[VV_VAL].vv_type = VAR_STRING;
1472 vimvars[VV_VAL].vv_str = badword;
1473 if (p_verbose == 0)
1474 ++emsg_off;
1475
1476 if (eval1(&p, &rettv, TRUE) == OK)
1477 {
1478 if (rettv.v_type != VAR_LIST)
1479 clear_tv(&rettv);
1480 else
1481 list = rettv.vval.v_list;
1482 }
1483
1484 if (p_verbose == 0)
1485 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001486 restore_vimvar(VV_VAL, &save_val);
1487
1488 return list;
1489}
1490
1491/*
1492 * "list" is supposed to contain two items: a word and a number. Return the
1493 * word in "pp" and the number as the return value.
1494 * Return -1 if anything isn't right.
1495 * Used to get the good word and score from the eval_spell_expr() result.
1496 */
1497 int
1498get_spellword(list, pp)
1499 list_T *list;
1500 char_u **pp;
1501{
1502 listitem_T *li;
1503
1504 li = list->lv_first;
1505 if (li == NULL)
1506 return -1;
1507 *pp = get_tv_string(&li->li_tv);
1508
1509 li = li->li_next;
1510 if (li == NULL)
1511 return -1;
1512 return get_tv_number(&li->li_tv);
1513}
1514#endif
1515
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001516/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001517 * Top level evaluation function.
1518 * Returns an allocated typval_T with the result.
1519 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001520 */
1521 typval_T *
1522eval_expr(arg, nextcmd)
1523 char_u *arg;
1524 char_u **nextcmd;
1525{
1526 typval_T *tv;
1527
1528 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001529 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001530 {
1531 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001532 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001533 }
1534
1535 return tv;
1536}
1537
1538
Bram Moolenaar4f688582007-07-24 12:34:30 +00001539#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1540 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001542 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001543 * Uses argv[argc] for the function arguments. Only Number and String
1544 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001545 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001547 static int
1548call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 char_u *func;
1550 int argc;
1551 char_u **argv;
1552 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
Bram Moolenaar33570922005-01-25 22:26:29 +00001555 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 long n;
1557 int len;
1558 int i;
1559 int doesrange;
1560 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001563 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
1567 for (i = 0; i < argc; i++)
1568 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001569 /* Pass a NULL or empty argument as an empty string */
1570 if (argv[i] == NULL || *argv[i] == NUL)
1571 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001572 argvars[i].v_type = VAR_STRING;
1573 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001574 continue;
1575 }
1576
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 /* Recognize a number argument, the others must be strings. */
1578 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1579 if (len != 0 && len == (int)STRLEN(argv[i]))
1580 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001581 argvars[i].v_type = VAR_NUMBER;
1582 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 }
1584 else
1585 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001586 argvars[i].v_type = VAR_STRING;
1587 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 }
1589 }
1590
1591 if (safe)
1592 {
1593 save_funccalp = save_funccal();
1594 ++sandbox;
1595 }
1596
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001597 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1598 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001600 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 if (safe)
1602 {
1603 --sandbox;
1604 restore_funccal(save_funccalp);
1605 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001606 vim_free(argvars);
1607
1608 if (ret == FAIL)
1609 clear_tv(rettv);
1610
1611 return ret;
1612}
1613
Bram Moolenaar4f688582007-07-24 12:34:30 +00001614# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001616 * Call vimL function "func" and return the result as a string.
1617 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001618 * Uses argv[argc] for the function arguments.
1619 */
1620 void *
1621call_func_retstr(func, argc, argv, safe)
1622 char_u *func;
1623 int argc;
1624 char_u **argv;
1625 int safe; /* use the sandbox */
1626{
1627 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001628 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001629
1630 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1631 return NULL;
1632
1633 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 return retval;
1636}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001637# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001638
Bram Moolenaar4f688582007-07-24 12:34:30 +00001639# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001640/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001641 * Call vimL function "func" and return the result as a number.
1642 * Returns -1 when calling the function fails.
1643 * Uses argv[argc] for the function arguments.
1644 */
1645 long
1646call_func_retnr(func, argc, argv, safe)
1647 char_u *func;
1648 int argc;
1649 char_u **argv;
1650 int safe; /* use the sandbox */
1651{
1652 typval_T rettv;
1653 long retval;
1654
1655 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1656 return -1;
1657
1658 retval = get_tv_number_chk(&rettv, NULL);
1659 clear_tv(&rettv);
1660 return retval;
1661}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001662# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001663
1664/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001665 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001666 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001667 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001668 */
1669 void *
1670call_func_retlist(func, argc, argv, safe)
1671 char_u *func;
1672 int argc;
1673 char_u **argv;
1674 int safe; /* use the sandbox */
1675{
1676 typval_T rettv;
1677
1678 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1679 return NULL;
1680
1681 if (rettv.v_type != VAR_LIST)
1682 {
1683 clear_tv(&rettv);
1684 return NULL;
1685 }
1686
1687 return rettv.vval.v_list;
1688}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689#endif
1690
Bram Moolenaar4f688582007-07-24 12:34:30 +00001691
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692/*
1693 * Save the current function call pointer, and set it to NULL.
1694 * Used when executing autocommands and for ":source".
1695 */
1696 void *
1697save_funccal()
1698{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001699 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 current_funccal = NULL;
1702 return (void *)fc;
1703}
1704
1705 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001706restore_funccal(vfc)
1707 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001709 funccall_T *fc = (funccall_T *)vfc;
1710
1711 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712}
1713
Bram Moolenaar05159a02005-02-26 23:04:13 +00001714#if defined(FEAT_PROFILE) || defined(PROTO)
1715/*
1716 * Prepare profiling for entering a child or something else that is not
1717 * counted for the script/function itself.
1718 * Should always be called in pair with prof_child_exit().
1719 */
1720 void
1721prof_child_enter(tm)
1722 proftime_T *tm; /* place to store waittime */
1723{
1724 funccall_T *fc = current_funccal;
1725
1726 if (fc != NULL && fc->func->uf_profiling)
1727 profile_start(&fc->prof_child);
1728 script_prof_save(tm);
1729}
1730
1731/*
1732 * Take care of time spent in a child.
1733 * Should always be called after prof_child_enter().
1734 */
1735 void
1736prof_child_exit(tm)
1737 proftime_T *tm; /* where waittime was stored */
1738{
1739 funccall_T *fc = current_funccal;
1740
1741 if (fc != NULL && fc->func->uf_profiling)
1742 {
1743 profile_end(&fc->prof_child);
1744 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1745 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1746 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1747 }
1748 script_prof_restore(tm);
1749}
1750#endif
1751
1752
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753#ifdef FEAT_FOLDING
1754/*
1755 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1756 * it in "*cp". Doesn't give error messages.
1757 */
1758 int
1759eval_foldexpr(arg, cp)
1760 char_u *arg;
1761 int *cp;
1762{
Bram Moolenaar33570922005-01-25 22:26:29 +00001763 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 int retval;
1765 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001766 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1767 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
1769 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001770 if (use_sandbox)
1771 ++sandbox;
1772 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001774 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 retval = 0;
1776 else
1777 {
1778 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779 if (tv.v_type == VAR_NUMBER)
1780 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001781 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 retval = 0;
1783 else
1784 {
1785 /* If the result is a string, check if there is a non-digit before
1786 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 if (!VIM_ISDIGIT(*s) && *s != '-')
1789 *cp = *s++;
1790 retval = atol((char *)s);
1791 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001792 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 }
1794 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001795 if (use_sandbox)
1796 --sandbox;
1797 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
1799 return retval;
1800}
1801#endif
1802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001804 * ":let" list all variable values
1805 * ":let var1 var2" list variable values
1806 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001807 * ":let var += expr" assignment command.
1808 * ":let var -= expr" assignment command.
1809 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001810 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 */
1812 void
1813ex_let(eap)
1814 exarg_T *eap;
1815{
1816 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001818 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 int var_count = 0;
1821 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001822 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001823 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001824 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
Bram Moolenaardb552d602006-03-23 22:59:57 +00001826 argend = skip_var_list(arg, &var_count, &semicolon);
1827 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001828 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001829 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1830 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001831 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001832 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001834 /*
1835 * ":let" without "=": list variables
1836 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001837 if (*arg == '[')
1838 EMSG(_(e_invarg));
1839 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001841 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001843 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001845 list_glob_vars(&first);
1846 list_buf_vars(&first);
1847 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001848#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001849 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001850#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001851 list_script_vars(&first);
1852 list_func_vars(&first);
1853 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 eap->nextcmd = check_nextcmd(arg);
1856 }
1857 else
1858 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001859 op[0] = '=';
1860 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001861 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001862 {
1863 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1864 op[0] = expr[-1]; /* +=, -= or .= */
1865 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001866 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 if (eap->skip)
1869 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001870 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if (eap->skip)
1872 {
1873 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 --emsg_skip;
1876 }
1877 else if (i != FAIL)
1878 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001880 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 }
1883 }
1884}
1885
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886/*
1887 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1888 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001889 * When "nextchars" is not NULL it points to a string with characters that
1890 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1891 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 * Returns OK or FAIL;
1893 */
1894 static int
1895ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1896 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001897 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001898 int copy; /* copy values from "tv", don't move */
1899 int semicolon; /* from skip_var_list() */
1900 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902{
1903 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001904 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001906 listitem_T *item;
1907 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001908
1909 if (*arg != '[')
1910 {
1911 /*
1912 * ":let var = expr" or ":for var in list"
1913 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 return FAIL;
1916 return OK;
1917 }
1918
1919 /*
1920 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1921 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001922 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923 {
1924 EMSG(_(e_listreq));
1925 return FAIL;
1926 }
1927
1928 i = list_len(l);
1929 if (semicolon == 0 && var_count < i)
1930 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001931 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 return FAIL;
1933 }
1934 if (var_count - semicolon > i)
1935 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001936 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 return FAIL;
1938 }
1939
1940 item = l->lv_first;
1941 while (*arg != ']')
1942 {
1943 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945 item = item->li_next;
1946 if (arg == NULL)
1947 return FAIL;
1948
1949 arg = skipwhite(arg);
1950 if (*arg == ';')
1951 {
1952 /* Put the rest of the list (may be empty) in the var after ';'.
1953 * Create a new list for this. */
1954 l = list_alloc();
1955 if (l == NULL)
1956 return FAIL;
1957 while (item != NULL)
1958 {
1959 list_append_tv(l, &item->li_tv);
1960 item = item->li_next;
1961 }
1962
1963 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001964 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 ltv.vval.v_list = l;
1966 l->lv_refcount = 1;
1967
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001968 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1969 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970 clear_tv(&ltv);
1971 if (arg == NULL)
1972 return FAIL;
1973 break;
1974 }
1975 else if (*arg != ',' && *arg != ']')
1976 {
1977 EMSG2(_(e_intern2), "ex_let_vars()");
1978 return FAIL;
1979 }
1980 }
1981
1982 return OK;
1983}
1984
1985/*
1986 * Skip over assignable variable "var" or list of variables "[var, var]".
1987 * Used for ":let varvar = expr" and ":for varvar in expr".
1988 * For "[var, var]" increment "*var_count" for each variable.
1989 * for "[var, var; var]" set "semicolon".
1990 * Return NULL for an error.
1991 */
1992 static char_u *
1993skip_var_list(arg, var_count, semicolon)
1994 char_u *arg;
1995 int *var_count;
1996 int *semicolon;
1997{
1998 char_u *p, *s;
1999
2000 if (*arg == '[')
2001 {
2002 /* "[var, var]": find the matching ']'. */
2003 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002004 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002005 {
2006 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2007 s = skip_var_one(p);
2008 if (s == p)
2009 {
2010 EMSG2(_(e_invarg2), p);
2011 return NULL;
2012 }
2013 ++*var_count;
2014
2015 p = skipwhite(s);
2016 if (*p == ']')
2017 break;
2018 else if (*p == ';')
2019 {
2020 if (*semicolon == 1)
2021 {
2022 EMSG(_("Double ; in list of variables"));
2023 return NULL;
2024 }
2025 *semicolon = 1;
2026 }
2027 else if (*p != ',')
2028 {
2029 EMSG2(_(e_invarg2), p);
2030 return NULL;
2031 }
2032 }
2033 return p + 1;
2034 }
2035 else
2036 return skip_var_one(arg);
2037}
2038
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002039/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002040 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002041 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002042 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002043 static char_u *
2044skip_var_one(arg)
2045 char_u *arg;
2046{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002047 if (*arg == '@' && arg[1] != NUL)
2048 return arg + 2;
2049 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2050 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051}
2052
Bram Moolenaara7043832005-01-21 11:56:39 +00002053/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002054 * List variables for hashtab "ht" with prefix "prefix".
2055 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002056 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002057 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002058list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002059 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002060 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002061 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002062 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002063{
Bram Moolenaar33570922005-01-25 22:26:29 +00002064 hashitem_T *hi;
2065 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002066 int todo;
2067
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002068 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002069 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2070 {
2071 if (!HASHITEM_EMPTY(hi))
2072 {
2073 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002074 di = HI2DI(hi);
2075 if (empty || di->di_tv.v_type != VAR_STRING
2076 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002077 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002078 }
2079 }
2080}
2081
2082/*
2083 * List global variables.
2084 */
2085 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002086list_glob_vars(first)
2087 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002088{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002089 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002090}
2091
2092/*
2093 * List buffer variables.
2094 */
2095 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002096list_buf_vars(first)
2097 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002098{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002099 char_u numbuf[NUMBUFLEN];
2100
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2102 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002103
2104 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2106 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002107}
2108
2109/*
2110 * List window variables.
2111 */
2112 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113list_win_vars(first)
2114 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002115{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2117 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002118}
2119
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002120#ifdef FEAT_WINDOWS
2121/*
2122 * List tab page variables.
2123 */
2124 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125list_tab_vars(first)
2126 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002127{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2129 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002130}
2131#endif
2132
Bram Moolenaara7043832005-01-21 11:56:39 +00002133/*
2134 * List Vim variables.
2135 */
2136 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137list_vim_vars(first)
2138 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002139{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002141}
2142
2143/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002144 * List script-local variables, if there is a script.
2145 */
2146 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147list_script_vars(first)
2148 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002149{
2150 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2152 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002153}
2154
2155/*
2156 * List function variables, if there is a function.
2157 */
2158 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159list_func_vars(first)
2160 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002161{
2162 if (current_funccal != NULL)
2163 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002165}
2166
2167/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168 * List variables in "arg".
2169 */
2170 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 exarg_T *eap;
2173 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175{
2176 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002177 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002178 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002179 char_u *name_start;
2180 char_u *arg_subsc;
2181 char_u *tofree;
2182 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183
2184 while (!ends_excmd(*arg) && !got_int)
2185 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002186 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002188 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002189 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2190 {
2191 emsg_severe = TRUE;
2192 EMSG(_(e_trailing));
2193 break;
2194 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002196 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002198 /* get_name_len() takes care of expanding curly braces */
2199 name_start = name = arg;
2200 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2201 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002203 /* This is mainly to keep test 49 working: when expanding
2204 * curly braces fails overrule the exception error message. */
2205 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 emsg_severe = TRUE;
2208 EMSG2(_(e_invarg2), arg);
2209 break;
2210 }
2211 error = TRUE;
2212 }
2213 else
2214 {
2215 if (tofree != NULL)
2216 name = tofree;
2217 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 else
2220 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221 /* handle d.key, l[idx], f(expr) */
2222 arg_subsc = arg;
2223 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002224 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002226 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002227 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002230 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 case 'g': list_glob_vars(first); break;
2232 case 'b': list_buf_vars(first); break;
2233 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002234#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002235 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002236#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002237 case 'v': list_vim_vars(first); break;
2238 case 's': list_script_vars(first); break;
2239 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 default:
2241 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002242 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 }
2244 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 {
2246 char_u numbuf[NUMBUFLEN];
2247 char_u *tf;
2248 int c;
2249 char_u *s;
2250
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002251 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 c = *arg;
2253 *arg = NUL;
2254 list_one_var_a((char_u *)"",
2255 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002256 tv.v_type,
2257 s == NULL ? (char_u *)"" : s,
2258 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 *arg = c;
2260 vim_free(tf);
2261 }
2262 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 }
2265 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266
2267 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269
2270 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 }
2272
2273 return arg;
2274}
2275
2276/*
2277 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2278 * Returns a pointer to the char just after the var name.
2279 * Returns NULL if there is an error.
2280 */
2281 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002282ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002284 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002286 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002287 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288{
2289 int c1;
2290 char_u *name;
2291 char_u *p;
2292 char_u *arg_end = NULL;
2293 int len;
2294 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002295 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296
2297 /*
2298 * ":let $VAR = expr": Set environment variable.
2299 */
2300 if (*arg == '$')
2301 {
2302 /* Find the end of the name. */
2303 ++arg;
2304 name = arg;
2305 len = get_env_len(&arg);
2306 if (len == 0)
2307 EMSG2(_(e_invarg2), name - 1);
2308 else
2309 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002310 if (op != NULL && (*op == '+' || *op == '-'))
2311 EMSG2(_(e_letwrong), op);
2312 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002313 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 EMSG(_(e_letunexp));
2315 else
2316 {
2317 c1 = name[len];
2318 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002319 p = get_tv_string_chk(tv);
2320 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002321 {
2322 int mustfree = FALSE;
2323 char_u *s = vim_getenv(name, &mustfree);
2324
2325 if (s != NULL)
2326 {
2327 p = tofree = concat_str(s, p);
2328 if (mustfree)
2329 vim_free(s);
2330 }
2331 }
2332 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002333 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002334 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002335 if (STRICMP(name, "HOME") == 0)
2336 init_homedir();
2337 else if (didset_vim && STRICMP(name, "VIM") == 0)
2338 didset_vim = FALSE;
2339 else if (didset_vimruntime
2340 && STRICMP(name, "VIMRUNTIME") == 0)
2341 didset_vimruntime = FALSE;
2342 arg_end = arg;
2343 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002345 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 }
2347 }
2348 }
2349
2350 /*
2351 * ":let &option = expr": Set option value.
2352 * ":let &l:option = expr": Set local option value.
2353 * ":let &g:option = expr": Set global option value.
2354 */
2355 else if (*arg == '&')
2356 {
2357 /* Find the end of the name. */
2358 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002359 if (p == NULL || (endchars != NULL
2360 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 EMSG(_(e_letunexp));
2362 else
2363 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 long n;
2365 int opt_type;
2366 long numval;
2367 char_u *stringval = NULL;
2368 char_u *s;
2369
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 c1 = *p;
2371 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372
2373 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002374 s = get_tv_string_chk(tv); /* != NULL if number or string */
2375 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002376 {
2377 opt_type = get_option_value(arg, &numval,
2378 &stringval, opt_flags);
2379 if ((opt_type == 1 && *op == '.')
2380 || (opt_type == 0 && *op != '.'))
2381 EMSG2(_(e_letwrong), op);
2382 else
2383 {
2384 if (opt_type == 1) /* number */
2385 {
2386 if (*op == '+')
2387 n = numval + n;
2388 else
2389 n = numval - n;
2390 }
2391 else if (opt_type == 0 && stringval != NULL) /* string */
2392 {
2393 s = concat_str(stringval, s);
2394 vim_free(stringval);
2395 stringval = s;
2396 }
2397 }
2398 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002399 if (s != NULL)
2400 {
2401 set_option_value(arg, n, s, opt_flags);
2402 arg_end = p;
2403 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406 }
2407 }
2408
2409 /*
2410 * ":let @r = expr": Set register contents.
2411 */
2412 else if (*arg == '@')
2413 {
2414 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 if (op != NULL && (*op == '+' || *op == '-'))
2416 EMSG2(_(e_letwrong), op);
2417 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002418 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 EMSG(_(e_letunexp));
2420 else
2421 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002422 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423 char_u *s;
2424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002425 p = get_tv_string_chk(tv);
2426 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002428 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 if (s != NULL)
2430 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002431 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 vim_free(s);
2433 }
2434 }
2435 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002436 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002438 arg_end = arg + 1;
2439 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002440 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002441 }
2442 }
2443
2444 /*
2445 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002448 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002450 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002452 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002454 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2456 EMSG(_(e_letunexp));
2457 else
2458 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 arg_end = p;
2461 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 }
2465
2466 else
2467 EMSG2(_(e_invarg2), arg);
2468
2469 return arg_end;
2470}
2471
2472/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002473 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2474 */
2475 static int
2476check_changedtick(arg)
2477 char_u *arg;
2478{
2479 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2480 {
2481 EMSG2(_(e_readonlyvar), arg);
2482 return TRUE;
2483 }
2484 return FALSE;
2485}
2486
2487/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 * Get an lval: variable, Dict item or List item that can be assigned a value
2489 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2490 * "name.key", "name.key[expr]" etc.
2491 * Indexing only works if "name" is an existing List or Dictionary.
2492 * "name" points to the start of the name.
2493 * If "rettv" is not NULL it points to the value to be assigned.
2494 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2495 * wrong; must end in space or cmd separator.
2496 *
2497 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002498 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 */
2501 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002502get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002503 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002504 typval_T *rettv;
2505 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 int unlet;
2507 int skip;
2508 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002509 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 char_u *expr_start, *expr_end;
2513 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002514 dictitem_T *v;
2515 typval_T var1;
2516 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002518 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002521 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002524 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525
2526 if (skip)
2527 {
2528 /* When skipping just find the end of the name. */
2529 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002530 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 }
2532
2533 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002534 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 if (expr_start != NULL)
2536 {
2537 /* Don't expand the name when we already know there is an error. */
2538 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2539 && *p != '[' && *p != '.')
2540 {
2541 EMSG(_(e_trailing));
2542 return NULL;
2543 }
2544
2545 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2546 if (lp->ll_exp_name == NULL)
2547 {
2548 /* Report an invalid expression in braces, unless the
2549 * expression evaluation has been cancelled due to an
2550 * aborting error, an interrupt, or an exception. */
2551 if (!aborting() && !quiet)
2552 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002553 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 EMSG2(_(e_invarg2), name);
2555 return NULL;
2556 }
2557 }
2558 lp->ll_name = lp->ll_exp_name;
2559 }
2560 else
2561 lp->ll_name = name;
2562
2563 /* Without [idx] or .key we are done. */
2564 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2565 return p;
2566
2567 cc = *p;
2568 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002569 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 if (v == NULL && !quiet)
2571 EMSG2(_(e_undefvar), lp->ll_name);
2572 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573 if (v == NULL)
2574 return NULL;
2575
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 /*
2577 * Loop until no more [idx] or .key is following.
2578 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002579 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2583 && !(lp->ll_tv->v_type == VAR_DICT
2584 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002585 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 if (!quiet)
2587 EMSG(_("E689: Can only index a List or Dictionary"));
2588 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002589 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002591 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 if (!quiet)
2593 EMSG(_("E708: [:] must come last"));
2594 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002595 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002596
Bram Moolenaar8c711452005-01-14 21:53:12 +00002597 len = -1;
2598 if (*p == '.')
2599 {
2600 key = p + 1;
2601 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2602 ;
2603 if (len == 0)
2604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (!quiet)
2606 EMSG(_(e_emptykey));
2607 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 }
2609 p = key + len;
2610 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002611 else
2612 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002614 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 if (*p == ':')
2616 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002617 else
2618 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 empty1 = FALSE;
2620 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002622 if (get_tv_string_chk(&var1) == NULL)
2623 {
2624 /* not a number or string */
2625 clear_tv(&var1);
2626 return NULL;
2627 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 }
2629
2630 /* Optionally get the second index [ :expr]. */
2631 if (*p == ':')
2632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002636 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637 if (!empty1)
2638 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002640 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 if (rettv != NULL && (rettv->v_type != VAR_LIST
2642 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (!quiet)
2645 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 if (!empty1)
2647 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 }
2650 p = skipwhite(p + 1);
2651 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 else
2654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2657 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 if (!empty1)
2659 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002662 if (get_tv_string_chk(&var2) == NULL)
2663 {
2664 /* not a number or string */
2665 if (!empty1)
2666 clear_tv(&var1);
2667 clear_tv(&var2);
2668 return NULL;
2669 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002672 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002675
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002677 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 if (!quiet)
2679 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 if (!empty1)
2681 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 }
2686
2687 /* Skip to past ']'. */
2688 ++p;
2689 }
2690
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 {
2693 if (len == -1)
2694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002696 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 if (*key == NUL)
2698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (!quiet)
2700 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 }
2704 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002705 lp->ll_list = NULL;
2706 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002707 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002710 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002714 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 if (len == -1)
2716 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (len == -1)
2724 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 p = NULL;
2727 break;
2728 }
2729 if (len == -1)
2730 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
2733 else
2734 {
2735 /*
2736 * Get the number and item for the only or first index of the List.
2737 */
2738 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 else
2741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002742 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 clear_tv(&var1);
2744 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002745 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 lp->ll_list = lp->ll_tv->vval.v_list;
2747 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2748 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002750 if (lp->ll_n1 < 0)
2751 {
2752 lp->ll_n1 = 0;
2753 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2754 }
2755 }
2756 if (lp->ll_li == NULL)
2757 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 }
2762
2763 /*
2764 * May need to find the item or absolute index for the second
2765 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 * When no index given: "lp->ll_empty2" is TRUE.
2767 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002771 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 }
2780
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2782 if (lp->ll_n1 < 0)
2783 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2784 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002786 }
2787
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002790 }
2791
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 return p;
2793}
2794
2795/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002796 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 */
2798 static void
2799clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002800 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801{
2802 vim_free(lp->ll_exp_name);
2803 vim_free(lp->ll_newkey);
2804}
2805
2806/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002807 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002809 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 */
2811 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002812set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002813 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002815 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002817 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818{
2819 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002820 listitem_T *ri;
2821 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822
2823 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002824 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002826 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 cc = *endp;
2828 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002829 if (op != NULL && *op != '=')
2830 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002831 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002832
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002833 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002834 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002835 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002836 {
2837 if (tv_op(&tv, rettv, op) == OK)
2838 set_var(lp->ll_name, &tv, FALSE);
2839 clear_tv(&tv);
2840 }
2841 }
2842 else
2843 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002845 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002847 else if (tv_check_lock(lp->ll_newkey == NULL
2848 ? lp->ll_tv->v_lock
2849 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2850 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 else if (lp->ll_range)
2852 {
2853 /*
2854 * Assign the List values to the list items.
2855 */
2856 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002857 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002858 if (op != NULL && *op != '=')
2859 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2860 else
2861 {
2862 clear_tv(&lp->ll_li->li_tv);
2863 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2864 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 ri = ri->li_next;
2866 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2867 break;
2868 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002871 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002872 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 ri = NULL;
2874 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002875 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002876 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 lp->ll_li = lp->ll_li->li_next;
2878 ++lp->ll_n1;
2879 }
2880 if (ri != NULL)
2881 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002882 else if (lp->ll_empty2
2883 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 : lp->ll_n1 != lp->ll_n2)
2885 EMSG(_("E711: List value has not enough items"));
2886 }
2887 else
2888 {
2889 /*
2890 * Assign to a List or Dictionary item.
2891 */
2892 if (lp->ll_newkey != NULL)
2893 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002894 if (op != NULL && *op != '=')
2895 {
2896 EMSG2(_(e_letwrong), op);
2897 return;
2898 }
2899
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002901 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 if (di == NULL)
2903 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002904 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2905 {
2906 vim_free(di);
2907 return;
2908 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002910 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002911 else if (op != NULL && *op != '=')
2912 {
2913 tv_op(lp->ll_tv, rettv, op);
2914 return;
2915 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002916 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002918
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 /*
2920 * Assign the value to the variable or list item.
2921 */
2922 if (copy)
2923 copy_tv(rettv, lp->ll_tv);
2924 else
2925 {
2926 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002927 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002929 }
2930 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002931}
2932
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002933/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002934 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2935 * Returns OK or FAIL.
2936 */
2937 static int
2938tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 typval_T *tv1;
2940 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002941 char_u *op;
2942{
2943 long n;
2944 char_u numbuf[NUMBUFLEN];
2945 char_u *s;
2946
2947 /* Can't do anything with a Funcref or a Dict on the right. */
2948 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2949 {
2950 switch (tv1->v_type)
2951 {
2952 case VAR_DICT:
2953 case VAR_FUNC:
2954 break;
2955
2956 case VAR_LIST:
2957 if (*op != '+' || tv2->v_type != VAR_LIST)
2958 break;
2959 /* List += List */
2960 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2961 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2962 return OK;
2963
2964 case VAR_NUMBER:
2965 case VAR_STRING:
2966 if (tv2->v_type == VAR_LIST)
2967 break;
2968 if (*op == '+' || *op == '-')
2969 {
2970 /* nr += nr or nr -= nr*/
2971 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002972#ifdef FEAT_FLOAT
2973 if (tv2->v_type == VAR_FLOAT)
2974 {
2975 float_T f = n;
2976
2977 if (*op == '+')
2978 f += tv2->vval.v_float;
2979 else
2980 f -= tv2->vval.v_float;
2981 clear_tv(tv1);
2982 tv1->v_type = VAR_FLOAT;
2983 tv1->vval.v_float = f;
2984 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002985 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002986#endif
2987 {
2988 if (*op == '+')
2989 n += get_tv_number(tv2);
2990 else
2991 n -= get_tv_number(tv2);
2992 clear_tv(tv1);
2993 tv1->v_type = VAR_NUMBER;
2994 tv1->vval.v_number = n;
2995 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 }
2997 else
2998 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002999 if (tv2->v_type == VAR_FLOAT)
3000 break;
3001
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003002 /* str .= str */
3003 s = get_tv_string(tv1);
3004 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3005 clear_tv(tv1);
3006 tv1->v_type = VAR_STRING;
3007 tv1->vval.v_string = s;
3008 }
3009 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003010
3011#ifdef FEAT_FLOAT
3012 case VAR_FLOAT:
3013 {
3014 float_T f;
3015
3016 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3017 && tv2->v_type != VAR_NUMBER
3018 && tv2->v_type != VAR_STRING))
3019 break;
3020 if (tv2->v_type == VAR_FLOAT)
3021 f = tv2->vval.v_float;
3022 else
3023 f = get_tv_number(tv2);
3024 if (*op == '+')
3025 tv1->vval.v_float += f;
3026 else
3027 tv1->vval.v_float -= f;
3028 }
3029 return OK;
3030#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003031 }
3032 }
3033
3034 EMSG2(_(e_letwrong), op);
3035 return FAIL;
3036}
3037
3038/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003039 * Add a watcher to a list.
3040 */
3041 static void
3042list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003043 list_T *l;
3044 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003045{
3046 lw->lw_next = l->lv_watch;
3047 l->lv_watch = lw;
3048}
3049
3050/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003051 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003052 * No warning when it isn't found...
3053 */
3054 static void
3055list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003056 list_T *l;
3057 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003058{
Bram Moolenaar33570922005-01-25 22:26:29 +00003059 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003060
3061 lwp = &l->lv_watch;
3062 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3063 {
3064 if (lw == lwrem)
3065 {
3066 *lwp = lw->lw_next;
3067 break;
3068 }
3069 lwp = &lw->lw_next;
3070 }
3071}
3072
3073/*
3074 * Just before removing an item from a list: advance watchers to the next
3075 * item.
3076 */
3077 static void
3078list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003079 list_T *l;
3080 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003081{
Bram Moolenaar33570922005-01-25 22:26:29 +00003082 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003083
3084 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3085 if (lw->lw_item == item)
3086 lw->lw_item = item->li_next;
3087}
3088
3089/*
3090 * Evaluate the expression used in a ":for var in expr" command.
3091 * "arg" points to "var".
3092 * Set "*errp" to TRUE for an error, FALSE otherwise;
3093 * Return a pointer that holds the info. Null when there is an error.
3094 */
3095 void *
3096eval_for_line(arg, errp, nextcmdp, skip)
3097 char_u *arg;
3098 int *errp;
3099 char_u **nextcmdp;
3100 int skip;
3101{
Bram Moolenaar33570922005-01-25 22:26:29 +00003102 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003103 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003104 typval_T tv;
3105 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003106
3107 *errp = TRUE; /* default: there is an error */
3108
Bram Moolenaar33570922005-01-25 22:26:29 +00003109 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003110 if (fi == NULL)
3111 return NULL;
3112
3113 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3114 if (expr == NULL)
3115 return fi;
3116
3117 expr = skipwhite(expr);
3118 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3119 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003120 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003121 return fi;
3122 }
3123
3124 if (skip)
3125 ++emsg_skip;
3126 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3127 {
3128 *errp = FALSE;
3129 if (!skip)
3130 {
3131 l = tv.vval.v_list;
3132 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003133 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003135 clear_tv(&tv);
3136 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003137 else
3138 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003139 /* No need to increment the refcount, it's already set for the
3140 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003141 fi->fi_list = l;
3142 list_add_watch(l, &fi->fi_lw);
3143 fi->fi_lw.lw_item = l->lv_first;
3144 }
3145 }
3146 }
3147 if (skip)
3148 --emsg_skip;
3149
3150 return fi;
3151}
3152
3153/*
3154 * Use the first item in a ":for" list. Advance to the next.
3155 * Assign the values to the variable (list). "arg" points to the first one.
3156 * Return TRUE when a valid item was found, FALSE when at end of list or
3157 * something wrong.
3158 */
3159 int
3160next_for_item(fi_void, arg)
3161 void *fi_void;
3162 char_u *arg;
3163{
Bram Moolenaar33570922005-01-25 22:26:29 +00003164 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003165 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003166 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167
3168 item = fi->fi_lw.lw_item;
3169 if (item == NULL)
3170 result = FALSE;
3171 else
3172 {
3173 fi->fi_lw.lw_item = item->li_next;
3174 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3175 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3176 }
3177 return result;
3178}
3179
3180/*
3181 * Free the structure used to store info used by ":for".
3182 */
3183 void
3184free_for_info(fi_void)
3185 void *fi_void;
3186{
Bram Moolenaar33570922005-01-25 22:26:29 +00003187 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003189 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003190 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003192 list_unref(fi->fi_list);
3193 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194 vim_free(fi);
3195}
3196
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3198
3199 void
3200set_context_for_expression(xp, arg, cmdidx)
3201 expand_T *xp;
3202 char_u *arg;
3203 cmdidx_T cmdidx;
3204{
3205 int got_eq = FALSE;
3206 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003207 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003209 if (cmdidx == CMD_let)
3210 {
3211 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003212 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213 {
3214 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003215 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216 {
3217 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003218 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219 if (vim_iswhite(*p))
3220 break;
3221 }
3222 return;
3223 }
3224 }
3225 else
3226 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3227 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 while ((xp->xp_pattern = vim_strpbrk(arg,
3229 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3230 {
3231 c = *xp->xp_pattern;
3232 if (c == '&')
3233 {
3234 c = xp->xp_pattern[1];
3235 if (c == '&')
3236 {
3237 ++xp->xp_pattern;
3238 xp->xp_context = cmdidx != CMD_let || got_eq
3239 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3240 }
3241 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003244 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3245 xp->xp_pattern += 2;
3246
3247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 }
3249 else if (c == '$')
3250 {
3251 /* environment variable */
3252 xp->xp_context = EXPAND_ENV_VARS;
3253 }
3254 else if (c == '=')
3255 {
3256 got_eq = TRUE;
3257 xp->xp_context = EXPAND_EXPRESSION;
3258 }
3259 else if (c == '<'
3260 && xp->xp_context == EXPAND_FUNCTIONS
3261 && vim_strchr(xp->xp_pattern, '(') == NULL)
3262 {
3263 /* Function name can start with "<SNR>" */
3264 break;
3265 }
3266 else if (cmdidx != CMD_let || got_eq)
3267 {
3268 if (c == '"') /* string */
3269 {
3270 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3271 if (c == '\\' && xp->xp_pattern[1] != NUL)
3272 ++xp->xp_pattern;
3273 xp->xp_context = EXPAND_NOTHING;
3274 }
3275 else if (c == '\'') /* literal string */
3276 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003277 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3279 /* skip */ ;
3280 xp->xp_context = EXPAND_NOTHING;
3281 }
3282 else if (c == '|')
3283 {
3284 if (xp->xp_pattern[1] == '|')
3285 {
3286 ++xp->xp_pattern;
3287 xp->xp_context = EXPAND_EXPRESSION;
3288 }
3289 else
3290 xp->xp_context = EXPAND_COMMANDS;
3291 }
3292 else
3293 xp->xp_context = EXPAND_EXPRESSION;
3294 }
3295 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296 /* Doesn't look like something valid, expand as an expression
3297 * anyway. */
3298 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 arg = xp->xp_pattern;
3300 if (*arg != NUL)
3301 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3302 /* skip */ ;
3303 }
3304 xp->xp_pattern = arg;
3305}
3306
3307#endif /* FEAT_CMDL_COMPL */
3308
3309/*
3310 * ":1,25call func(arg1, arg2)" function call.
3311 */
3312 void
3313ex_call(eap)
3314 exarg_T *eap;
3315{
3316 char_u *arg = eap->arg;
3317 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003319 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003321 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 linenr_T lnum;
3323 int doesrange;
3324 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003325 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003327 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003328 if (fudi.fd_newkey != NULL)
3329 {
3330 /* Still need to give an error message for missing key. */
3331 EMSG2(_(e_dictkey), fudi.fd_newkey);
3332 vim_free(fudi.fd_newkey);
3333 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003334 if (tofree == NULL)
3335 return;
3336
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003337 /* Increase refcount on dictionary, it could get deleted when evaluating
3338 * the arguments. */
3339 if (fudi.fd_dict != NULL)
3340 ++fudi.fd_dict->dv_refcount;
3341
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003342 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003343 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003344 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345
Bram Moolenaar532c7802005-01-27 14:44:31 +00003346 /* Skip white space to allow ":call func ()". Not good, but required for
3347 * backward compatibility. */
3348 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003349 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351 if (*startarg != '(')
3352 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003353 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 goto end;
3355 }
3356
3357 /*
3358 * When skipping, evaluate the function once, to find the end of the
3359 * arguments.
3360 * When the function takes a range, this is discovered after the first
3361 * call, and the loop is broken.
3362 */
3363 if (eap->skip)
3364 {
3365 ++emsg_skip;
3366 lnum = eap->line2; /* do it once, also with an invalid range */
3367 }
3368 else
3369 lnum = eap->line1;
3370 for ( ; lnum <= eap->line2; ++lnum)
3371 {
3372 if (!eap->skip && eap->addr_count > 0)
3373 {
3374 curwin->w_cursor.lnum = lnum;
3375 curwin->w_cursor.col = 0;
3376 }
3377 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003378 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003379 eap->line1, eap->line2, &doesrange,
3380 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 {
3382 failed = TRUE;
3383 break;
3384 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003385
3386 /* Handle a function returning a Funcref, Dictionary or List. */
3387 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3388 {
3389 failed = TRUE;
3390 break;
3391 }
3392
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003393 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 if (doesrange || eap->skip)
3395 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003396
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003398 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003399 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003400 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 if (aborting())
3402 break;
3403 }
3404 if (eap->skip)
3405 --emsg_skip;
3406
3407 if (!failed)
3408 {
3409 /* Check for trailing illegal characters and a following command. */
3410 if (!ends_excmd(*arg))
3411 {
3412 emsg_severe = TRUE;
3413 EMSG(_(e_trailing));
3414 }
3415 else
3416 eap->nextcmd = check_nextcmd(arg);
3417 }
3418
3419end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003420 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003421 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422}
3423
3424/*
3425 * ":unlet[!] var1 ... " command.
3426 */
3427 void
3428ex_unlet(eap)
3429 exarg_T *eap;
3430{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003431 ex_unletlock(eap, eap->arg, 0);
3432}
3433
3434/*
3435 * ":lockvar" and ":unlockvar" commands
3436 */
3437 void
3438ex_lockvar(eap)
3439 exarg_T *eap;
3440{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003442 int deep = 2;
3443
3444 if (eap->forceit)
3445 deep = -1;
3446 else if (vim_isdigit(*arg))
3447 {
3448 deep = getdigits(&arg);
3449 arg = skipwhite(arg);
3450 }
3451
3452 ex_unletlock(eap, arg, deep);
3453}
3454
3455/*
3456 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3457 */
3458 static void
3459ex_unletlock(eap, argstart, deep)
3460 exarg_T *eap;
3461 char_u *argstart;
3462 int deep;
3463{
3464 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003467 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
3469 do
3470 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003471 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003472 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3473 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003474 if (lv.ll_name == NULL)
3475 error = TRUE; /* error but continue parsing */
3476 if (name_end == NULL || (!vim_iswhite(*name_end)
3477 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003479 if (name_end != NULL)
3480 {
3481 emsg_severe = TRUE;
3482 EMSG(_(e_trailing));
3483 }
3484 if (!(eap->skip || error))
3485 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 break;
3487 }
3488
3489 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003490 {
3491 if (eap->cmdidx == CMD_unlet)
3492 {
3493 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3494 error = TRUE;
3495 }
3496 else
3497 {
3498 if (do_lock_var(&lv, name_end, deep,
3499 eap->cmdidx == CMD_lockvar) == FAIL)
3500 error = TRUE;
3501 }
3502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003504 if (!eap->skip)
3505 clear_lval(&lv);
3506
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 arg = skipwhite(name_end);
3508 } while (!ends_excmd(*arg));
3509
3510 eap->nextcmd = check_nextcmd(arg);
3511}
3512
Bram Moolenaar8c711452005-01-14 21:53:12 +00003513 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003514do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003515 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003516 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003517 int forceit;
3518{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003519 int ret = OK;
3520 int cc;
3521
3522 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003523 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003524 cc = *name_end;
3525 *name_end = NUL;
3526
3527 /* Normal name or expanded name. */
3528 if (check_changedtick(lp->ll_name))
3529 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003530 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003531 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003532 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003533 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003534 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3535 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003536 else if (lp->ll_range)
3537 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003538 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003539
3540 /* Delete a range of List items. */
3541 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3542 {
3543 li = lp->ll_li->li_next;
3544 listitem_remove(lp->ll_list, lp->ll_li);
3545 lp->ll_li = li;
3546 ++lp->ll_n1;
3547 }
3548 }
3549 else
3550 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003551 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003552 /* unlet a List item. */
3553 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003554 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003555 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003556 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003557 }
3558
3559 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003560}
3561
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562/*
3563 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003564 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 */
3566 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003567do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003569 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
Bram Moolenaar33570922005-01-25 22:26:29 +00003571 hashtab_T *ht;
3572 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003573 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003574 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
Bram Moolenaar33570922005-01-25 22:26:29 +00003576 ht = find_var_ht(name, &varname);
3577 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003579 hi = hash_find(ht, varname);
3580 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003581 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003582 di = HI2DI(hi);
3583 if (var_check_fixed(di->di_flags, name)
3584 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003585 return FAIL;
3586 delete_var(ht, hi);
3587 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 if (forceit)
3591 return OK;
3592 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 return FAIL;
3594}
3595
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003596/*
3597 * Lock or unlock variable indicated by "lp".
3598 * "deep" is the levels to go (-1 for unlimited);
3599 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3600 */
3601 static int
3602do_lock_var(lp, name_end, deep, lock)
3603 lval_T *lp;
3604 char_u *name_end;
3605 int deep;
3606 int lock;
3607{
3608 int ret = OK;
3609 int cc;
3610 dictitem_T *di;
3611
3612 if (deep == 0) /* nothing to do */
3613 return OK;
3614
3615 if (lp->ll_tv == NULL)
3616 {
3617 cc = *name_end;
3618 *name_end = NUL;
3619
3620 /* Normal name or expanded name. */
3621 if (check_changedtick(lp->ll_name))
3622 ret = FAIL;
3623 else
3624 {
3625 di = find_var(lp->ll_name, NULL);
3626 if (di == NULL)
3627 ret = FAIL;
3628 else
3629 {
3630 if (lock)
3631 di->di_flags |= DI_FLAGS_LOCK;
3632 else
3633 di->di_flags &= ~DI_FLAGS_LOCK;
3634 item_lock(&di->di_tv, deep, lock);
3635 }
3636 }
3637 *name_end = cc;
3638 }
3639 else if (lp->ll_range)
3640 {
3641 listitem_T *li = lp->ll_li;
3642
3643 /* (un)lock a range of List items. */
3644 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3645 {
3646 item_lock(&li->li_tv, deep, lock);
3647 li = li->li_next;
3648 ++lp->ll_n1;
3649 }
3650 }
3651 else if (lp->ll_list != NULL)
3652 /* (un)lock a List item. */
3653 item_lock(&lp->ll_li->li_tv, deep, lock);
3654 else
3655 /* un(lock) a Dictionary item. */
3656 item_lock(&lp->ll_di->di_tv, deep, lock);
3657
3658 return ret;
3659}
3660
3661/*
3662 * Lock or unlock an item. "deep" is nr of levels to go.
3663 */
3664 static void
3665item_lock(tv, deep, lock)
3666 typval_T *tv;
3667 int deep;
3668 int lock;
3669{
3670 static int recurse = 0;
3671 list_T *l;
3672 listitem_T *li;
3673 dict_T *d;
3674 hashitem_T *hi;
3675 int todo;
3676
3677 if (recurse >= DICT_MAXNEST)
3678 {
3679 EMSG(_("E743: variable nested too deep for (un)lock"));
3680 return;
3681 }
3682 if (deep == 0)
3683 return;
3684 ++recurse;
3685
3686 /* lock/unlock the item itself */
3687 if (lock)
3688 tv->v_lock |= VAR_LOCKED;
3689 else
3690 tv->v_lock &= ~VAR_LOCKED;
3691
3692 switch (tv->v_type)
3693 {
3694 case VAR_LIST:
3695 if ((l = tv->vval.v_list) != NULL)
3696 {
3697 if (lock)
3698 l->lv_lock |= VAR_LOCKED;
3699 else
3700 l->lv_lock &= ~VAR_LOCKED;
3701 if (deep < 0 || deep > 1)
3702 /* recursive: lock/unlock the items the List contains */
3703 for (li = l->lv_first; li != NULL; li = li->li_next)
3704 item_lock(&li->li_tv, deep - 1, lock);
3705 }
3706 break;
3707 case VAR_DICT:
3708 if ((d = tv->vval.v_dict) != NULL)
3709 {
3710 if (lock)
3711 d->dv_lock |= VAR_LOCKED;
3712 else
3713 d->dv_lock &= ~VAR_LOCKED;
3714 if (deep < 0 || deep > 1)
3715 {
3716 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003717 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003718 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3719 {
3720 if (!HASHITEM_EMPTY(hi))
3721 {
3722 --todo;
3723 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3724 }
3725 }
3726 }
3727 }
3728 }
3729 --recurse;
3730}
3731
Bram Moolenaara40058a2005-07-11 22:42:07 +00003732/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003733 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3734 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003735 */
3736 static int
3737tv_islocked(tv)
3738 typval_T *tv;
3739{
3740 return (tv->v_lock & VAR_LOCKED)
3741 || (tv->v_type == VAR_LIST
3742 && tv->vval.v_list != NULL
3743 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3744 || (tv->v_type == VAR_DICT
3745 && tv->vval.v_dict != NULL
3746 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3747}
3748
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3750/*
3751 * Delete all "menutrans_" variables.
3752 */
3753 void
3754del_menutrans_vars()
3755{
Bram Moolenaar33570922005-01-25 22:26:29 +00003756 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003757 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758
Bram Moolenaar33570922005-01-25 22:26:29 +00003759 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003760 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003761 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003762 {
3763 if (!HASHITEM_EMPTY(hi))
3764 {
3765 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003766 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3767 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003768 }
3769 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003770 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771}
3772#endif
3773
3774#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3775
3776/*
3777 * Local string buffer for the next two functions to store a variable name
3778 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3779 * get_user_var_name().
3780 */
3781
3782static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3783
3784static char_u *varnamebuf = NULL;
3785static int varnamebuflen = 0;
3786
3787/*
3788 * Function to concatenate a prefix and a variable name.
3789 */
3790 static char_u *
3791cat_prefix_varname(prefix, name)
3792 int prefix;
3793 char_u *name;
3794{
3795 int len;
3796
3797 len = (int)STRLEN(name) + 3;
3798 if (len > varnamebuflen)
3799 {
3800 vim_free(varnamebuf);
3801 len += 10; /* some additional space */
3802 varnamebuf = alloc(len);
3803 if (varnamebuf == NULL)
3804 {
3805 varnamebuflen = 0;
3806 return NULL;
3807 }
3808 varnamebuflen = len;
3809 }
3810 *varnamebuf = prefix;
3811 varnamebuf[1] = ':';
3812 STRCPY(varnamebuf + 2, name);
3813 return varnamebuf;
3814}
3815
3816/*
3817 * Function given to ExpandGeneric() to obtain the list of user defined
3818 * (global/buffer/window/built-in) variable names.
3819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 char_u *
3821get_user_var_name(xp, idx)
3822 expand_T *xp;
3823 int idx;
3824{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003825 static long_u gdone;
3826 static long_u bdone;
3827 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003828#ifdef FEAT_WINDOWS
3829 static long_u tdone;
3830#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003831 static int vidx;
3832 static hashitem_T *hi;
3833 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
3835 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003836 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003837 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003838#ifdef FEAT_WINDOWS
3839 tdone = 0;
3840#endif
3841 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003842
3843 /* Global variables */
3844 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003846 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003847 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003848 else
3849 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003850 while (HASHITEM_EMPTY(hi))
3851 ++hi;
3852 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3853 return cat_prefix_varname('g', hi->hi_key);
3854 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003856
3857 /* b: variables */
3858 ht = &curbuf->b_vars.dv_hashtab;
3859 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003861 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003862 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003863 else
3864 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003865 while (HASHITEM_EMPTY(hi))
3866 ++hi;
3867 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003869 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003871 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 return (char_u *)"b:changedtick";
3873 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003874
3875 /* w: variables */
3876 ht = &curwin->w_vars.dv_hashtab;
3877 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003879 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003880 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003881 else
3882 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003883 while (HASHITEM_EMPTY(hi))
3884 ++hi;
3885 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003887
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003888#ifdef FEAT_WINDOWS
3889 /* t: variables */
3890 ht = &curtab->tp_vars.dv_hashtab;
3891 if (tdone < ht->ht_used)
3892 {
3893 if (tdone++ == 0)
3894 hi = ht->ht_array;
3895 else
3896 ++hi;
3897 while (HASHITEM_EMPTY(hi))
3898 ++hi;
3899 return cat_prefix_varname('t', hi->hi_key);
3900 }
3901#endif
3902
Bram Moolenaar33570922005-01-25 22:26:29 +00003903 /* v: variables */
3904 if (vidx < VV_LEN)
3905 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
3907 vim_free(varnamebuf);
3908 varnamebuf = NULL;
3909 varnamebuflen = 0;
3910 return NULL;
3911}
3912
3913#endif /* FEAT_CMDL_COMPL */
3914
3915/*
3916 * types for expressions.
3917 */
3918typedef enum
3919{
3920 TYPE_UNKNOWN = 0
3921 , TYPE_EQUAL /* == */
3922 , TYPE_NEQUAL /* != */
3923 , TYPE_GREATER /* > */
3924 , TYPE_GEQUAL /* >= */
3925 , TYPE_SMALLER /* < */
3926 , TYPE_SEQUAL /* <= */
3927 , TYPE_MATCH /* =~ */
3928 , TYPE_NOMATCH /* !~ */
3929} exptype_T;
3930
3931/*
3932 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003933 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3935 */
3936
3937/*
3938 * Handle zero level expression.
3939 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003940 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003941 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 * Return OK or FAIL.
3943 */
3944 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003945eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 char_u **nextcmd;
3949 int evaluate;
3950{
3951 int ret;
3952 char_u *p;
3953
3954 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003955 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 if (ret == FAIL || !ends_excmd(*p))
3957 {
3958 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003959 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 /*
3961 * Report the invalid expression unless the expression evaluation has
3962 * been cancelled due to an aborting error, an interrupt, or an
3963 * exception.
3964 */
3965 if (!aborting())
3966 EMSG2(_(e_invexpr2), arg);
3967 ret = FAIL;
3968 }
3969 if (nextcmd != NULL)
3970 *nextcmd = check_nextcmd(p);
3971
3972 return ret;
3973}
3974
3975/*
3976 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003977 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 *
3979 * "arg" must point to the first non-white of the expression.
3980 * "arg" is advanced to the next non-white after the recognized expression.
3981 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003982 * Note: "rettv.v_lock" is not set.
3983 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 * Return OK or FAIL.
3985 */
3986 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003987eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 int evaluate;
3991{
3992 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003993 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
3995 /*
3996 * Get the first variable.
3997 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003998 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 return FAIL;
4000
4001 if ((*arg)[0] == '?')
4002 {
4003 result = FALSE;
4004 if (evaluate)
4005 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004006 int error = FALSE;
4007
4008 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004010 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004011 if (error)
4012 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
4014
4015 /*
4016 * Get the second variable.
4017 */
4018 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 return FAIL;
4021
4022 /*
4023 * Check for the ":".
4024 */
4025 if ((*arg)[0] != ':')
4026 {
4027 EMSG(_("E109: Missing ':' after '?'"));
4028 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004029 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 return FAIL;
4031 }
4032
4033 /*
4034 * Get the third variable.
4035 */
4036 *arg = skipwhite(*arg + 1);
4037 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4038 {
4039 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004040 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 return FAIL;
4042 }
4043 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004044 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
4046
4047 return OK;
4048}
4049
4050/*
4051 * Handle first level expression:
4052 * expr2 || expr2 || expr2 logical OR
4053 *
4054 * "arg" must point to the first non-white of the expression.
4055 * "arg" is advanced to the next non-white after the recognized expression.
4056 *
4057 * Return OK or FAIL.
4058 */
4059 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004060eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 int evaluate;
4064{
Bram Moolenaar33570922005-01-25 22:26:29 +00004065 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 long result;
4067 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004068 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
4070 /*
4071 * Get the first variable.
4072 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004073 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 return FAIL;
4075
4076 /*
4077 * Repeat until there is no following "||".
4078 */
4079 first = TRUE;
4080 result = FALSE;
4081 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4082 {
4083 if (evaluate && first)
4084 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004085 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004087 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004088 if (error)
4089 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 first = FALSE;
4091 }
4092
4093 /*
4094 * Get the second variable.
4095 */
4096 *arg = skipwhite(*arg + 2);
4097 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4098 return FAIL;
4099
4100 /*
4101 * Compute the result.
4102 */
4103 if (evaluate && !result)
4104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004105 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004107 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004108 if (error)
4109 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111 if (evaluate)
4112 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 rettv->v_type = VAR_NUMBER;
4114 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
4116 }
4117
4118 return OK;
4119}
4120
4121/*
4122 * Handle second level expression:
4123 * expr3 && expr3 && expr3 logical AND
4124 *
4125 * "arg" must point to the first non-white of the expression.
4126 * "arg" is advanced to the next non-white after the recognized expression.
4127 *
4128 * Return OK or FAIL.
4129 */
4130 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004131eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 int evaluate;
4135{
Bram Moolenaar33570922005-01-25 22:26:29 +00004136 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 long result;
4138 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004139 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140
4141 /*
4142 * Get the first variable.
4143 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 return FAIL;
4146
4147 /*
4148 * Repeat until there is no following "&&".
4149 */
4150 first = TRUE;
4151 result = TRUE;
4152 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4153 {
4154 if (evaluate && first)
4155 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004156 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004158 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004159 if (error)
4160 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 first = FALSE;
4162 }
4163
4164 /*
4165 * Get the second variable.
4166 */
4167 *arg = skipwhite(*arg + 2);
4168 if (eval4(arg, &var2, evaluate && result) == FAIL)
4169 return FAIL;
4170
4171 /*
4172 * Compute the result.
4173 */
4174 if (evaluate && result)
4175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004176 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 if (error)
4180 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
4182 if (evaluate)
4183 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004184 rettv->v_type = VAR_NUMBER;
4185 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187 }
4188
4189 return OK;
4190}
4191
4192/*
4193 * Handle third level expression:
4194 * var1 == var2
4195 * var1 =~ var2
4196 * var1 != var2
4197 * var1 !~ var2
4198 * var1 > var2
4199 * var1 >= var2
4200 * var1 < var2
4201 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004202 * var1 is var2
4203 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 *
4205 * "arg" must point to the first non-white of the expression.
4206 * "arg" is advanced to the next non-white after the recognized expression.
4207 *
4208 * Return OK or FAIL.
4209 */
4210 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 int evaluate;
4215{
Bram Moolenaar33570922005-01-25 22:26:29 +00004216 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 char_u *p;
4218 int i;
4219 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004220 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 int len = 2;
4222 long n1, n2;
4223 char_u *s1, *s2;
4224 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4225 regmatch_T regmatch;
4226 int ic;
4227 char_u *save_cpo;
4228
4229 /*
4230 * Get the first variable.
4231 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 return FAIL;
4234
4235 p = *arg;
4236 switch (p[0])
4237 {
4238 case '=': if (p[1] == '=')
4239 type = TYPE_EQUAL;
4240 else if (p[1] == '~')
4241 type = TYPE_MATCH;
4242 break;
4243 case '!': if (p[1] == '=')
4244 type = TYPE_NEQUAL;
4245 else if (p[1] == '~')
4246 type = TYPE_NOMATCH;
4247 break;
4248 case '>': if (p[1] != '=')
4249 {
4250 type = TYPE_GREATER;
4251 len = 1;
4252 }
4253 else
4254 type = TYPE_GEQUAL;
4255 break;
4256 case '<': if (p[1] != '=')
4257 {
4258 type = TYPE_SMALLER;
4259 len = 1;
4260 }
4261 else
4262 type = TYPE_SEQUAL;
4263 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004264 case 'i': if (p[1] == 's')
4265 {
4266 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4267 len = 5;
4268 if (!vim_isIDc(p[len]))
4269 {
4270 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4271 type_is = TRUE;
4272 }
4273 }
4274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276
4277 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004278 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 */
4280 if (type != TYPE_UNKNOWN)
4281 {
4282 /* extra question mark appended: ignore case */
4283 if (p[len] == '?')
4284 {
4285 ic = TRUE;
4286 ++len;
4287 }
4288 /* extra '#' appended: match case */
4289 else if (p[len] == '#')
4290 {
4291 ic = FALSE;
4292 ++len;
4293 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004294 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 else
4296 ic = p_ic;
4297
4298 /*
4299 * Get the second variable.
4300 */
4301 *arg = skipwhite(p + len);
4302 if (eval5(arg, &var2, evaluate) == FAIL)
4303 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004304 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 return FAIL;
4306 }
4307
4308 if (evaluate)
4309 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004310 if (type_is && rettv->v_type != var2.v_type)
4311 {
4312 /* For "is" a different type always means FALSE, for "notis"
4313 * it means TRUE. */
4314 n1 = (type == TYPE_NEQUAL);
4315 }
4316 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4317 {
4318 if (type_is)
4319 {
4320 n1 = (rettv->v_type == var2.v_type
4321 && rettv->vval.v_list == var2.vval.v_list);
4322 if (type == TYPE_NEQUAL)
4323 n1 = !n1;
4324 }
4325 else if (rettv->v_type != var2.v_type
4326 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4327 {
4328 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004329 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004330 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004331 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004332 clear_tv(rettv);
4333 clear_tv(&var2);
4334 return FAIL;
4335 }
4336 else
4337 {
4338 /* Compare two Lists for being equal or unequal. */
4339 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4340 if (type == TYPE_NEQUAL)
4341 n1 = !n1;
4342 }
4343 }
4344
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004345 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4346 {
4347 if (type_is)
4348 {
4349 n1 = (rettv->v_type == var2.v_type
4350 && rettv->vval.v_dict == var2.vval.v_dict);
4351 if (type == TYPE_NEQUAL)
4352 n1 = !n1;
4353 }
4354 else if (rettv->v_type != var2.v_type
4355 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4356 {
4357 if (rettv->v_type != var2.v_type)
4358 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4359 else
4360 EMSG(_("E736: Invalid operation for Dictionary"));
4361 clear_tv(rettv);
4362 clear_tv(&var2);
4363 return FAIL;
4364 }
4365 else
4366 {
4367 /* Compare two Dictionaries for being equal or unequal. */
4368 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4369 if (type == TYPE_NEQUAL)
4370 n1 = !n1;
4371 }
4372 }
4373
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004374 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4375 {
4376 if (rettv->v_type != var2.v_type
4377 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4378 {
4379 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004380 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004381 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004382 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004383 clear_tv(rettv);
4384 clear_tv(&var2);
4385 return FAIL;
4386 }
4387 else
4388 {
4389 /* Compare two Funcrefs for being equal or unequal. */
4390 if (rettv->vval.v_string == NULL
4391 || var2.vval.v_string == NULL)
4392 n1 = FALSE;
4393 else
4394 n1 = STRCMP(rettv->vval.v_string,
4395 var2.vval.v_string) == 0;
4396 if (type == TYPE_NEQUAL)
4397 n1 = !n1;
4398 }
4399 }
4400
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004401#ifdef FEAT_FLOAT
4402 /*
4403 * If one of the two variables is a float, compare as a float.
4404 * When using "=~" or "!~", always compare as string.
4405 */
4406 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4407 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4408 {
4409 float_T f1, f2;
4410
4411 if (rettv->v_type == VAR_FLOAT)
4412 f1 = rettv->vval.v_float;
4413 else
4414 f1 = get_tv_number(rettv);
4415 if (var2.v_type == VAR_FLOAT)
4416 f2 = var2.vval.v_float;
4417 else
4418 f2 = get_tv_number(&var2);
4419 n1 = FALSE;
4420 switch (type)
4421 {
4422 case TYPE_EQUAL: n1 = (f1 == f2); break;
4423 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4424 case TYPE_GREATER: n1 = (f1 > f2); break;
4425 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4426 case TYPE_SMALLER: n1 = (f1 < f2); break;
4427 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4428 case TYPE_UNKNOWN:
4429 case TYPE_MATCH:
4430 case TYPE_NOMATCH: break; /* avoid gcc warning */
4431 }
4432 }
4433#endif
4434
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 /*
4436 * If one of the two variables is a number, compare as a number.
4437 * When using "=~" or "!~", always compare as string.
4438 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004439 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004442 n1 = get_tv_number(rettv);
4443 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 switch (type)
4445 {
4446 case TYPE_EQUAL: n1 = (n1 == n2); break;
4447 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4448 case TYPE_GREATER: n1 = (n1 > n2); break;
4449 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4450 case TYPE_SMALLER: n1 = (n1 < n2); break;
4451 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4452 case TYPE_UNKNOWN:
4453 case TYPE_MATCH:
4454 case TYPE_NOMATCH: break; /* avoid gcc warning */
4455 }
4456 }
4457 else
4458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004459 s1 = get_tv_string_buf(rettv, buf1);
4460 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4462 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4463 else
4464 i = 0;
4465 n1 = FALSE;
4466 switch (type)
4467 {
4468 case TYPE_EQUAL: n1 = (i == 0); break;
4469 case TYPE_NEQUAL: n1 = (i != 0); break;
4470 case TYPE_GREATER: n1 = (i > 0); break;
4471 case TYPE_GEQUAL: n1 = (i >= 0); break;
4472 case TYPE_SMALLER: n1 = (i < 0); break;
4473 case TYPE_SEQUAL: n1 = (i <= 0); break;
4474
4475 case TYPE_MATCH:
4476 case TYPE_NOMATCH:
4477 /* avoid 'l' flag in 'cpoptions' */
4478 save_cpo = p_cpo;
4479 p_cpo = (char_u *)"";
4480 regmatch.regprog = vim_regcomp(s2,
4481 RE_MAGIC + RE_STRING);
4482 regmatch.rm_ic = ic;
4483 if (regmatch.regprog != NULL)
4484 {
4485 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4486 vim_free(regmatch.regprog);
4487 if (type == TYPE_NOMATCH)
4488 n1 = !n1;
4489 }
4490 p_cpo = save_cpo;
4491 break;
4492
4493 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4494 }
4495 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004496 clear_tv(rettv);
4497 clear_tv(&var2);
4498 rettv->v_type = VAR_NUMBER;
4499 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501 }
4502
4503 return OK;
4504}
4505
4506/*
4507 * Handle fourth level expression:
4508 * + number addition
4509 * - number subtraction
4510 * . string concatenation
4511 *
4512 * "arg" must point to the first non-white of the expression.
4513 * "arg" is advanced to the next non-white after the recognized expression.
4514 *
4515 * Return OK or FAIL.
4516 */
4517 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004518eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 int evaluate;
4522{
Bram Moolenaar33570922005-01-25 22:26:29 +00004523 typval_T var2;
4524 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 int op;
4526 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004527#ifdef FEAT_FLOAT
4528 float_T f1 = 0, f2 = 0;
4529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 char_u *s1, *s2;
4531 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4532 char_u *p;
4533
4534 /*
4535 * Get the first variable.
4536 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004537 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 return FAIL;
4539
4540 /*
4541 * Repeat computing, until no '+', '-' or '.' is following.
4542 */
4543 for (;;)
4544 {
4545 op = **arg;
4546 if (op != '+' && op != '-' && op != '.')
4547 break;
4548
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004549 if ((op != '+' || rettv->v_type != VAR_LIST)
4550#ifdef FEAT_FLOAT
4551 && (op == '.' || rettv->v_type != VAR_FLOAT)
4552#endif
4553 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004554 {
4555 /* For "list + ...", an illegal use of the first operand as
4556 * a number cannot be determined before evaluating the 2nd
4557 * operand: if this is also a list, all is ok.
4558 * For "something . ...", "something - ..." or "non-list + ...",
4559 * we know that the first operand needs to be a string or number
4560 * without evaluating the 2nd operand. So check before to avoid
4561 * side effects after an error. */
4562 if (evaluate && get_tv_string_chk(rettv) == NULL)
4563 {
4564 clear_tv(rettv);
4565 return FAIL;
4566 }
4567 }
4568
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 /*
4570 * Get the second variable.
4571 */
4572 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004573 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004575 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 return FAIL;
4577 }
4578
4579 if (evaluate)
4580 {
4581 /*
4582 * Compute the result.
4583 */
4584 if (op == '.')
4585 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004586 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4587 s2 = get_tv_string_buf_chk(&var2, buf2);
4588 if (s2 == NULL) /* type error ? */
4589 {
4590 clear_tv(rettv);
4591 clear_tv(&var2);
4592 return FAIL;
4593 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004594 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004595 clear_tv(rettv);
4596 rettv->v_type = VAR_STRING;
4597 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004599 else if (op == '+' && rettv->v_type == VAR_LIST
4600 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004601 {
4602 /* concatenate Lists */
4603 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4604 &var3) == FAIL)
4605 {
4606 clear_tv(rettv);
4607 clear_tv(&var2);
4608 return FAIL;
4609 }
4610 clear_tv(rettv);
4611 *rettv = var3;
4612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 else
4614 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004615 int error = FALSE;
4616
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004617#ifdef FEAT_FLOAT
4618 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004619 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004620 f1 = rettv->vval.v_float;
4621 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004622 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004623 else
4624#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004625 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004626 n1 = get_tv_number_chk(rettv, &error);
4627 if (error)
4628 {
4629 /* This can only happen for "list + non-list". For
4630 * "non-list + ..." or "something - ...", we returned
4631 * before evaluating the 2nd operand. */
4632 clear_tv(rettv);
4633 return FAIL;
4634 }
4635#ifdef FEAT_FLOAT
4636 if (var2.v_type == VAR_FLOAT)
4637 f1 = n1;
4638#endif
4639 }
4640#ifdef FEAT_FLOAT
4641 if (var2.v_type == VAR_FLOAT)
4642 {
4643 f2 = var2.vval.v_float;
4644 n2 = 0;
4645 }
4646 else
4647#endif
4648 {
4649 n2 = get_tv_number_chk(&var2, &error);
4650 if (error)
4651 {
4652 clear_tv(rettv);
4653 clear_tv(&var2);
4654 return FAIL;
4655 }
4656#ifdef FEAT_FLOAT
4657 if (rettv->v_type == VAR_FLOAT)
4658 f2 = n2;
4659#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004660 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004661 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004662
4663#ifdef FEAT_FLOAT
4664 /* If there is a float on either side the result is a float. */
4665 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4666 {
4667 if (op == '+')
4668 f1 = f1 + f2;
4669 else
4670 f1 = f1 - f2;
4671 rettv->v_type = VAR_FLOAT;
4672 rettv->vval.v_float = f1;
4673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004675#endif
4676 {
4677 if (op == '+')
4678 n1 = n1 + n2;
4679 else
4680 n1 = n1 - n2;
4681 rettv->v_type = VAR_NUMBER;
4682 rettv->vval.v_number = n1;
4683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004685 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 }
4687 }
4688 return OK;
4689}
4690
4691/*
4692 * Handle fifth level expression:
4693 * * number multiplication
4694 * / number division
4695 * % number modulo
4696 *
4697 * "arg" must point to the first non-white of the expression.
4698 * "arg" is advanced to the next non-white after the recognized expression.
4699 *
4700 * Return OK or FAIL.
4701 */
4702 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004703eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004707 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708{
Bram Moolenaar33570922005-01-25 22:26:29 +00004709 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 int op;
4711 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712#ifdef FEAT_FLOAT
4713 int use_float = FALSE;
4714 float_T f1 = 0, f2;
4715#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004716 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717
4718 /*
4719 * Get the first variable.
4720 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004721 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 return FAIL;
4723
4724 /*
4725 * Repeat computing, until no '*', '/' or '%' is following.
4726 */
4727 for (;;)
4728 {
4729 op = **arg;
4730 if (op != '*' && op != '/' && op != '%')
4731 break;
4732
4733 if (evaluate)
4734 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004735#ifdef FEAT_FLOAT
4736 if (rettv->v_type == VAR_FLOAT)
4737 {
4738 f1 = rettv->vval.v_float;
4739 use_float = TRUE;
4740 n1 = 0;
4741 }
4742 else
4743#endif
4744 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004745 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004746 if (error)
4747 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 }
4749 else
4750 n1 = 0;
4751
4752 /*
4753 * Get the second variable.
4754 */
4755 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004756 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 return FAIL;
4758
4759 if (evaluate)
4760 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004761#ifdef FEAT_FLOAT
4762 if (var2.v_type == VAR_FLOAT)
4763 {
4764 if (!use_float)
4765 {
4766 f1 = n1;
4767 use_float = TRUE;
4768 }
4769 f2 = var2.vval.v_float;
4770 n2 = 0;
4771 }
4772 else
4773#endif
4774 {
4775 n2 = get_tv_number_chk(&var2, &error);
4776 clear_tv(&var2);
4777 if (error)
4778 return FAIL;
4779#ifdef FEAT_FLOAT
4780 if (use_float)
4781 f2 = n2;
4782#endif
4783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
4785 /*
4786 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004787 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789#ifdef FEAT_FLOAT
4790 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792 if (op == '*')
4793 f1 = f1 * f2;
4794 else if (op == '/')
4795 {
4796 /* We rely on the floating point library to handle divide
4797 * by zero to result in "inf" and not a crash. */
4798 f1 = f1 / f2;
4799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004801 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004802 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004803 return FAIL;
4804 }
4805 rettv->v_type = VAR_FLOAT;
4806 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 }
4808 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811 if (op == '*')
4812 n1 = n1 * n2;
4813 else if (op == '/')
4814 {
4815 if (n2 == 0) /* give an error message? */
4816 {
4817 if (n1 == 0)
4818 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4819 else if (n1 < 0)
4820 n1 = -0x7fffffffL;
4821 else
4822 n1 = 0x7fffffffL;
4823 }
4824 else
4825 n1 = n1 / n2;
4826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004828 {
4829 if (n2 == 0) /* give an error message? */
4830 n1 = 0;
4831 else
4832 n1 = n1 % n2;
4833 }
4834 rettv->v_type = VAR_NUMBER;
4835 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
4838 }
4839
4840 return OK;
4841}
4842
4843/*
4844 * Handle sixth level expression:
4845 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004846 * "string" string constant
4847 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 * &option-name option value
4849 * @r register contents
4850 * identifier variable value
4851 * function() function call
4852 * $VAR environment variable
4853 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004854 * [expr, expr] List
4855 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 *
4857 * Also handle:
4858 * ! in front logical NOT
4859 * - in front unary minus
4860 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004861 * trailing [] subscript in String or List
4862 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 *
4864 * "arg" must point to the first non-white of the expression.
4865 * "arg" is advanced to the next non-white after the recognized expression.
4866 *
4867 * Return OK or FAIL.
4868 */
4869 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004870eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004874 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 long n;
4877 int len;
4878 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 char_u *start_leader, *end_leader;
4880 int ret = OK;
4881 char_u *alias;
4882
4883 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004884 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004885 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888
4889 /*
4890 * Skip '!' and '-' characters. They are handled later.
4891 */
4892 start_leader = *arg;
4893 while (**arg == '!' || **arg == '-' || **arg == '+')
4894 *arg = skipwhite(*arg + 1);
4895 end_leader = *arg;
4896
4897 switch (**arg)
4898 {
4899 /*
4900 * Number constant.
4901 */
4902 case '0':
4903 case '1':
4904 case '2':
4905 case '3':
4906 case '4':
4907 case '5':
4908 case '6':
4909 case '7':
4910 case '8':
4911 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004912 {
4913#ifdef FEAT_FLOAT
4914 char_u *p = skipdigits(*arg + 1);
4915 int get_float = FALSE;
4916
4917 /* We accept a float when the format matches
4918 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004919 * strict to avoid backwards compatibility problems.
4920 * Don't look for a float after the "." operator, so that
4921 * ":let vers = 1.2.3" doesn't fail. */
4922 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004924 get_float = TRUE;
4925 p = skipdigits(p + 2);
4926 if (*p == 'e' || *p == 'E')
4927 {
4928 ++p;
4929 if (*p == '-' || *p == '+')
4930 ++p;
4931 if (!vim_isdigit(*p))
4932 get_float = FALSE;
4933 else
4934 p = skipdigits(p + 1);
4935 }
4936 if (ASCII_ISALPHA(*p) || *p == '.')
4937 get_float = FALSE;
4938 }
4939 if (get_float)
4940 {
4941 float_T f;
4942
4943 *arg += string2float(*arg, &f);
4944 if (evaluate)
4945 {
4946 rettv->v_type = VAR_FLOAT;
4947 rettv->vval.v_float = f;
4948 }
4949 }
4950 else
4951#endif
4952 {
4953 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4954 *arg += len;
4955 if (evaluate)
4956 {
4957 rettv->v_type = VAR_NUMBER;
4958 rettv->vval.v_number = n;
4959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 }
4961 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963
4964 /*
4965 * String constant: "string".
4966 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004967 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 break;
4969
4970 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004973 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004974 break;
4975
4976 /*
4977 * List: [expr, expr]
4978 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004979 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 break;
4981
4982 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 * Dictionary: {key: val, key: val}
4984 */
4985 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4986 break;
4987
4988 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004989 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004991 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 break;
4993
4994 /*
4995 * Environment variable: $VAR.
4996 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004997 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 break;
4999
5000 /*
5001 * Register contents: @r.
5002 */
5003 case '@': ++*arg;
5004 if (evaluate)
5005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005006 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005007 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
5009 if (**arg != NUL)
5010 ++*arg;
5011 break;
5012
5013 /*
5014 * nested expression: (expression).
5015 */
5016 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005017 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 if (**arg == ')')
5019 ++*arg;
5020 else if (ret == OK)
5021 {
5022 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005023 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 ret = FAIL;
5025 }
5026 break;
5027
Bram Moolenaar8c711452005-01-14 21:53:12 +00005028 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 break;
5030 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031
5032 if (ret == NOTDONE)
5033 {
5034 /*
5035 * Must be a variable or function name.
5036 * Can also be a curly-braces kind of name: {expr}.
5037 */
5038 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005039 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 if (alias != NULL)
5041 s = alias;
5042
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005043 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 ret = FAIL;
5045 else
5046 {
5047 if (**arg == '(') /* recursive! */
5048 {
5049 /* If "s" is the name of a variable of type VAR_FUNC
5050 * use its contents. */
5051 s = deref_func_name(s, &len);
5052
5053 /* Invoke the function. */
5054 ret = get_func_tv(s, len, rettv, arg,
5055 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005056 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 /* Stop the expression evaluation when immediately
5058 * aborting on error, or when an interrupt occurred or
5059 * an exception was thrown but not caught. */
5060 if (aborting())
5061 {
5062 if (ret == OK)
5063 clear_tv(rettv);
5064 ret = FAIL;
5065 }
5066 }
5067 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005068 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005069 else
5070 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 }
5072
5073 if (alias != NULL)
5074 vim_free(alias);
5075 }
5076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 *arg = skipwhite(*arg);
5078
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005079 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5080 * expr(expr). */
5081 if (ret == OK)
5082 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
5084 /*
5085 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5086 */
5087 if (ret == OK && evaluate && end_leader > start_leader)
5088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005089 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005090 int val = 0;
5091#ifdef FEAT_FLOAT
5092 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005093
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005094 if (rettv->v_type == VAR_FLOAT)
5095 f = rettv->vval.v_float;
5096 else
5097#endif
5098 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005099 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005101 clear_tv(rettv);
5102 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005104 else
5105 {
5106 while (end_leader > start_leader)
5107 {
5108 --end_leader;
5109 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005110 {
5111#ifdef FEAT_FLOAT
5112 if (rettv->v_type == VAR_FLOAT)
5113 f = !f;
5114 else
5115#endif
5116 val = !val;
5117 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005118 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005119 {
5120#ifdef FEAT_FLOAT
5121 if (rettv->v_type == VAR_FLOAT)
5122 f = -f;
5123 else
5124#endif
5125 val = -val;
5126 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005127 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005128#ifdef FEAT_FLOAT
5129 if (rettv->v_type == VAR_FLOAT)
5130 {
5131 clear_tv(rettv);
5132 rettv->vval.v_float = f;
5133 }
5134 else
5135#endif
5136 {
5137 clear_tv(rettv);
5138 rettv->v_type = VAR_NUMBER;
5139 rettv->vval.v_number = val;
5140 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 }
5143
5144 return ret;
5145}
5146
5147/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005148 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5149 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005150 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5151 */
5152 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005153eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005154 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005155 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005156 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005157 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158{
5159 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005160 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005161 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 long len = -1;
5163 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005166
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005167 if (rettv->v_type == VAR_FUNC
5168#ifdef FEAT_FLOAT
5169 || rettv->v_type == VAR_FLOAT
5170#endif
5171 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005172 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005173 if (verbose)
5174 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005175 return FAIL;
5176 }
5177
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 /*
5181 * dict.name
5182 */
5183 key = *arg + 1;
5184 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5185 ;
5186 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005189 }
5190 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005191 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 /*
5193 * something[idx]
5194 *
5195 * Get the (first) variable from inside the [].
5196 */
5197 *arg = skipwhite(*arg + 1);
5198 if (**arg == ':')
5199 empty1 = TRUE;
5200 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5201 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005202 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5203 {
5204 /* not a number or string */
5205 clear_tv(&var1);
5206 return FAIL;
5207 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208
5209 /*
5210 * Get the second variable from inside the [:].
5211 */
5212 if (**arg == ':')
5213 {
5214 range = TRUE;
5215 *arg = skipwhite(*arg + 1);
5216 if (**arg == ']')
5217 empty2 = TRUE;
5218 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005220 if (!empty1)
5221 clear_tv(&var1);
5222 return FAIL;
5223 }
5224 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5225 {
5226 /* not a number or string */
5227 if (!empty1)
5228 clear_tv(&var1);
5229 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 return FAIL;
5231 }
5232 }
5233
5234 /* Check for the ']'. */
5235 if (**arg != ']')
5236 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005237 if (verbose)
5238 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 clear_tv(&var1);
5240 if (range)
5241 clear_tv(&var2);
5242 return FAIL;
5243 }
5244 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005245 }
5246
5247 if (evaluate)
5248 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 n1 = 0;
5250 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005252 n1 = get_tv_number(&var1);
5253 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 }
5255 if (range)
5256 {
5257 if (empty2)
5258 n2 = -1;
5259 else
5260 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005261 n2 = get_tv_number(&var2);
5262 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 }
5264 }
5265
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005266 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267 {
5268 case VAR_NUMBER:
5269 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005270 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 len = (long)STRLEN(s);
5272 if (range)
5273 {
5274 /* The resulting variable is a substring. If the indexes
5275 * are out of range the result is empty. */
5276 if (n1 < 0)
5277 {
5278 n1 = len + n1;
5279 if (n1 < 0)
5280 n1 = 0;
5281 }
5282 if (n2 < 0)
5283 n2 = len + n2;
5284 else if (n2 >= len)
5285 n2 = len;
5286 if (n1 >= len || n2 < 0 || n1 > n2)
5287 s = NULL;
5288 else
5289 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5290 }
5291 else
5292 {
5293 /* The resulting variable is a string of a single
5294 * character. If the index is too big or negative the
5295 * result is empty. */
5296 if (n1 >= len || n1 < 0)
5297 s = NULL;
5298 else
5299 s = vim_strnsave(s + n1, 1);
5300 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005301 clear_tv(rettv);
5302 rettv->v_type = VAR_STRING;
5303 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 break;
5305
5306 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005307 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308 if (n1 < 0)
5309 n1 = len + n1;
5310 if (!empty1 && (n1 < 0 || n1 >= len))
5311 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005312 /* For a range we allow invalid values and return an empty
5313 * list. A list index out of range is an error. */
5314 if (!range)
5315 {
5316 if (verbose)
5317 EMSGN(_(e_listidx), n1);
5318 return FAIL;
5319 }
5320 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005321 }
5322 if (range)
5323 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005324 list_T *l;
5325 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005326
5327 if (n2 < 0)
5328 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005329 else if (n2 >= len)
5330 n2 = len - 1;
5331 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005332 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333 l = list_alloc();
5334 if (l == NULL)
5335 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005336 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337 n1 <= n2; ++n1)
5338 {
5339 if (list_append_tv(l, &item->li_tv) == FAIL)
5340 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005341 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 return FAIL;
5343 }
5344 item = item->li_next;
5345 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005346 clear_tv(rettv);
5347 rettv->v_type = VAR_LIST;
5348 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005349 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 }
5351 else
5352 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005353 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005354 clear_tv(rettv);
5355 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 }
5357 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005358
5359 case VAR_DICT:
5360 if (range)
5361 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005362 if (verbose)
5363 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005364 if (len == -1)
5365 clear_tv(&var1);
5366 return FAIL;
5367 }
5368 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005369 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005370
5371 if (len == -1)
5372 {
5373 key = get_tv_string(&var1);
5374 if (*key == NUL)
5375 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005376 if (verbose)
5377 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005378 clear_tv(&var1);
5379 return FAIL;
5380 }
5381 }
5382
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005383 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005384
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005385 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005386 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005387 if (len == -1)
5388 clear_tv(&var1);
5389 if (item == NULL)
5390 return FAIL;
5391
5392 copy_tv(&item->di_tv, &var1);
5393 clear_tv(rettv);
5394 *rettv = var1;
5395 }
5396 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 }
5398 }
5399
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 return OK;
5401}
5402
5403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 * Get an option value.
5405 * "arg" points to the '&' or '+' before the option name.
5406 * "arg" is advanced to character after the option name.
5407 * Return OK or FAIL.
5408 */
5409 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005410get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005412 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 int evaluate;
5414{
5415 char_u *option_end;
5416 long numval;
5417 char_u *stringval;
5418 int opt_type;
5419 int c;
5420 int working = (**arg == '+'); /* has("+option") */
5421 int ret = OK;
5422 int opt_flags;
5423
5424 /*
5425 * Isolate the option name and find its value.
5426 */
5427 option_end = find_option_end(arg, &opt_flags);
5428 if (option_end == NULL)
5429 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005430 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 EMSG2(_("E112: Option name missing: %s"), *arg);
5432 return FAIL;
5433 }
5434
5435 if (!evaluate)
5436 {
5437 *arg = option_end;
5438 return OK;
5439 }
5440
5441 c = *option_end;
5442 *option_end = NUL;
5443 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005444 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445
5446 if (opt_type == -3) /* invalid name */
5447 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005448 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 EMSG2(_("E113: Unknown option: %s"), *arg);
5450 ret = FAIL;
5451 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005452 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 {
5454 if (opt_type == -2) /* hidden string option */
5455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005456 rettv->v_type = VAR_STRING;
5457 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 }
5459 else if (opt_type == -1) /* hidden number option */
5460 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005461 rettv->v_type = VAR_NUMBER;
5462 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 }
5464 else if (opt_type == 1) /* number option */
5465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 rettv->v_type = VAR_NUMBER;
5467 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 }
5469 else /* string option */
5470 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005471 rettv->v_type = VAR_STRING;
5472 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 }
5474 }
5475 else if (working && (opt_type == -2 || opt_type == -1))
5476 ret = FAIL;
5477
5478 *option_end = c; /* put back for error messages */
5479 *arg = option_end;
5480
5481 return ret;
5482}
5483
5484/*
5485 * Allocate a variable for a string constant.
5486 * Return OK or FAIL.
5487 */
5488 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005489get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 int evaluate;
5493{
5494 char_u *p;
5495 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 int extra = 0;
5497
5498 /*
5499 * Find the end of the string, skipping backslashed characters.
5500 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005501 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
5503 if (*p == '\\' && p[1] != NUL)
5504 {
5505 ++p;
5506 /* A "\<x>" form occupies at least 4 characters, and produces up
5507 * to 6 characters: reserve space for 2 extra */
5508 if (*p == '<')
5509 extra += 2;
5510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 }
5512
5513 if (*p != '"')
5514 {
5515 EMSG2(_("E114: Missing quote: %s"), *arg);
5516 return FAIL;
5517 }
5518
5519 /* If only parsing, set *arg and return here */
5520 if (!evaluate)
5521 {
5522 *arg = p + 1;
5523 return OK;
5524 }
5525
5526 /*
5527 * Copy the string into allocated memory, handling backslashed
5528 * characters.
5529 */
5530 name = alloc((unsigned)(p - *arg + extra));
5531 if (name == NULL)
5532 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005533 rettv->v_type = VAR_STRING;
5534 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
Bram Moolenaar8c711452005-01-14 21:53:12 +00005536 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 {
5538 if (*p == '\\')
5539 {
5540 switch (*++p)
5541 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005542 case 'b': *name++ = BS; ++p; break;
5543 case 'e': *name++ = ESC; ++p; break;
5544 case 'f': *name++ = FF; ++p; break;
5545 case 'n': *name++ = NL; ++p; break;
5546 case 'r': *name++ = CAR; ++p; break;
5547 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548
5549 case 'X': /* hex: "\x1", "\x12" */
5550 case 'x':
5551 case 'u': /* Unicode: "\u0023" */
5552 case 'U':
5553 if (vim_isxdigit(p[1]))
5554 {
5555 int n, nr;
5556 int c = toupper(*p);
5557
5558 if (c == 'X')
5559 n = 2;
5560 else
5561 n = 4;
5562 nr = 0;
5563 while (--n >= 0 && vim_isxdigit(p[1]))
5564 {
5565 ++p;
5566 nr = (nr << 4) + hex2nr(*p);
5567 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005568 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569#ifdef FEAT_MBYTE
5570 /* For "\u" store the number according to
5571 * 'encoding'. */
5572 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 else
5575#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005576 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 break;
5579
5580 /* octal: "\1", "\12", "\123" */
5581 case '0':
5582 case '1':
5583 case '2':
5584 case '3':
5585 case '4':
5586 case '5':
5587 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005588 case '7': *name = *p++ - '0';
5589 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005591 *name = (*name << 3) + *p++ - '0';
5592 if (*p >= '0' && *p <= '7')
5593 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 break;
5597
5598 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 if (extra != 0)
5601 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005602 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 break;
5604 }
5605 /* FALLTHROUGH */
5606
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 break;
5609 }
5610 }
5611 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005612 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005615 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 *arg = p + 1;
5617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 return OK;
5619}
5620
5621/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005622 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 * Return OK or FAIL.
5624 */
5625 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005626get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 int evaluate;
5630{
5631 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005632 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005633 int reduce = 0;
5634
5635 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005637 */
5638 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5639 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005641 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005642 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005643 break;
5644 ++reduce;
5645 ++p;
5646 }
5647 }
5648
Bram Moolenaar8c711452005-01-14 21:53:12 +00005649 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005652 return FAIL;
5653 }
5654
Bram Moolenaar8c711452005-01-14 21:53:12 +00005655 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005656 if (!evaluate)
5657 {
5658 *arg = p + 1;
5659 return OK;
5660 }
5661
5662 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005663 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005664 */
5665 str = alloc((unsigned)((p - *arg) - reduce));
5666 if (str == NULL)
5667 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005668 rettv->v_type = VAR_STRING;
5669 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005670
Bram Moolenaar8c711452005-01-14 21:53:12 +00005671 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005676 break;
5677 ++p;
5678 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005682 *arg = p + 1;
5683
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005684 return OK;
5685}
5686
5687/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005688 * Allocate a variable for a List and fill it from "*arg".
5689 * Return OK or FAIL.
5690 */
5691 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005692get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005693 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005694 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005695 int evaluate;
5696{
Bram Moolenaar33570922005-01-25 22:26:29 +00005697 list_T *l = NULL;
5698 typval_T tv;
5699 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005700
5701 if (evaluate)
5702 {
5703 l = list_alloc();
5704 if (l == NULL)
5705 return FAIL;
5706 }
5707
5708 *arg = skipwhite(*arg + 1);
5709 while (**arg != ']' && **arg != NUL)
5710 {
5711 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5712 goto failret;
5713 if (evaluate)
5714 {
5715 item = listitem_alloc();
5716 if (item != NULL)
5717 {
5718 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005719 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005720 list_append(l, item);
5721 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005722 else
5723 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005724 }
5725
5726 if (**arg == ']')
5727 break;
5728 if (**arg != ',')
5729 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005731 goto failret;
5732 }
5733 *arg = skipwhite(*arg + 1);
5734 }
5735
5736 if (**arg != ']')
5737 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005739failret:
5740 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005741 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005742 return FAIL;
5743 }
5744
5745 *arg = skipwhite(*arg + 1);
5746 if (evaluate)
5747 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005748 rettv->v_type = VAR_LIST;
5749 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005750 ++l->lv_refcount;
5751 }
5752
5753 return OK;
5754}
5755
5756/*
5757 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005758 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005760 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761list_alloc()
5762{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005763 list_T *l;
5764
5765 l = (list_T *)alloc_clear(sizeof(list_T));
5766 if (l != NULL)
5767 {
5768 /* Prepend the list to the list of lists for garbage collection. */
5769 if (first_list != NULL)
5770 first_list->lv_used_prev = l;
5771 l->lv_used_prev = NULL;
5772 l->lv_used_next = first_list;
5773 first_list = l;
5774 }
5775 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005776}
5777
5778/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005779 * Allocate an empty list for a return value.
5780 * Returns OK or FAIL.
5781 */
5782 static int
5783rettv_list_alloc(rettv)
5784 typval_T *rettv;
5785{
5786 list_T *l = list_alloc();
5787
5788 if (l == NULL)
5789 return FAIL;
5790
5791 rettv->vval.v_list = l;
5792 rettv->v_type = VAR_LIST;
5793 ++l->lv_refcount;
5794 return OK;
5795}
5796
5797/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005798 * Unreference a list: decrement the reference count and free it when it
5799 * becomes zero.
5800 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005801 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005803 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005805 if (l != NULL && --l->lv_refcount <= 0)
5806 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807}
5808
5809/*
5810 * Free a list, including all items it points to.
5811 * Ignores the reference count.
5812 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005813 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005814list_free(l, recurse)
5815 list_T *l;
5816 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005817{
Bram Moolenaar33570922005-01-25 22:26:29 +00005818 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005819
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005820 /* Remove the list from the list of lists for garbage collection. */
5821 if (l->lv_used_prev == NULL)
5822 first_list = l->lv_used_next;
5823 else
5824 l->lv_used_prev->lv_used_next = l->lv_used_next;
5825 if (l->lv_used_next != NULL)
5826 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5827
Bram Moolenaard9fba312005-06-26 22:34:35 +00005828 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005829 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005830 /* Remove the item before deleting it. */
5831 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005832 if (recurse || (item->li_tv.v_type != VAR_LIST
5833 && item->li_tv.v_type != VAR_DICT))
5834 clear_tv(&item->li_tv);
5835 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836 }
5837 vim_free(l);
5838}
5839
5840/*
5841 * Allocate a list item.
5842 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005843 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005844listitem_alloc()
5845{
Bram Moolenaar33570922005-01-25 22:26:29 +00005846 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847}
5848
5849/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005850 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005851 */
5852 static void
5853listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005854 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005855{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005856 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857 vim_free(item);
5858}
5859
5860/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005861 * Remove a list item from a List and free it. Also clears the value.
5862 */
5863 static void
5864listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005865 list_T *l;
5866 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005867{
5868 list_remove(l, item, item);
5869 listitem_free(item);
5870}
5871
5872/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873 * Get the number of items in a list.
5874 */
5875 static long
5876list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005877 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879 if (l == NULL)
5880 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005881 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882}
5883
5884/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005885 * Return TRUE when two lists have exactly the same values.
5886 */
5887 static int
5888list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005889 list_T *l1;
5890 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005891 int ic; /* ignore case for strings */
5892{
Bram Moolenaar33570922005-01-25 22:26:29 +00005893 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005894
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005895 if (l1 == NULL || l2 == NULL)
5896 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005897 if (l1 == l2)
5898 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005899 if (list_len(l1) != list_len(l2))
5900 return FALSE;
5901
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005902 for (item1 = l1->lv_first, item2 = l2->lv_first;
5903 item1 != NULL && item2 != NULL;
5904 item1 = item1->li_next, item2 = item2->li_next)
5905 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5906 return FALSE;
5907 return item1 == NULL && item2 == NULL;
5908}
5909
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01005910#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_MZSCHEME) \
5911 || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005912/*
5913 * Return the dictitem that an entry in a hashtable points to.
5914 */
5915 dictitem_T *
5916dict_lookup(hi)
5917 hashitem_T *hi;
5918{
5919 return HI2DI(hi);
5920}
5921#endif
5922
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005923/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005924 * Return TRUE when two dictionaries have exactly the same key/values.
5925 */
5926 static int
5927dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005928 dict_T *d1;
5929 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005930 int ic; /* ignore case for strings */
5931{
Bram Moolenaar33570922005-01-25 22:26:29 +00005932 hashitem_T *hi;
5933 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005934 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005935
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005936 if (d1 == NULL || d2 == NULL)
5937 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005938 if (d1 == d2)
5939 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005940 if (dict_len(d1) != dict_len(d2))
5941 return FALSE;
5942
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005943 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005944 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005945 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005946 if (!HASHITEM_EMPTY(hi))
5947 {
5948 item2 = dict_find(d2, hi->hi_key, -1);
5949 if (item2 == NULL)
5950 return FALSE;
5951 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5952 return FALSE;
5953 --todo;
5954 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005955 }
5956 return TRUE;
5957}
5958
5959/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005960 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005961 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005962 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005963 */
5964 static int
5965tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005966 typval_T *tv1;
5967 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005968 int ic; /* ignore case */
5969{
5970 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005971 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005972 static int recursive = 0; /* cach recursive loops */
5973 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005974
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005975 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005976 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005977 /* Catch lists and dicts that have an endless loop by limiting
5978 * recursiveness to 1000. We guess they are equal then. */
5979 if (recursive >= 1000)
5980 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005981
5982 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005983 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005984 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005985 ++recursive;
5986 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5987 --recursive;
5988 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005989
5990 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005991 ++recursive;
5992 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5993 --recursive;
5994 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005995
5996 case VAR_FUNC:
5997 return (tv1->vval.v_string != NULL
5998 && tv2->vval.v_string != NULL
5999 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6000
6001 case VAR_NUMBER:
6002 return tv1->vval.v_number == tv2->vval.v_number;
6003
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006004#ifdef FEAT_FLOAT
6005 case VAR_FLOAT:
6006 return tv1->vval.v_float == tv2->vval.v_float;
6007#endif
6008
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006009 case VAR_STRING:
6010 s1 = get_tv_string_buf(tv1, buf1);
6011 s2 = get_tv_string_buf(tv2, buf2);
6012 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006013 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006014
6015 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006016 return TRUE;
6017}
6018
6019/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020 * Locate item with index "n" in list "l" and return it.
6021 * A negative index is counted from the end; -1 is the last item.
6022 * Returns NULL when "n" is out of range.
6023 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006024 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006026 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027 long n;
6028{
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 long idx;
6031
6032 if (l == NULL)
6033 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006034
6035 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006037 n = l->lv_len + n;
6038
6039 /* Check for index out of range. */
6040 if (n < 0 || n >= l->lv_len)
6041 return NULL;
6042
6043 /* When there is a cached index may start search from there. */
6044 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006046 if (n < l->lv_idx / 2)
6047 {
6048 /* closest to the start of the list */
6049 item = l->lv_first;
6050 idx = 0;
6051 }
6052 else if (n > (l->lv_idx + l->lv_len) / 2)
6053 {
6054 /* closest to the end of the list */
6055 item = l->lv_last;
6056 idx = l->lv_len - 1;
6057 }
6058 else
6059 {
6060 /* closest to the cached index */
6061 item = l->lv_idx_item;
6062 idx = l->lv_idx;
6063 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064 }
6065 else
6066 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006067 if (n < l->lv_len / 2)
6068 {
6069 /* closest to the start of the list */
6070 item = l->lv_first;
6071 idx = 0;
6072 }
6073 else
6074 {
6075 /* closest to the end of the list */
6076 item = l->lv_last;
6077 idx = l->lv_len - 1;
6078 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006080
6081 while (n > idx)
6082 {
6083 /* search forward */
6084 item = item->li_next;
6085 ++idx;
6086 }
6087 while (n < idx)
6088 {
6089 /* search backward */
6090 item = item->li_prev;
6091 --idx;
6092 }
6093
6094 /* cache the used index */
6095 l->lv_idx = idx;
6096 l->lv_idx_item = item;
6097
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098 return item;
6099}
6100
6101/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006102 * Get list item "l[idx]" as a number.
6103 */
6104 static long
6105list_find_nr(l, idx, errorp)
6106 list_T *l;
6107 long idx;
6108 int *errorp; /* set to TRUE when something wrong */
6109{
6110 listitem_T *li;
6111
6112 li = list_find(l, idx);
6113 if (li == NULL)
6114 {
6115 if (errorp != NULL)
6116 *errorp = TRUE;
6117 return -1L;
6118 }
6119 return get_tv_number_chk(&li->li_tv, errorp);
6120}
6121
6122/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006123 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6124 */
6125 char_u *
6126list_find_str(l, idx)
6127 list_T *l;
6128 long idx;
6129{
6130 listitem_T *li;
6131
6132 li = list_find(l, idx - 1);
6133 if (li == NULL)
6134 {
6135 EMSGN(_(e_listidx), idx);
6136 return NULL;
6137 }
6138 return get_tv_string(&li->li_tv);
6139}
6140
6141/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006142 * Locate "item" list "l" and return its index.
6143 * Returns -1 when "item" is not in the list.
6144 */
6145 static long
6146list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 list_T *l;
6148 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006149{
6150 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006151 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006152
6153 if (l == NULL)
6154 return -1;
6155 idx = 0;
6156 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6157 ++idx;
6158 if (li == NULL)
6159 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006160 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006161}
6162
6163/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164 * Append item "item" to the end of list "l".
6165 */
6166 static void
6167list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006168 list_T *l;
6169 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006170{
6171 if (l->lv_last == NULL)
6172 {
6173 /* empty list */
6174 l->lv_first = item;
6175 l->lv_last = item;
6176 item->li_prev = NULL;
6177 }
6178 else
6179 {
6180 l->lv_last->li_next = item;
6181 item->li_prev = l->lv_last;
6182 l->lv_last = item;
6183 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006184 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185 item->li_next = NULL;
6186}
6187
6188/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006189 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006190 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006192 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006194 list_T *l;
6195 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006196{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006197 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006198
Bram Moolenaar05159a02005-02-26 23:04:13 +00006199 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006201 copy_tv(tv, &li->li_tv);
6202 list_append(l, li);
6203 return OK;
6204}
6205
6206/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006207 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006208 * Return FAIL when out of memory.
6209 */
6210 int
6211list_append_dict(list, dict)
6212 list_T *list;
6213 dict_T *dict;
6214{
6215 listitem_T *li = listitem_alloc();
6216
6217 if (li == NULL)
6218 return FAIL;
6219 li->li_tv.v_type = VAR_DICT;
6220 li->li_tv.v_lock = 0;
6221 li->li_tv.vval.v_dict = dict;
6222 list_append(list, li);
6223 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224 return OK;
6225}
6226
6227/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006228 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006229 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006230 * Returns FAIL when out of memory.
6231 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006232 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006233list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006234 list_T *l;
6235 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006236 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006237{
6238 listitem_T *li = listitem_alloc();
6239
6240 if (li == NULL)
6241 return FAIL;
6242 list_append(l, li);
6243 li->li_tv.v_type = VAR_STRING;
6244 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006245 if (str == NULL)
6246 li->li_tv.vval.v_string = NULL;
6247 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006248 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006249 return FAIL;
6250 return OK;
6251}
6252
6253/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006254 * Append "n" to list "l".
6255 * Returns FAIL when out of memory.
6256 */
6257 static int
6258list_append_number(l, n)
6259 list_T *l;
6260 varnumber_T n;
6261{
6262 listitem_T *li;
6263
6264 li = listitem_alloc();
6265 if (li == NULL)
6266 return FAIL;
6267 li->li_tv.v_type = VAR_NUMBER;
6268 li->li_tv.v_lock = 0;
6269 li->li_tv.vval.v_number = n;
6270 list_append(l, li);
6271 return OK;
6272}
6273
6274/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006275 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006276 * If "item" is NULL append at the end.
6277 * Return FAIL when out of memory.
6278 */
6279 static int
6280list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006281 list_T *l;
6282 typval_T *tv;
6283 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006284{
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006286
6287 if (ni == NULL)
6288 return FAIL;
6289 copy_tv(tv, &ni->li_tv);
6290 if (item == NULL)
6291 /* Append new item at end of list. */
6292 list_append(l, ni);
6293 else
6294 {
6295 /* Insert new item before existing item. */
6296 ni->li_prev = item->li_prev;
6297 ni->li_next = item;
6298 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006299 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006300 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006301 ++l->lv_idx;
6302 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006303 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006304 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006305 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006306 l->lv_idx_item = NULL;
6307 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006308 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006309 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006310 }
6311 return OK;
6312}
6313
6314/*
6315 * Extend "l1" with "l2".
6316 * If "bef" is NULL append at the end, otherwise insert before this item.
6317 * Returns FAIL when out of memory.
6318 */
6319 static int
6320list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006321 list_T *l1;
6322 list_T *l2;
6323 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006324{
Bram Moolenaar33570922005-01-25 22:26:29 +00006325 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006326 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006327
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006328 /* We also quit the loop when we have inserted the original item count of
6329 * the list, avoid a hang when we extend a list with itself. */
6330 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006331 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6332 return FAIL;
6333 return OK;
6334}
6335
6336/*
6337 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6338 * Return FAIL when out of memory.
6339 */
6340 static int
6341list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 list_T *l1;
6343 list_T *l2;
6344 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006345{
Bram Moolenaar33570922005-01-25 22:26:29 +00006346 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006347
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006348 if (l1 == NULL || l2 == NULL)
6349 return FAIL;
6350
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006351 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006352 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006353 if (l == NULL)
6354 return FAIL;
6355 tv->v_type = VAR_LIST;
6356 tv->vval.v_list = l;
6357
6358 /* append all items from the second list */
6359 return list_extend(l, l2, NULL);
6360}
6361
6362/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006363 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006364 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006365 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006366 * Returns NULL when out of memory.
6367 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006368 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006369list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006370 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006371 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006372 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006373{
Bram Moolenaar33570922005-01-25 22:26:29 +00006374 list_T *copy;
6375 listitem_T *item;
6376 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377
6378 if (orig == NULL)
6379 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006380
6381 copy = list_alloc();
6382 if (copy != NULL)
6383 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006384 if (copyID != 0)
6385 {
6386 /* Do this before adding the items, because one of the items may
6387 * refer back to this list. */
6388 orig->lv_copyID = copyID;
6389 orig->lv_copylist = copy;
6390 }
6391 for (item = orig->lv_first; item != NULL && !got_int;
6392 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393 {
6394 ni = listitem_alloc();
6395 if (ni == NULL)
6396 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006397 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006398 {
6399 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6400 {
6401 vim_free(ni);
6402 break;
6403 }
6404 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006406 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006407 list_append(copy, ni);
6408 }
6409 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006410 if (item != NULL)
6411 {
6412 list_unref(copy);
6413 copy = NULL;
6414 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415 }
6416
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417 return copy;
6418}
6419
6420/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006421 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006422 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006425list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006426 list_T *l;
6427 listitem_T *item;
6428 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429{
Bram Moolenaar33570922005-01-25 22:26:29 +00006430 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432 /* notify watchers */
6433 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006434 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006435 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006436 list_fix_watch(l, ip);
6437 if (ip == item2)
6438 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006439 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006440
6441 if (item2->li_next == NULL)
6442 l->lv_last = item->li_prev;
6443 else
6444 item2->li_next->li_prev = item->li_prev;
6445 if (item->li_prev == NULL)
6446 l->lv_first = item2->li_next;
6447 else
6448 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006449 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006450}
6451
6452/*
6453 * Return an allocated string with the string representation of a list.
6454 * May return NULL.
6455 */
6456 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006457list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006459 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006460{
6461 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006462
6463 if (tv->vval.v_list == NULL)
6464 return NULL;
6465 ga_init2(&ga, (int)sizeof(char), 80);
6466 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006467 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006468 {
6469 vim_free(ga.ga_data);
6470 return NULL;
6471 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006472 ga_append(&ga, ']');
6473 ga_append(&ga, NUL);
6474 return (char_u *)ga.ga_data;
6475}
6476
6477/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006478 * Join list "l" into a string in "*gap", using separator "sep".
6479 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006480 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006481 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006482 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006483list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006484 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006485 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006486 char_u *sep;
6487 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006488 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006489{
6490 int first = TRUE;
6491 char_u *tofree;
6492 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006494 char_u *s;
6495
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006496 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006497 {
6498 if (first)
6499 first = FALSE;
6500 else
6501 ga_concat(gap, sep);
6502
6503 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006504 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006505 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006506 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006507 if (s != NULL)
6508 ga_concat(gap, s);
6509 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006510 if (s == NULL)
6511 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006512 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006513 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006514 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006515}
6516
6517/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006518 * Garbage collection for lists and dictionaries.
6519 *
6520 * We use reference counts to be able to free most items right away when they
6521 * are no longer used. But for composite items it's possible that it becomes
6522 * unused while the reference count is > 0: When there is a recursive
6523 * reference. Example:
6524 * :let l = [1, 2, 3]
6525 * :let d = {9: l}
6526 * :let l[1] = d
6527 *
6528 * Since this is quite unusual we handle this with garbage collection: every
6529 * once in a while find out which lists and dicts are not referenced from any
6530 * variable.
6531 *
6532 * Here is a good reference text about garbage collection (refers to Python
6533 * but it applies to all reference-counting mechanisms):
6534 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006535 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006536
6537/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006538 * Do garbage collection for lists and dicts.
6539 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006540 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006541 int
6542garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006543{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006544 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006545 buf_T *buf;
6546 win_T *wp;
6547 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006548 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006549 int did_free;
6550 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006551#ifdef FEAT_WINDOWS
6552 tabpage_T *tp;
6553#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006554
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006555 /* Only do this once. */
6556 want_garbage_collect = FALSE;
6557 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006558 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006559
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006560 /* We advance by two because we add one for items referenced through
6561 * previous_funccal. */
6562 current_copyID += COPYID_INC;
6563 copyID = current_copyID;
6564
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006565 /*
6566 * 1. Go through all accessible variables and mark all lists and dicts
6567 * with copyID.
6568 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006569
6570 /* Don't free variables in the previous_funccal list unless they are only
6571 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006572 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006573 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6574 {
6575 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6576 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6577 }
6578
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006579 /* script-local variables */
6580 for (i = 1; i <= ga_scripts.ga_len; ++i)
6581 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6582
6583 /* buffer-local variables */
6584 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6585 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6586
6587 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006588 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006589 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6590
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006591#ifdef FEAT_WINDOWS
6592 /* tabpage-local variables */
6593 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6594 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6595#endif
6596
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006597 /* global variables */
6598 set_ref_in_ht(&globvarht, copyID);
6599
6600 /* function-local variables */
6601 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6602 {
6603 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6604 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6605 }
6606
Bram Moolenaard812df62008-11-09 12:46:09 +00006607 /* v: vars */
6608 set_ref_in_ht(&vimvarht, copyID);
6609
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006610 /*
6611 * 2. Free lists and dictionaries that are not referenced.
6612 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006613 did_free = free_unref_items(copyID);
6614
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006615 /*
6616 * 3. Check if any funccal can be freed now.
6617 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006618 for (pfc = &previous_funccal; *pfc != NULL; )
6619 {
6620 if (can_free_funccal(*pfc, copyID))
6621 {
6622 fc = *pfc;
6623 *pfc = fc->caller;
6624 free_funccal(fc, TRUE);
6625 did_free = TRUE;
6626 did_free_funccal = TRUE;
6627 }
6628 else
6629 pfc = &(*pfc)->caller;
6630 }
6631 if (did_free_funccal)
6632 /* When a funccal was freed some more items might be garbage
6633 * collected, so run again. */
6634 (void)garbage_collect();
6635
6636 return did_free;
6637}
6638
6639/*
6640 * Free lists and dictionaries that are no longer referenced.
6641 */
6642 static int
6643free_unref_items(copyID)
6644 int copyID;
6645{
6646 dict_T *dd;
6647 list_T *ll;
6648 int did_free = FALSE;
6649
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006650 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006651 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006652 */
6653 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006654 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006655 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006656 /* Free the Dictionary and ordinary items it contains, but don't
6657 * recurse into Lists and Dictionaries, they will be in the list
6658 * of dicts or list of lists. */
6659 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006660 did_free = TRUE;
6661
6662 /* restart, next dict may also have been freed */
6663 dd = first_dict;
6664 }
6665 else
6666 dd = dd->dv_used_next;
6667
6668 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006669 * Go through the list of lists and free items without the copyID.
6670 * But don't free a list that has a watcher (used in a for loop), these
6671 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006672 */
6673 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006674 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6675 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006676 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006677 /* Free the List and ordinary items it contains, but don't recurse
6678 * into Lists and Dictionaries, they will be in the list of dicts
6679 * or list of lists. */
6680 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006681 did_free = TRUE;
6682
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006683 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006684 ll = first_list;
6685 }
6686 else
6687 ll = ll->lv_used_next;
6688
6689 return did_free;
6690}
6691
6692/*
6693 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6694 */
6695 static void
6696set_ref_in_ht(ht, copyID)
6697 hashtab_T *ht;
6698 int copyID;
6699{
6700 int todo;
6701 hashitem_T *hi;
6702
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006703 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006704 for (hi = ht->ht_array; todo > 0; ++hi)
6705 if (!HASHITEM_EMPTY(hi))
6706 {
6707 --todo;
6708 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6709 }
6710}
6711
6712/*
6713 * Mark all lists and dicts referenced through list "l" with "copyID".
6714 */
6715 static void
6716set_ref_in_list(l, copyID)
6717 list_T *l;
6718 int copyID;
6719{
6720 listitem_T *li;
6721
6722 for (li = l->lv_first; li != NULL; li = li->li_next)
6723 set_ref_in_item(&li->li_tv, copyID);
6724}
6725
6726/*
6727 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6728 */
6729 static void
6730set_ref_in_item(tv, copyID)
6731 typval_T *tv;
6732 int copyID;
6733{
6734 dict_T *dd;
6735 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006736
6737 switch (tv->v_type)
6738 {
6739 case VAR_DICT:
6740 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006741 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006742 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006743 /* Didn't see this dict yet. */
6744 dd->dv_copyID = copyID;
6745 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006746 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006747 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006748
6749 case VAR_LIST:
6750 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006751 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006752 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006753 /* Didn't see this list yet. */
6754 ll->lv_copyID = copyID;
6755 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006756 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006757 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006758 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006759 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006760}
6761
6762/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006763 * Allocate an empty header for a dictionary.
6764 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006765 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006766dict_alloc()
6767{
Bram Moolenaar33570922005-01-25 22:26:29 +00006768 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006769
Bram Moolenaar33570922005-01-25 22:26:29 +00006770 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006771 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006772 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006773 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006774 if (first_dict != NULL)
6775 first_dict->dv_used_prev = d;
6776 d->dv_used_next = first_dict;
6777 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006778 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006779
Bram Moolenaar33570922005-01-25 22:26:29 +00006780 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006781 d->dv_lock = 0;
6782 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006783 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006784 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006785 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006786}
6787
6788/*
6789 * Unreference a Dictionary: decrement the reference count and free it when it
6790 * becomes zero.
6791 */
6792 static void
6793dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006794 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006795{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006796 if (d != NULL && --d->dv_refcount <= 0)
6797 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006798}
6799
6800/*
6801 * Free a Dictionary, including all items it contains.
6802 * Ignores the reference count.
6803 */
6804 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006805dict_free(d, recurse)
6806 dict_T *d;
6807 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006808{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006809 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006810 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006811 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006812
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006813 /* Remove the dict from the list of dicts for garbage collection. */
6814 if (d->dv_used_prev == NULL)
6815 first_dict = d->dv_used_next;
6816 else
6817 d->dv_used_prev->dv_used_next = d->dv_used_next;
6818 if (d->dv_used_next != NULL)
6819 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6820
6821 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006822 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006823 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006824 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006825 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006826 if (!HASHITEM_EMPTY(hi))
6827 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006828 /* Remove the item before deleting it, just in case there is
6829 * something recursive causing trouble. */
6830 di = HI2DI(hi);
6831 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006832 if (recurse || (di->di_tv.v_type != VAR_LIST
6833 && di->di_tv.v_type != VAR_DICT))
6834 clear_tv(&di->di_tv);
6835 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006836 --todo;
6837 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006838 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006839 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006840 vim_free(d);
6841}
6842
6843/*
6844 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006845 * The "key" is copied to the new item.
6846 * Note that the value of the item "di_tv" still needs to be initialized!
6847 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006848 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006849 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006850dictitem_alloc(key)
6851 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006852{
Bram Moolenaar33570922005-01-25 22:26:29 +00006853 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006854
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006855 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006856 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006857 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006858 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006859 di->di_flags = 0;
6860 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006861 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006862}
6863
6864/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006865 * Make a copy of a Dictionary item.
6866 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006867 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006868dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006869 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006870{
Bram Moolenaar33570922005-01-25 22:26:29 +00006871 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006872
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006873 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6874 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006875 if (di != NULL)
6876 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006877 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006878 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006879 copy_tv(&org->di_tv, &di->di_tv);
6880 }
6881 return di;
6882}
6883
6884/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006885 * Remove item "item" from Dictionary "dict" and free it.
6886 */
6887 static void
6888dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006889 dict_T *dict;
6890 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006891{
Bram Moolenaar33570922005-01-25 22:26:29 +00006892 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006893
Bram Moolenaar33570922005-01-25 22:26:29 +00006894 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006895 if (HASHITEM_EMPTY(hi))
6896 EMSG2(_(e_intern2), "dictitem_remove()");
6897 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006898 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006899 dictitem_free(item);
6900}
6901
6902/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006903 * Free a dict item. Also clears the value.
6904 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006905 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006906dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006907 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006908{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006909 clear_tv(&item->di_tv);
6910 vim_free(item);
6911}
6912
6913/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006914 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6915 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006916 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006917 * Returns NULL when out of memory.
6918 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006919 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006920dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006921 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006922 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006923 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006924{
Bram Moolenaar33570922005-01-25 22:26:29 +00006925 dict_T *copy;
6926 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006927 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006928 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006929
6930 if (orig == NULL)
6931 return NULL;
6932
6933 copy = dict_alloc();
6934 if (copy != NULL)
6935 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006936 if (copyID != 0)
6937 {
6938 orig->dv_copyID = copyID;
6939 orig->dv_copydict = copy;
6940 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006941 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006942 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006943 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006944 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006945 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006946 --todo;
6947
6948 di = dictitem_alloc(hi->hi_key);
6949 if (di == NULL)
6950 break;
6951 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006952 {
6953 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6954 copyID) == FAIL)
6955 {
6956 vim_free(di);
6957 break;
6958 }
6959 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006960 else
6961 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6962 if (dict_add(copy, di) == FAIL)
6963 {
6964 dictitem_free(di);
6965 break;
6966 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006967 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006968 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006969
Bram Moolenaare9a41262005-01-15 22:18:47 +00006970 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006971 if (todo > 0)
6972 {
6973 dict_unref(copy);
6974 copy = NULL;
6975 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006976 }
6977
6978 return copy;
6979}
6980
6981/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006982 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006983 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006984 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006985 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006986dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006987 dict_T *d;
6988 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006989{
Bram Moolenaar33570922005-01-25 22:26:29 +00006990 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006991}
6992
Bram Moolenaar8c711452005-01-14 21:53:12 +00006993/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006994 * Add a number or string entry to dictionary "d".
6995 * When "str" is NULL use number "nr", otherwise use "str".
6996 * Returns FAIL when out of memory and when key already exists.
6997 */
6998 int
6999dict_add_nr_str(d, key, nr, str)
7000 dict_T *d;
7001 char *key;
7002 long nr;
7003 char_u *str;
7004{
7005 dictitem_T *item;
7006
7007 item = dictitem_alloc((char_u *)key);
7008 if (item == NULL)
7009 return FAIL;
7010 item->di_tv.v_lock = 0;
7011 if (str == NULL)
7012 {
7013 item->di_tv.v_type = VAR_NUMBER;
7014 item->di_tv.vval.v_number = nr;
7015 }
7016 else
7017 {
7018 item->di_tv.v_type = VAR_STRING;
7019 item->di_tv.vval.v_string = vim_strsave(str);
7020 }
7021 if (dict_add(d, item) == FAIL)
7022 {
7023 dictitem_free(item);
7024 return FAIL;
7025 }
7026 return OK;
7027}
7028
7029/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007030 * Get the number of items in a Dictionary.
7031 */
7032 static long
7033dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007034 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007035{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007036 if (d == NULL)
7037 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007038 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007039}
7040
7041/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007042 * Find item "key[len]" in Dictionary "d".
7043 * If "len" is negative use strlen(key).
7044 * Returns NULL when not found.
7045 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007046 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007047dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007048 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007049 char_u *key;
7050 int len;
7051{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007052#define AKEYLEN 200
7053 char_u buf[AKEYLEN];
7054 char_u *akey;
7055 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007056 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007057
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007058 if (len < 0)
7059 akey = key;
7060 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007061 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007062 tofree = akey = vim_strnsave(key, len);
7063 if (akey == NULL)
7064 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007065 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007066 else
7067 {
7068 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007069 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007070 akey = buf;
7071 }
7072
Bram Moolenaar33570922005-01-25 22:26:29 +00007073 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007074 vim_free(tofree);
7075 if (HASHITEM_EMPTY(hi))
7076 return NULL;
7077 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007078}
7079
7080/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007081 * Get a string item from a dictionary.
7082 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007083 * Returns NULL if the entry doesn't exist or out of memory.
7084 */
7085 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007086get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007087 dict_T *d;
7088 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007089 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007090{
7091 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007092 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007093
7094 di = dict_find(d, key, -1);
7095 if (di == NULL)
7096 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007097 s = get_tv_string(&di->di_tv);
7098 if (save && s != NULL)
7099 s = vim_strsave(s);
7100 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007101}
7102
7103/*
7104 * Get a number item from a dictionary.
7105 * Returns 0 if the entry doesn't exist or out of memory.
7106 */
7107 long
7108get_dict_number(d, key)
7109 dict_T *d;
7110 char_u *key;
7111{
7112 dictitem_T *di;
7113
7114 di = dict_find(d, key, -1);
7115 if (di == NULL)
7116 return 0;
7117 return get_tv_number(&di->di_tv);
7118}
7119
7120/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007121 * Return an allocated string with the string representation of a Dictionary.
7122 * May return NULL.
7123 */
7124 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007125dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007126 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007127 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007128{
7129 garray_T ga;
7130 int first = TRUE;
7131 char_u *tofree;
7132 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007134 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007135 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007136 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007137
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007138 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007139 return NULL;
7140 ga_init2(&ga, (int)sizeof(char), 80);
7141 ga_append(&ga, '{');
7142
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007143 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007144 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007145 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007146 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007147 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007148 --todo;
7149
7150 if (first)
7151 first = FALSE;
7152 else
7153 ga_concat(&ga, (char_u *)", ");
7154
7155 tofree = string_quote(hi->hi_key, FALSE);
7156 if (tofree != NULL)
7157 {
7158 ga_concat(&ga, tofree);
7159 vim_free(tofree);
7160 }
7161 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007162 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007163 if (s != NULL)
7164 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007165 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007166 if (s == NULL)
7167 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007168 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007169 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007170 if (todo > 0)
7171 {
7172 vim_free(ga.ga_data);
7173 return NULL;
7174 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175
7176 ga_append(&ga, '}');
7177 ga_append(&ga, NUL);
7178 return (char_u *)ga.ga_data;
7179}
7180
7181/*
7182 * Allocate a variable for a Dictionary and fill it from "*arg".
7183 * Return OK or FAIL. Returns NOTDONE for {expr}.
7184 */
7185 static int
7186get_dict_tv(arg, rettv, evaluate)
7187 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007189 int evaluate;
7190{
Bram Moolenaar33570922005-01-25 22:26:29 +00007191 dict_T *d = NULL;
7192 typval_T tvkey;
7193 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007194 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007195 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007196 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007197 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198
7199 /*
7200 * First check if it's not a curly-braces thing: {expr}.
7201 * Must do this without evaluating, otherwise a function may be called
7202 * twice. Unfortunately this means we need to call eval1() twice for the
7203 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007204 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007205 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007206 if (*start != '}')
7207 {
7208 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7209 return FAIL;
7210 if (*start == '}')
7211 return NOTDONE;
7212 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213
7214 if (evaluate)
7215 {
7216 d = dict_alloc();
7217 if (d == NULL)
7218 return FAIL;
7219 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007220 tvkey.v_type = VAR_UNKNOWN;
7221 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222
7223 *arg = skipwhite(*arg + 1);
7224 while (**arg != '}' && **arg != NUL)
7225 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007226 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227 goto failret;
7228 if (**arg != ':')
7229 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007230 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007231 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007232 goto failret;
7233 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007234 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007236 key = get_tv_string_buf_chk(&tvkey, buf);
7237 if (key == NULL || *key == NUL)
7238 {
7239 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7240 if (key != NULL)
7241 EMSG(_(e_emptykey));
7242 clear_tv(&tvkey);
7243 goto failret;
7244 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246
7247 *arg = skipwhite(*arg + 1);
7248 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7249 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007250 if (evaluate)
7251 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252 goto failret;
7253 }
7254 if (evaluate)
7255 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007256 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257 if (item != NULL)
7258 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007259 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007260 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261 clear_tv(&tv);
7262 goto failret;
7263 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007264 item = dictitem_alloc(key);
7265 clear_tv(&tvkey);
7266 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007269 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007270 if (dict_add(d, item) == FAIL)
7271 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272 }
7273 }
7274
7275 if (**arg == '}')
7276 break;
7277 if (**arg != ',')
7278 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007279 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280 goto failret;
7281 }
7282 *arg = skipwhite(*arg + 1);
7283 }
7284
7285 if (**arg != '}')
7286 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007287 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288failret:
7289 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007290 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291 return FAIL;
7292 }
7293
7294 *arg = skipwhite(*arg + 1);
7295 if (evaluate)
7296 {
7297 rettv->v_type = VAR_DICT;
7298 rettv->vval.v_dict = d;
7299 ++d->dv_refcount;
7300 }
7301
7302 return OK;
7303}
7304
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007306 * Return a string with the string representation of a variable.
7307 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007308 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007309 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007310 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007311 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007312 */
7313 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007314echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007316 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007317 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007318 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007319{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 static int recurse = 0;
7321 char_u *r = NULL;
7322
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007325 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326 *tofree = NULL;
7327 return NULL;
7328 }
7329 ++recurse;
7330
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007331 switch (tv->v_type)
7332 {
7333 case VAR_FUNC:
7334 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007335 r = tv->vval.v_string;
7336 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007337
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007338 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007339 if (tv->vval.v_list == NULL)
7340 {
7341 *tofree = NULL;
7342 r = NULL;
7343 }
7344 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7345 {
7346 *tofree = NULL;
7347 r = (char_u *)"[...]";
7348 }
7349 else
7350 {
7351 tv->vval.v_list->lv_copyID = copyID;
7352 *tofree = list2string(tv, copyID);
7353 r = *tofree;
7354 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007355 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007356
Bram Moolenaar8c711452005-01-14 21:53:12 +00007357 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007358 if (tv->vval.v_dict == NULL)
7359 {
7360 *tofree = NULL;
7361 r = NULL;
7362 }
7363 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7364 {
7365 *tofree = NULL;
7366 r = (char_u *)"{...}";
7367 }
7368 else
7369 {
7370 tv->vval.v_dict->dv_copyID = copyID;
7371 *tofree = dict2string(tv, copyID);
7372 r = *tofree;
7373 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007375
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007376 case VAR_STRING:
7377 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378 *tofree = NULL;
7379 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007380 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007381
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007382#ifdef FEAT_FLOAT
7383 case VAR_FLOAT:
7384 *tofree = NULL;
7385 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7386 r = numbuf;
7387 break;
7388#endif
7389
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007390 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007391 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007393 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394
7395 --recurse;
7396 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007397}
7398
7399/*
7400 * Return a string with the string representation of a variable.
7401 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7402 * "numbuf" is used for a number.
7403 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007404 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007405 */
7406 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007407tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007408 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007409 char_u **tofree;
7410 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007411 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007412{
7413 switch (tv->v_type)
7414 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007415 case VAR_FUNC:
7416 *tofree = string_quote(tv->vval.v_string, TRUE);
7417 return *tofree;
7418 case VAR_STRING:
7419 *tofree = string_quote(tv->vval.v_string, FALSE);
7420 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007421#ifdef FEAT_FLOAT
7422 case VAR_FLOAT:
7423 *tofree = NULL;
7424 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7425 return numbuf;
7426#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007427 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007428 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007429 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007430 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007431 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007432 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007433 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007434 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007435}
7436
7437/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007438 * Return string "str" in ' quotes, doubling ' characters.
7439 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007441 */
7442 static char_u *
7443string_quote(str, function)
7444 char_u *str;
7445 int function;
7446{
Bram Moolenaar33570922005-01-25 22:26:29 +00007447 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007448 char_u *p, *r, *s;
7449
Bram Moolenaar33570922005-01-25 22:26:29 +00007450 len = (function ? 13 : 3);
7451 if (str != NULL)
7452 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007453 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007454 for (p = str; *p != NUL; mb_ptr_adv(p))
7455 if (*p == '\'')
7456 ++len;
7457 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007458 s = r = alloc(len);
7459 if (r != NULL)
7460 {
7461 if (function)
7462 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007464 r += 10;
7465 }
7466 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007468 if (str != NULL)
7469 for (p = str; *p != NUL; )
7470 {
7471 if (*p == '\'')
7472 *r++ = '\'';
7473 MB_COPY_CHAR(p, r);
7474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007475 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007476 if (function)
7477 *r++ = ')';
7478 *r++ = NUL;
7479 }
7480 return s;
7481}
7482
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007483#ifdef FEAT_FLOAT
7484/*
7485 * Convert the string "text" to a floating point number.
7486 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7487 * this always uses a decimal point.
7488 * Returns the length of the text that was consumed.
7489 */
7490 static int
7491string2float(text, value)
7492 char_u *text;
7493 float_T *value; /* result stored here */
7494{
7495 char *s = (char *)text;
7496 float_T f;
7497
7498 f = strtod(s, &s);
7499 *value = f;
7500 return (int)((char_u *)s - text);
7501}
7502#endif
7503
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007504/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 * Get the value of an environment variable.
7506 * "arg" is pointing to the '$'. It is advanced to after the name.
7507 * If the environment variable was not set, silently assume it is empty.
7508 * Always return OK.
7509 */
7510 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007511get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 int evaluate;
7515{
7516 char_u *string = NULL;
7517 int len;
7518 int cc;
7519 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007520 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521
7522 ++*arg;
7523 name = *arg;
7524 len = get_env_len(arg);
7525 if (evaluate)
7526 {
7527 if (len != 0)
7528 {
7529 cc = name[len];
7530 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007531 /* first try vim_getenv(), fast for normal environment vars */
7532 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007534 {
7535 if (!mustfree)
7536 string = vim_strsave(string);
7537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 else
7539 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007540 if (mustfree)
7541 vim_free(string);
7542
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 /* next try expanding things like $VIM and ${HOME} */
7544 string = expand_env_save(name - 1);
7545 if (string != NULL && *string == '$')
7546 {
7547 vim_free(string);
7548 string = NULL;
7549 }
7550 }
7551 name[len] = cc;
7552 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007553 rettv->v_type = VAR_STRING;
7554 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 }
7556
7557 return OK;
7558}
7559
7560/*
7561 * Array with names and number of arguments of all internal functions
7562 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7563 */
7564static struct fst
7565{
7566 char *f_name; /* function name */
7567 char f_min_argc; /* minimal number of arguments */
7568 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007570 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571} functions[] =
7572{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007573#ifdef FEAT_FLOAT
7574 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007575 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007576#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007577 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 {"append", 2, 2, f_append},
7579 {"argc", 0, 0, f_argc},
7580 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007581 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007582#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007583 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007584 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007585 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007588 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 {"bufexists", 1, 1, f_bufexists},
7590 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7591 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7592 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7593 {"buflisted", 1, 1, f_buflisted},
7594 {"bufloaded", 1, 1, f_bufloaded},
7595 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007596 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 {"bufwinnr", 1, 1, f_bufwinnr},
7598 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007599 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007600 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007601#ifdef FEAT_FLOAT
7602 {"ceil", 1, 1, f_ceil},
7603#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007604 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 {"char2nr", 1, 1, f_char2nr},
7606 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007607 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007609#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007610 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007611 {"complete_add", 1, 1, f_complete_add},
7612 {"complete_check", 0, 0, f_complete_check},
7613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007615 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007616#ifdef FEAT_FLOAT
7617 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007618 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007619#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007620 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007622 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007623 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 {"delete", 1, 1, f_delete},
7625 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007626 {"diff_filler", 1, 1, f_diff_filler},
7627 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007628 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007630 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 {"eventhandler", 0, 0, f_eventhandler},
7632 {"executable", 1, 1, f_executable},
7633 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007634#ifdef FEAT_FLOAT
7635 {"exp", 1, 1, f_exp},
7636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007638 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007639 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7641 {"filereadable", 1, 1, f_filereadable},
7642 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007643 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007644 {"finddir", 1, 3, f_finddir},
7645 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007646#ifdef FEAT_FLOAT
7647 {"float2nr", 1, 1, f_float2nr},
7648 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007649 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007650#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007651 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {"fnamemodify", 2, 2, f_fnamemodify},
7653 {"foldclosed", 1, 1, f_foldclosed},
7654 {"foldclosedend", 1, 1, f_foldclosedend},
7655 {"foldlevel", 1, 1, f_foldlevel},
7656 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007657 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007659 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007660 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007661 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007662 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 {"getbufvar", 2, 2, f_getbufvar},
7664 {"getchar", 0, 1, f_getchar},
7665 {"getcharmod", 0, 0, f_getcharmod},
7666 {"getcmdline", 0, 0, f_getcmdline},
7667 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007668 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007670 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007671 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 {"getfsize", 1, 1, f_getfsize},
7673 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007674 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007675 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007676 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007677 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007678 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007679 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007680 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007681 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007683 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007684 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 {"getwinposx", 0, 0, f_getwinposx},
7686 {"getwinposy", 0, 0, f_getwinposy},
7687 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007688 {"glob", 1, 2, f_glob},
7689 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007691 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007692 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007693 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7695 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7696 {"histadd", 2, 2, f_histadd},
7697 {"histdel", 1, 2, f_histdel},
7698 {"histget", 1, 2, f_histget},
7699 {"histnr", 1, 1, f_histnr},
7700 {"hlID", 1, 1, f_hlID},
7701 {"hlexists", 1, 1, f_hlexists},
7702 {"hostname", 0, 0, f_hostname},
7703 {"iconv", 3, 3, f_iconv},
7704 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007705 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007706 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007708 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 {"inputrestore", 0, 0, f_inputrestore},
7710 {"inputsave", 0, 0, f_inputsave},
7711 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007712 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007714 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007716 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007719 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 {"libcall", 3, 3, f_libcall},
7721 {"libcallnr", 3, 3, f_libcallnr},
7722 {"line", 1, 1, f_line},
7723 {"line2byte", 1, 1, f_line2byte},
7724 {"lispindent", 1, 1, f_lispindent},
7725 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007726#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007727 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007728 {"log10", 1, 1, f_log10},
7729#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007730 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007731 {"maparg", 1, 3, f_maparg},
7732 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007733 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007734 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007735 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007736 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007737 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007738 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007739 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007740 {"max", 1, 1, f_max},
7741 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007742#ifdef vim_mkdir
7743 {"mkdir", 1, 3, f_mkdir},
7744#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007745 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007746#ifdef FEAT_MZSCHEME
7747 {"mzeval", 1, 1, f_mzeval},
7748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 {"nextnonblank", 1, 1, f_nextnonblank},
7750 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007751 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007752#ifdef FEAT_FLOAT
7753 {"pow", 2, 2, f_pow},
7754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007756 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007757 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007759 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007760 {"reltime", 0, 2, f_reltime},
7761 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {"remote_expr", 2, 3, f_remote_expr},
7763 {"remote_foreground", 1, 1, f_remote_foreground},
7764 {"remote_peek", 1, 2, f_remote_peek},
7765 {"remote_read", 1, 1, f_remote_read},
7766 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007767 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007769 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007771 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007772#ifdef FEAT_FLOAT
7773 {"round", 1, 1, f_round},
7774#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007775 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007776 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007777 {"searchpair", 3, 7, f_searchpair},
7778 {"searchpairpos", 3, 7, f_searchpairpos},
7779 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 {"server2client", 2, 2, f_server2client},
7781 {"serverlist", 0, 0, f_serverlist},
7782 {"setbufvar", 3, 3, f_setbufvar},
7783 {"setcmdpos", 1, 1, f_setcmdpos},
7784 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007785 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007786 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007787 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007788 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007790 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007791 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007793 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007795#ifdef FEAT_FLOAT
7796 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007797 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007798#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007799 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007800 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007801 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007802 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007803 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007804#ifdef FEAT_FLOAT
7805 {"sqrt", 1, 1, f_sqrt},
7806 {"str2float", 1, 1, f_str2float},
7807#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007808 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809#ifdef HAVE_STRFTIME
7810 {"strftime", 1, 2, f_strftime},
7811#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007812 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007813 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 {"strlen", 1, 1, f_strlen},
7815 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007816 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 {"strtrans", 1, 1, f_strtrans},
7818 {"submatch", 1, 1, f_submatch},
7819 {"substitute", 4, 4, f_substitute},
7820 {"synID", 3, 3, f_synID},
7821 {"synIDattr", 2, 3, f_synIDattr},
7822 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007823 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007824 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007825 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007826 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007827 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007828 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007829 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007830#ifdef FEAT_FLOAT
7831 {"tan", 1, 1, f_tan},
7832 {"tanh", 1, 1, f_tanh},
7833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007835 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 {"tolower", 1, 1, f_tolower},
7837 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007838 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007839#ifdef FEAT_FLOAT
7840 {"trunc", 1, 1, f_trunc},
7841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007843 {"undofile", 1, 1, f_undofile},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007844 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 {"virtcol", 1, 1, f_virtcol},
7846 {"visualmode", 0, 1, f_visualmode},
7847 {"winbufnr", 1, 1, f_winbufnr},
7848 {"wincol", 0, 0, f_wincol},
7849 {"winheight", 1, 1, f_winheight},
7850 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007851 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007853 {"winrestview", 1, 1, f_winrestview},
7854 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007856 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857};
7858
7859#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7860
7861/*
7862 * Function given to ExpandGeneric() to obtain the list of internal
7863 * or user defined function names.
7864 */
7865 char_u *
7866get_function_name(xp, idx)
7867 expand_T *xp;
7868 int idx;
7869{
7870 static int intidx = -1;
7871 char_u *name;
7872
7873 if (idx == 0)
7874 intidx = -1;
7875 if (intidx < 0)
7876 {
7877 name = get_user_func_name(xp, idx);
7878 if (name != NULL)
7879 return name;
7880 }
7881 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7882 {
7883 STRCPY(IObuff, functions[intidx].f_name);
7884 STRCAT(IObuff, "(");
7885 if (functions[intidx].f_max_argc == 0)
7886 STRCAT(IObuff, ")");
7887 return IObuff;
7888 }
7889
7890 return NULL;
7891}
7892
7893/*
7894 * Function given to ExpandGeneric() to obtain the list of internal or
7895 * user defined variable or function names.
7896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 char_u *
7898get_expr_name(xp, idx)
7899 expand_T *xp;
7900 int idx;
7901{
7902 static int intidx = -1;
7903 char_u *name;
7904
7905 if (idx == 0)
7906 intidx = -1;
7907 if (intidx < 0)
7908 {
7909 name = get_function_name(xp, idx);
7910 if (name != NULL)
7911 return name;
7912 }
7913 return get_user_var_name(xp, ++intidx);
7914}
7915
7916#endif /* FEAT_CMDL_COMPL */
7917
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007918#if defined(EBCDIC) || defined(PROTO)
7919/*
7920 * Compare struct fst by function name.
7921 */
7922 static int
7923compare_func_name(s1, s2)
7924 const void *s1;
7925 const void *s2;
7926{
7927 struct fst *p1 = (struct fst *)s1;
7928 struct fst *p2 = (struct fst *)s2;
7929
7930 return STRCMP(p1->f_name, p2->f_name);
7931}
7932
7933/*
7934 * Sort the function table by function name.
7935 * The sorting of the table above is ASCII dependant.
7936 * On machines using EBCDIC we have to sort it.
7937 */
7938 static void
7939sortFunctions()
7940{
7941 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7942
7943 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
7944}
7945#endif
7946
7947
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948/*
7949 * Find internal function in table above.
7950 * Return index, or -1 if not found
7951 */
7952 static int
7953find_internal_func(name)
7954 char_u *name; /* name of the function */
7955{
7956 int first = 0;
7957 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7958 int cmp;
7959 int x;
7960
7961 /*
7962 * Find the function name in the table. Binary search.
7963 */
7964 while (first <= last)
7965 {
7966 x = first + ((unsigned)(last - first) >> 1);
7967 cmp = STRCMP(name, functions[x].f_name);
7968 if (cmp < 0)
7969 last = x - 1;
7970 else if (cmp > 0)
7971 first = x + 1;
7972 else
7973 return x;
7974 }
7975 return -1;
7976}
7977
7978/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007979 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7980 * name it contains, otherwise return "name".
7981 */
7982 static char_u *
7983deref_func_name(name, lenp)
7984 char_u *name;
7985 int *lenp;
7986{
Bram Moolenaar33570922005-01-25 22:26:29 +00007987 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007988 int cc;
7989
7990 cc = name[*lenp];
7991 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007992 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007993 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007994 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007995 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007996 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007997 {
7998 *lenp = 0;
7999 return (char_u *)""; /* just in case */
8000 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008001 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008002 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008003 }
8004
8005 return name;
8006}
8007
8008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 * Allocate a variable for the result of a function.
8010 * Return OK or FAIL.
8011 */
8012 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008013get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8014 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 char_u *name; /* name of the function */
8016 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 char_u **arg; /* argument, pointing to the '(' */
8019 linenr_T firstline; /* first line of range */
8020 linenr_T lastline; /* last line of range */
8021 int *doesrange; /* return: function handled range */
8022 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008023 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024{
8025 char_u *argp;
8026 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008027 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 int argcount = 0; /* number of arguments found */
8029
8030 /*
8031 * Get the arguments.
8032 */
8033 argp = *arg;
8034 while (argcount < MAX_FUNC_ARGS)
8035 {
8036 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8037 if (*argp == ')' || *argp == ',' || *argp == NUL)
8038 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8040 {
8041 ret = FAIL;
8042 break;
8043 }
8044 ++argcount;
8045 if (*argp != ',')
8046 break;
8047 }
8048 if (*argp == ')')
8049 ++argp;
8050 else
8051 ret = FAIL;
8052
8053 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008054 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008055 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008057 {
8058 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008059 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008060 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008061 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063
8064 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008065 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066
8067 *arg = skipwhite(argp);
8068 return ret;
8069}
8070
8071
8072/*
8073 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008074 * Return OK when the function can't be called, FAIL otherwise.
8075 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 */
8077 static int
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008078call_func(func_name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008079 doesrange, evaluate, selfdict)
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008080 char_u *func_name; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008082 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008084 typval_T *argvars; /* vars for arguments, must have "argcount"
8085 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 linenr_T firstline; /* first line of range */
8087 linenr_T lastline; /* last line of range */
8088 int *doesrange; /* return: function handled range */
8089 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008090 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091{
8092 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093#define ERROR_UNKNOWN 0
8094#define ERROR_TOOMANY 1
8095#define ERROR_TOOFEW 2
8096#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008097#define ERROR_DICT 4
8098#define ERROR_NONE 5
8099#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 int error = ERROR_NONE;
8101 int i;
8102 int llen;
8103 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104#define FLEN_FIXED 40
8105 char_u fname_buf[FLEN_FIXED + 1];
8106 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008107 char_u *name;
8108
8109 /* Make a copy of the name, if it comes from a funcref variable it could
8110 * be changed or deleted in the called function. */
8111 name = vim_strnsave(func_name, len);
8112 if (name == NULL)
8113 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114
8115 /*
8116 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8117 * Change <SNR>123_name() to K_SNR 123_name().
8118 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 llen = eval_fname_script(name);
8121 if (llen > 0)
8122 {
8123 fname_buf[0] = K_SPECIAL;
8124 fname_buf[1] = KS_EXTRA;
8125 fname_buf[2] = (int)KE_SNR;
8126 i = 3;
8127 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8128 {
8129 if (current_SID <= 0)
8130 error = ERROR_SCRIPT;
8131 else
8132 {
8133 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8134 i = (int)STRLEN(fname_buf);
8135 }
8136 }
8137 if (i + STRLEN(name + llen) < FLEN_FIXED)
8138 {
8139 STRCPY(fname_buf + i, name + llen);
8140 fname = fname_buf;
8141 }
8142 else
8143 {
8144 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8145 if (fname == NULL)
8146 error = ERROR_OTHER;
8147 else
8148 {
8149 mch_memmove(fname, fname_buf, (size_t)i);
8150 STRCPY(fname + i, name + llen);
8151 }
8152 }
8153 }
8154 else
8155 fname = name;
8156
8157 *doesrange = FALSE;
8158
8159
8160 /* execute the function if no errors detected and executing */
8161 if (evaluate && error == ERROR_NONE)
8162 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008163 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8164 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 error = ERROR_UNKNOWN;
8166
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008167 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {
8169 /*
8170 * User defined function.
8171 */
8172 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008173
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008175 /* Trigger FuncUndefined event, may load the function. */
8176 if (fp == NULL
8177 && apply_autocmds(EVENT_FUNCUNDEFINED,
8178 fname, fname, TRUE, NULL)
8179 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008181 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 fp = find_func(fname);
8183 }
8184#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008185 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008186 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008187 {
8188 /* loaded a package, search for the function again */
8189 fp = find_func(fname);
8190 }
8191
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 if (fp != NULL)
8193 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008194 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008196 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008198 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008200 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008201 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 else
8203 {
8204 /*
8205 * Call the user function.
8206 * Save and restore search patterns, script variables and
8207 * redo buffer.
8208 */
8209 save_search_patterns();
8210 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008211 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008212 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008213 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008214 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8215 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8216 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008217 /* Function was unreferenced while being used, free it
8218 * now. */
8219 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 restoreRedobuff();
8221 restore_search_patterns();
8222 error = ERROR_NONE;
8223 }
8224 }
8225 }
8226 else
8227 {
8228 /*
8229 * Find the function name in the table, call its implementation.
8230 */
8231 i = find_internal_func(fname);
8232 if (i >= 0)
8233 {
8234 if (argcount < functions[i].f_min_argc)
8235 error = ERROR_TOOFEW;
8236 else if (argcount > functions[i].f_max_argc)
8237 error = ERROR_TOOMANY;
8238 else
8239 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008240 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008241 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 error = ERROR_NONE;
8243 }
8244 }
8245 }
8246 /*
8247 * The function call (or "FuncUndefined" autocommand sequence) might
8248 * have been aborted by an error, an interrupt, or an explicitly thrown
8249 * exception that has not been caught so far. This situation can be
8250 * tested for by calling aborting(). For an error in an internal
8251 * function or for the "E132" error in call_user_func(), however, the
8252 * throw point at which the "force_abort" flag (temporarily reset by
8253 * emsg()) is normally updated has not been reached yet. We need to
8254 * update that flag first to make aborting() reliable.
8255 */
8256 update_force_abort();
8257 }
8258 if (error == ERROR_NONE)
8259 ret = OK;
8260
8261 /*
8262 * Report an error unless the argument evaluation or function call has been
8263 * cancelled due to an aborting error, an interrupt, or an exception.
8264 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008265 if (!aborting())
8266 {
8267 switch (error)
8268 {
8269 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008270 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008271 break;
8272 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008273 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008274 break;
8275 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008276 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008277 name);
8278 break;
8279 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008280 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008281 name);
8282 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008283 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008284 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008285 name);
8286 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008287 }
8288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 if (fname != name && fname != fname_buf)
8291 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008292 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293
8294 return ret;
8295}
8296
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008297/*
8298 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008299 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008300 */
8301 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008302emsg_funcname(ermsg, name)
8303 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008304 char_u *name;
8305{
8306 char_u *p;
8307
8308 if (*name == K_SPECIAL)
8309 p = concat_str((char_u *)"<SNR>", name + 3);
8310 else
8311 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008312 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008313 if (p != name)
8314 vim_free(p);
8315}
8316
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008317/*
8318 * Return TRUE for a non-zero Number and a non-empty String.
8319 */
8320 static int
8321non_zero_arg(argvars)
8322 typval_T *argvars;
8323{
8324 return ((argvars[0].v_type == VAR_NUMBER
8325 && argvars[0].vval.v_number != 0)
8326 || (argvars[0].v_type == VAR_STRING
8327 && argvars[0].vval.v_string != NULL
8328 && *argvars[0].vval.v_string != NUL));
8329}
8330
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331/*********************************************
8332 * Implementation of the built-in functions
8333 */
8334
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008335#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008336static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8337
8338/*
8339 * Get the float value of "argvars[0]" into "f".
8340 * Returns FAIL when the argument is not a Number or Float.
8341 */
8342 static int
8343get_float_arg(argvars, f)
8344 typval_T *argvars;
8345 float_T *f;
8346{
8347 if (argvars[0].v_type == VAR_FLOAT)
8348 {
8349 *f = argvars[0].vval.v_float;
8350 return OK;
8351 }
8352 if (argvars[0].v_type == VAR_NUMBER)
8353 {
8354 *f = (float_T)argvars[0].vval.v_number;
8355 return OK;
8356 }
8357 EMSG(_("E808: Number or Float required"));
8358 return FAIL;
8359}
8360
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008361/*
8362 * "abs(expr)" function
8363 */
8364 static void
8365f_abs(argvars, rettv)
8366 typval_T *argvars;
8367 typval_T *rettv;
8368{
8369 if (argvars[0].v_type == VAR_FLOAT)
8370 {
8371 rettv->v_type = VAR_FLOAT;
8372 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8373 }
8374 else
8375 {
8376 varnumber_T n;
8377 int error = FALSE;
8378
8379 n = get_tv_number_chk(&argvars[0], &error);
8380 if (error)
8381 rettv->vval.v_number = -1;
8382 else if (n > 0)
8383 rettv->vval.v_number = n;
8384 else
8385 rettv->vval.v_number = -n;
8386 }
8387}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008388
8389/*
8390 * "acos()" function
8391 */
8392 static void
8393f_acos(argvars, rettv)
8394 typval_T *argvars;
8395 typval_T *rettv;
8396{
8397 float_T f;
8398
8399 rettv->v_type = VAR_FLOAT;
8400 if (get_float_arg(argvars, &f) == OK)
8401 rettv->vval.v_float = acos(f);
8402 else
8403 rettv->vval.v_float = 0.0;
8404}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008405#endif
8406
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008408 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 */
8410 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008411f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008412 typval_T *argvars;
8413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414{
Bram Moolenaar33570922005-01-25 22:26:29 +00008415 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008417 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008418 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008420 if ((l = argvars[0].vval.v_list) != NULL
8421 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8422 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008423 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008424 }
8425 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008426 EMSG(_(e_listreq));
8427}
8428
8429/*
8430 * "append(lnum, string/list)" function
8431 */
8432 static void
8433f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008434 typval_T *argvars;
8435 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008436{
8437 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008438 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008439 list_T *l = NULL;
8440 listitem_T *li = NULL;
8441 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008442 long added = 0;
8443
Bram Moolenaar0d660222005-01-07 21:51:51 +00008444 lnum = get_tv_lnum(argvars);
8445 if (lnum >= 0
8446 && lnum <= curbuf->b_ml.ml_line_count
8447 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008448 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008449 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008450 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008451 l = argvars[1].vval.v_list;
8452 if (l == NULL)
8453 return;
8454 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008455 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008456 for (;;)
8457 {
8458 if (l == NULL)
8459 tv = &argvars[1]; /* append a string */
8460 else if (li == NULL)
8461 break; /* end of list */
8462 else
8463 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008464 line = get_tv_string_chk(tv);
8465 if (line == NULL) /* type error */
8466 {
8467 rettv->vval.v_number = 1; /* Failed */
8468 break;
8469 }
8470 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008471 ++added;
8472 if (l == NULL)
8473 break;
8474 li = li->li_next;
8475 }
8476
8477 appended_lines_mark(lnum, added);
8478 if (curwin->w_cursor.lnum > lnum)
8479 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008481 else
8482 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483}
8484
8485/*
8486 * "argc()" function
8487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008489f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008490 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008493 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494}
8495
8496/*
8497 * "argidx()" function
8498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008500f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008501 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008504 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505}
8506
8507/*
8508 * "argv(nr)" function
8509 */
8510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008511f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008512 typval_T *argvars;
8513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514{
8515 int idx;
8516
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008517 if (argvars[0].v_type != VAR_UNKNOWN)
8518 {
8519 idx = get_tv_number_chk(&argvars[0], NULL);
8520 if (idx >= 0 && idx < ARGCOUNT)
8521 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8522 else
8523 rettv->vval.v_string = NULL;
8524 rettv->v_type = VAR_STRING;
8525 }
8526 else if (rettv_list_alloc(rettv) == OK)
8527 for (idx = 0; idx < ARGCOUNT; ++idx)
8528 list_append_string(rettv->vval.v_list,
8529 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530}
8531
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008532#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008533/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008534 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008535 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008536 static void
8537f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008538 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008539 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008540{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008541 float_T f;
8542
8543 rettv->v_type = VAR_FLOAT;
8544 if (get_float_arg(argvars, &f) == OK)
8545 rettv->vval.v_float = asin(f);
8546 else
8547 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008548}
8549
8550/*
8551 * "atan()" function
8552 */
8553 static void
8554f_atan(argvars, rettv)
8555 typval_T *argvars;
8556 typval_T *rettv;
8557{
8558 float_T f;
8559
8560 rettv->v_type = VAR_FLOAT;
8561 if (get_float_arg(argvars, &f) == OK)
8562 rettv->vval.v_float = atan(f);
8563 else
8564 rettv->vval.v_float = 0.0;
8565}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008566
8567/*
8568 * "atan2()" function
8569 */
8570 static void
8571f_atan2(argvars, rettv)
8572 typval_T *argvars;
8573 typval_T *rettv;
8574{
8575 float_T fx, fy;
8576
8577 rettv->v_type = VAR_FLOAT;
8578 if (get_float_arg(argvars, &fx) == OK
8579 && get_float_arg(&argvars[1], &fy) == OK)
8580 rettv->vval.v_float = atan2(fx, fy);
8581 else
8582 rettv->vval.v_float = 0.0;
8583}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008584#endif
8585
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586/*
8587 * "browse(save, title, initdir, default)" function
8588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008591 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593{
8594#ifdef FEAT_BROWSE
8595 int save;
8596 char_u *title;
8597 char_u *initdir;
8598 char_u *defname;
8599 char_u buf[NUMBUFLEN];
8600 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008601 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008603 save = get_tv_number_chk(&argvars[0], &error);
8604 title = get_tv_string_chk(&argvars[1]);
8605 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8606 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008608 if (error || title == NULL || initdir == NULL || defname == NULL)
8609 rettv->vval.v_string = NULL;
8610 else
8611 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008612 do_browse(save ? BROWSE_SAVE : 0,
8613 title, defname, NULL, initdir, NULL, curbuf);
8614#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008615 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008616#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008617 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008618}
8619
8620/*
8621 * "browsedir(title, initdir)" function
8622 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008624f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008625 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008626 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008627{
8628#ifdef FEAT_BROWSE
8629 char_u *title;
8630 char_u *initdir;
8631 char_u buf[NUMBUFLEN];
8632
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008633 title = get_tv_string_chk(&argvars[0]);
8634 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008636 if (title == NULL || initdir == NULL)
8637 rettv->vval.v_string = NULL;
8638 else
8639 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008640 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008642 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008644 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645}
8646
Bram Moolenaar33570922005-01-25 22:26:29 +00008647static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008648
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649/*
8650 * Find a buffer by number or exact name.
8651 */
8652 static buf_T *
8653find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008654 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655{
8656 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008658 if (avar->v_type == VAR_NUMBER)
8659 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008660 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008662 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008663 if (buf == NULL)
8664 {
8665 /* No full path name match, try a match with a URL or a "nofile"
8666 * buffer, these don't use the full path. */
8667 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8668 if (buf->b_fname != NULL
8669 && (path_with_url(buf->b_fname)
8670#ifdef FEAT_QUICKFIX
8671 || bt_nofile(buf)
8672#endif
8673 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008674 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008675 break;
8676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 }
8678 return buf;
8679}
8680
8681/*
8682 * "bufexists(expr)" function
8683 */
8684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008685f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008686 typval_T *argvars;
8687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008689 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690}
8691
8692/*
8693 * "buflisted(expr)" function
8694 */
8695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008696f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008697 typval_T *argvars;
8698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699{
8700 buf_T *buf;
8701
8702 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008703 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704}
8705
8706/*
8707 * "bufloaded(expr)" function
8708 */
8709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008710f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008711 typval_T *argvars;
8712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713{
8714 buf_T *buf;
8715
8716 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008717 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718}
8719
Bram Moolenaar33570922005-01-25 22:26:29 +00008720static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008721
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722/*
8723 * Get buffer by number or pattern.
8724 */
8725 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008726get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008727 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008729 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 int save_magic;
8731 char_u *save_cpo;
8732 buf_T *buf;
8733
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008734 if (tv->v_type == VAR_NUMBER)
8735 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008736 if (tv->v_type != VAR_STRING)
8737 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 if (name == NULL || *name == NUL)
8739 return curbuf;
8740 if (name[0] == '$' && name[1] == NUL)
8741 return lastbuf;
8742
8743 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8744 save_magic = p_magic;
8745 p_magic = TRUE;
8746 save_cpo = p_cpo;
8747 p_cpo = (char_u *)"";
8748
8749 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8750 TRUE, FALSE));
8751
8752 p_magic = save_magic;
8753 p_cpo = save_cpo;
8754
8755 /* If not found, try expanding the name, like done for bufexists(). */
8756 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008757 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758
8759 return buf;
8760}
8761
8762/*
8763 * "bufname(expr)" function
8764 */
8765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008766f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008767 typval_T *argvars;
8768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769{
8770 buf_T *buf;
8771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008772 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008774 buf = get_buf_tv(&argvars[0]);
8775 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008779 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 --emsg_off;
8781}
8782
8783/*
8784 * "bufnr(expr)" function
8785 */
8786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008787f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008788 typval_T *argvars;
8789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790{
8791 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008792 int error = FALSE;
8793 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008795 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008797 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008798 --emsg_off;
8799
8800 /* If the buffer isn't found and the second argument is not zero create a
8801 * new buffer. */
8802 if (buf == NULL
8803 && argvars[1].v_type != VAR_UNKNOWN
8804 && get_tv_number_chk(&argvars[1], &error) != 0
8805 && !error
8806 && (name = get_tv_string_chk(&argvars[0])) != NULL
8807 && !error)
8808 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8809
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008811 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008813 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814}
8815
8816/*
8817 * "bufwinnr(nr)" function
8818 */
8819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008820f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008821 typval_T *argvars;
8822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823{
8824#ifdef FEAT_WINDOWS
8825 win_T *wp;
8826 int winnr = 0;
8827#endif
8828 buf_T *buf;
8829
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008830 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008832 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833#ifdef FEAT_WINDOWS
8834 for (wp = firstwin; wp; wp = wp->w_next)
8835 {
8836 ++winnr;
8837 if (wp->w_buffer == buf)
8838 break;
8839 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008840 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008842 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843#endif
8844 --emsg_off;
8845}
8846
8847/*
8848 * "byte2line(byte)" function
8849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008851f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008852 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854{
8855#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857#else
8858 long boff = 0;
8859
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008860 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008864 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 (linenr_T)0, &boff);
8866#endif
8867}
8868
8869/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008870 * "byteidx()" function
8871 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008873f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008874 typval_T *argvars;
8875 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008876{
8877#ifdef FEAT_MBYTE
8878 char_u *t;
8879#endif
8880 char_u *str;
8881 long idx;
8882
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008883 str = get_tv_string_chk(&argvars[0]);
8884 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008885 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008886 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008887 return;
8888
8889#ifdef FEAT_MBYTE
8890 t = str;
8891 for ( ; idx > 0; idx--)
8892 {
8893 if (*t == NUL) /* EOL reached */
8894 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008895 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008896 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008897 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008898#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008899 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008900 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008901#endif
8902}
8903
8904/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008905 * "call(func, arglist)" function
8906 */
8907 static void
8908f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008909 typval_T *argvars;
8910 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008911{
8912 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008913 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008914 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008915 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008916 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008917 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008918
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008919 if (argvars[1].v_type != VAR_LIST)
8920 {
8921 EMSG(_(e_listreq));
8922 return;
8923 }
8924 if (argvars[1].vval.v_list == NULL)
8925 return;
8926
8927 if (argvars[0].v_type == VAR_FUNC)
8928 func = argvars[0].vval.v_string;
8929 else
8930 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008931 if (*func == NUL)
8932 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008933
Bram Moolenaare9a41262005-01-15 22:18:47 +00008934 if (argvars[2].v_type != VAR_UNKNOWN)
8935 {
8936 if (argvars[2].v_type != VAR_DICT)
8937 {
8938 EMSG(_(e_dictreq));
8939 return;
8940 }
8941 selfdict = argvars[2].vval.v_dict;
8942 }
8943
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008944 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8945 item = item->li_next)
8946 {
8947 if (argc == MAX_FUNC_ARGS)
8948 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008949 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008950 break;
8951 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008952 /* Make a copy of each argument. This is needed to be able to set
8953 * v_lock to VAR_FIXED in the copy without changing the original list.
8954 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008955 copy_tv(&item->li_tv, &argv[argc++]);
8956 }
8957
8958 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008959 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008960 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8961 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008962
8963 /* Free the arguments. */
8964 while (argc > 0)
8965 clear_tv(&argv[--argc]);
8966}
8967
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008968#ifdef FEAT_FLOAT
8969/*
8970 * "ceil({float})" function
8971 */
8972 static void
8973f_ceil(argvars, rettv)
8974 typval_T *argvars;
8975 typval_T *rettv;
8976{
8977 float_T f;
8978
8979 rettv->v_type = VAR_FLOAT;
8980 if (get_float_arg(argvars, &f) == OK)
8981 rettv->vval.v_float = ceil(f);
8982 else
8983 rettv->vval.v_float = 0.0;
8984}
8985#endif
8986
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008987/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008988 * "changenr()" function
8989 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008990 static void
8991f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008992 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008993 typval_T *rettv;
8994{
8995 rettv->vval.v_number = curbuf->b_u_seq_cur;
8996}
8997
8998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 * "char2nr(string)" function
9000 */
9001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009003 typval_T *argvars;
9004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005{
9006#ifdef FEAT_MBYTE
9007 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009008 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 else
9010#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009011 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012}
9013
9014/*
9015 * "cindent(lnum)" function
9016 */
9017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 typval_T *argvars;
9020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021{
9022#ifdef FEAT_CINDENT
9023 pos_T pos;
9024 linenr_T lnum;
9025
9026 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9029 {
9030 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009031 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 curwin->w_cursor = pos;
9033 }
9034 else
9035#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009036 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037}
9038
9039/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009040 * "clearmatches()" function
9041 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009042 static void
9043f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009044 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009045 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009046{
9047#ifdef FEAT_SEARCH_EXTRA
9048 clear_matches(curwin);
9049#endif
9050}
9051
9052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 * "col(string)" function
9054 */
9055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009056f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009057 typval_T *argvars;
9058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059{
9060 colnr_T col = 0;
9061 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009062 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009064 fp = var2fpos(&argvars[0], FALSE, &fnum);
9065 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 {
9067 if (fp->col == MAXCOL)
9068 {
9069 /* '> can be MAXCOL, get the length of the line then */
9070 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009071 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 else
9073 col = MAXCOL;
9074 }
9075 else
9076 {
9077 col = fp->col + 1;
9078#ifdef FEAT_VIRTUALEDIT
9079 /* col(".") when the cursor is on the NUL at the end of the line
9080 * because of "coladd" can be seen as an extra column. */
9081 if (virtual_active() && fp == &curwin->w_cursor)
9082 {
9083 char_u *p = ml_get_cursor();
9084
9085 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9086 curwin->w_virtcol - curwin->w_cursor.coladd))
9087 {
9088# ifdef FEAT_MBYTE
9089 int l;
9090
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009091 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 col += l;
9093# else
9094 if (*p != NUL && p[1] == NUL)
9095 ++col;
9096# endif
9097 }
9098 }
9099#endif
9100 }
9101 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009102 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103}
9104
Bram Moolenaar572cb562005-08-05 21:35:02 +00009105#if defined(FEAT_INS_EXPAND)
9106/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009107 * "complete()" function
9108 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009109 static void
9110f_complete(argvars, rettv)
9111 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009112 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009113{
9114 int startcol;
9115
9116 if ((State & INSERT) == 0)
9117 {
9118 EMSG(_("E785: complete() can only be used in Insert mode"));
9119 return;
9120 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009121
9122 /* Check for undo allowed here, because if something was already inserted
9123 * the line was already saved for undo and this check isn't done. */
9124 if (!undo_allowed())
9125 return;
9126
Bram Moolenaarade00832006-03-10 21:46:58 +00009127 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9128 {
9129 EMSG(_(e_invarg));
9130 return;
9131 }
9132
9133 startcol = get_tv_number_chk(&argvars[0], NULL);
9134 if (startcol <= 0)
9135 return;
9136
9137 set_completion(startcol - 1, argvars[1].vval.v_list);
9138}
9139
9140/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009141 * "complete_add()" function
9142 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009143 static void
9144f_complete_add(argvars, rettv)
9145 typval_T *argvars;
9146 typval_T *rettv;
9147{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009148 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009149}
9150
9151/*
9152 * "complete_check()" function
9153 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009154 static void
9155f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009156 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009157 typval_T *rettv;
9158{
9159 int saved = RedrawingDisabled;
9160
9161 RedrawingDisabled = 0;
9162 ins_compl_check_keys(0);
9163 rettv->vval.v_number = compl_interrupted;
9164 RedrawingDisabled = saved;
9165}
9166#endif
9167
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168/*
9169 * "confirm(message, buttons[, default [, type]])" function
9170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009172f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009173 typval_T *argvars UNUSED;
9174 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175{
9176#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9177 char_u *message;
9178 char_u *buttons = NULL;
9179 char_u buf[NUMBUFLEN];
9180 char_u buf2[NUMBUFLEN];
9181 int def = 1;
9182 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009183 char_u *typestr;
9184 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009186 message = get_tv_string_chk(&argvars[0]);
9187 if (message == NULL)
9188 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009189 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009191 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9192 if (buttons == NULL)
9193 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009194 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009196 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009197 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009199 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9200 if (typestr == NULL)
9201 error = TRUE;
9202 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009204 switch (TOUPPER_ASC(*typestr))
9205 {
9206 case 'E': type = VIM_ERROR; break;
9207 case 'Q': type = VIM_QUESTION; break;
9208 case 'I': type = VIM_INFO; break;
9209 case 'W': type = VIM_WARNING; break;
9210 case 'G': type = VIM_GENERIC; break;
9211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 }
9213 }
9214 }
9215 }
9216
9217 if (buttons == NULL || *buttons == NUL)
9218 buttons = (char_u *)_("&Ok");
9219
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009220 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009221 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223#endif
9224}
9225
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009226/*
9227 * "copy()" function
9228 */
9229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009230f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009231 typval_T *argvars;
9232 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009233{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009234 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009235}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009237#ifdef FEAT_FLOAT
9238/*
9239 * "cos()" function
9240 */
9241 static void
9242f_cos(argvars, rettv)
9243 typval_T *argvars;
9244 typval_T *rettv;
9245{
9246 float_T f;
9247
9248 rettv->v_type = VAR_FLOAT;
9249 if (get_float_arg(argvars, &f) == OK)
9250 rettv->vval.v_float = cos(f);
9251 else
9252 rettv->vval.v_float = 0.0;
9253}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009254
9255/*
9256 * "cosh()" function
9257 */
9258 static void
9259f_cosh(argvars, rettv)
9260 typval_T *argvars;
9261 typval_T *rettv;
9262{
9263 float_T f;
9264
9265 rettv->v_type = VAR_FLOAT;
9266 if (get_float_arg(argvars, &f) == OK)
9267 rettv->vval.v_float = cosh(f);
9268 else
9269 rettv->vval.v_float = 0.0;
9270}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009271#endif
9272
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009274 * "count()" function
9275 */
9276 static void
9277f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009278 typval_T *argvars;
9279 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009280{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009281 long n = 0;
9282 int ic = FALSE;
9283
Bram Moolenaare9a41262005-01-15 22:18:47 +00009284 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009285 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009286 listitem_T *li;
9287 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009288 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009289
Bram Moolenaare9a41262005-01-15 22:18:47 +00009290 if ((l = argvars[0].vval.v_list) != NULL)
9291 {
9292 li = l->lv_first;
9293 if (argvars[2].v_type != VAR_UNKNOWN)
9294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009295 int error = FALSE;
9296
9297 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009298 if (argvars[3].v_type != VAR_UNKNOWN)
9299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009300 idx = get_tv_number_chk(&argvars[3], &error);
9301 if (!error)
9302 {
9303 li = list_find(l, idx);
9304 if (li == NULL)
9305 EMSGN(_(e_listidx), idx);
9306 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009307 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009308 if (error)
9309 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009310 }
9311
9312 for ( ; li != NULL; li = li->li_next)
9313 if (tv_equal(&li->li_tv, &argvars[1], ic))
9314 ++n;
9315 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009316 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009317 else if (argvars[0].v_type == VAR_DICT)
9318 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009319 int todo;
9320 dict_T *d;
9321 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009322
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009323 if ((d = argvars[0].vval.v_dict) != NULL)
9324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009325 int error = FALSE;
9326
Bram Moolenaare9a41262005-01-15 22:18:47 +00009327 if (argvars[2].v_type != VAR_UNKNOWN)
9328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009330 if (argvars[3].v_type != VAR_UNKNOWN)
9331 EMSG(_(e_invarg));
9332 }
9333
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009334 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009335 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009336 {
9337 if (!HASHITEM_EMPTY(hi))
9338 {
9339 --todo;
9340 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9341 ++n;
9342 }
9343 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009344 }
9345 }
9346 else
9347 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009348 rettv->vval.v_number = n;
9349}
9350
9351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9353 *
9354 * Checks the existence of a cscope connection.
9355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009357f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009358 typval_T *argvars UNUSED;
9359 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360{
9361#ifdef FEAT_CSCOPE
9362 int num = 0;
9363 char_u *dbpath = NULL;
9364 char_u *prepend = NULL;
9365 char_u buf[NUMBUFLEN];
9366
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009367 if (argvars[0].v_type != VAR_UNKNOWN
9368 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370 num = (int)get_tv_number(&argvars[0]);
9371 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009372 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009373 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 }
9375
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009376 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377#endif
9378}
9379
9380/*
9381 * "cursor(lnum, col)" function
9382 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009383 * Moves the cursor to the specified line and column.
9384 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009387f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009388 typval_T *argvars;
9389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390{
9391 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009392#ifdef FEAT_VIRTUALEDIT
9393 long coladd = 0;
9394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009396 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009397 if (argvars[1].v_type == VAR_UNKNOWN)
9398 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009399 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009400
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009401 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009402 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009403 line = pos.lnum;
9404 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009405#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009406 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009407#endif
9408 }
9409 else
9410 {
9411 line = get_tv_lnum(argvars);
9412 col = get_tv_number_chk(&argvars[1], NULL);
9413#ifdef FEAT_VIRTUALEDIT
9414 if (argvars[2].v_type != VAR_UNKNOWN)
9415 coladd = get_tv_number_chk(&argvars[2], NULL);
9416#endif
9417 }
9418 if (line < 0 || col < 0
9419#ifdef FEAT_VIRTUALEDIT
9420 || coladd < 0
9421#endif
9422 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009423 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 if (line > 0)
9425 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 if (col > 0)
9427 curwin->w_cursor.col = col - 1;
9428#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009429 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430#endif
9431
9432 /* Make sure the cursor is in a valid position. */
9433 check_cursor();
9434#ifdef FEAT_MBYTE
9435 /* Correct cursor for multi-byte character. */
9436 if (has_mbyte)
9437 mb_adjust_cursor();
9438#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009439
9440 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009441 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442}
9443
9444/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009445 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 */
9447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009448f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009449 typval_T *argvars;
9450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009452 int noref = 0;
9453
9454 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009455 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009456 if (noref < 0 || noref > 1)
9457 EMSG(_(e_invarg));
9458 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009459 {
9460 current_copyID += COPYID_INC;
9461 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463}
9464
9465/*
9466 * "delete()" function
9467 */
9468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009469f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009470 typval_T *argvars;
9471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472{
9473 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009476 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477}
9478
9479/*
9480 * "did_filetype()" function
9481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009483f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009484 typval_T *argvars UNUSED;
9485 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486{
9487#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009488 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489#endif
9490}
9491
9492/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009493 * "diff_filler()" function
9494 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009497 typval_T *argvars UNUSED;
9498 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009499{
9500#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009501 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009502#endif
9503}
9504
9505/*
9506 * "diff_hlID()" function
9507 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009509f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009510 typval_T *argvars UNUSED;
9511 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009512{
9513#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009514 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009515 static linenr_T prev_lnum = 0;
9516 static int changedtick = 0;
9517 static int fnum = 0;
9518 static int change_start = 0;
9519 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009520 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009521 int filler_lines;
9522 int col;
9523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009524 if (lnum < 0) /* ignore type error in {lnum} arg */
9525 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009526 if (lnum != prev_lnum
9527 || changedtick != curbuf->b_changedtick
9528 || fnum != curbuf->b_fnum)
9529 {
9530 /* New line, buffer, change: need to get the values. */
9531 filler_lines = diff_check(curwin, lnum);
9532 if (filler_lines < 0)
9533 {
9534 if (filler_lines == -1)
9535 {
9536 change_start = MAXCOL;
9537 change_end = -1;
9538 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9539 hlID = HLF_ADD; /* added line */
9540 else
9541 hlID = HLF_CHD; /* changed line */
9542 }
9543 else
9544 hlID = HLF_ADD; /* added line */
9545 }
9546 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009547 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009548 prev_lnum = lnum;
9549 changedtick = curbuf->b_changedtick;
9550 fnum = curbuf->b_fnum;
9551 }
9552
9553 if (hlID == HLF_CHD || hlID == HLF_TXD)
9554 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009555 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009556 if (col >= change_start && col <= change_end)
9557 hlID = HLF_TXD; /* changed text */
9558 else
9559 hlID = HLF_CHD; /* changed line */
9560 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009561 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009562#endif
9563}
9564
9565/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009566 * "empty({expr})" function
9567 */
9568 static void
9569f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009570 typval_T *argvars;
9571 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009572{
9573 int n;
9574
9575 switch (argvars[0].v_type)
9576 {
9577 case VAR_STRING:
9578 case VAR_FUNC:
9579 n = argvars[0].vval.v_string == NULL
9580 || *argvars[0].vval.v_string == NUL;
9581 break;
9582 case VAR_NUMBER:
9583 n = argvars[0].vval.v_number == 0;
9584 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009585#ifdef FEAT_FLOAT
9586 case VAR_FLOAT:
9587 n = argvars[0].vval.v_float == 0.0;
9588 break;
9589#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009590 case VAR_LIST:
9591 n = argvars[0].vval.v_list == NULL
9592 || argvars[0].vval.v_list->lv_first == NULL;
9593 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009594 case VAR_DICT:
9595 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009596 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009597 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009598 default:
9599 EMSG2(_(e_intern2), "f_empty()");
9600 n = 0;
9601 }
9602
9603 rettv->vval.v_number = n;
9604}
9605
9606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 * "escape({string}, {chars})" function
9608 */
9609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009611 typval_T *argvars;
9612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613{
9614 char_u buf[NUMBUFLEN];
9615
Bram Moolenaar758711c2005-02-02 23:11:38 +00009616 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9617 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619}
9620
9621/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009622 * "eval()" function
9623 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009624 static void
9625f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009626 typval_T *argvars;
9627 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009628{
9629 char_u *s;
9630
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009631 s = get_tv_string_chk(&argvars[0]);
9632 if (s != NULL)
9633 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009635 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9636 {
9637 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009638 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009639 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009640 else if (*s != NUL)
9641 EMSG(_(e_trailing));
9642}
9643
9644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 * "eventhandler()" function
9646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009648f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009649 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009650 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009652 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653}
9654
9655/*
9656 * "executable()" function
9657 */
9658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009659f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009660 typval_T *argvars;
9661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009663 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664}
9665
9666/*
9667 * "exists()" function
9668 */
9669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009670f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009671 typval_T *argvars;
9672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673{
9674 char_u *p;
9675 char_u *name;
9676 int n = FALSE;
9677 int len = 0;
9678
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009679 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 if (*p == '$') /* environment variable */
9681 {
9682 /* first try "normal" environment variables (fast) */
9683 if (mch_getenv(p + 1) != NULL)
9684 n = TRUE;
9685 else
9686 {
9687 /* try expanding things like $VIM and ${HOME} */
9688 p = expand_env_save(p);
9689 if (p != NULL && *p != '$')
9690 n = TRUE;
9691 vim_free(p);
9692 }
9693 }
9694 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009697 if (*skipwhite(p) != NUL)
9698 n = FALSE; /* trailing garbage */
9699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 else if (*p == '*') /* internal or user defined function */
9701 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009702 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 }
9704 else if (*p == ':')
9705 {
9706 n = cmd_exists(p + 1);
9707 }
9708 else if (*p == '#')
9709 {
9710#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009711 if (p[1] == '#')
9712 n = autocmd_supported(p + 2);
9713 else
9714 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715#endif
9716 }
9717 else /* internal variable */
9718 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009719 char_u *tofree;
9720 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009722 /* get_name_len() takes care of expanding curly braces */
9723 name = p;
9724 len = get_name_len(&p, &tofree, TRUE, FALSE);
9725 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009727 if (tofree != NULL)
9728 name = tofree;
9729 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9730 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009732 /* handle d.key, l[idx], f(expr) */
9733 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9734 if (n)
9735 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 }
9737 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009738 if (*p != NUL)
9739 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009741 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 }
9743
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009744 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745}
9746
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009747#ifdef FEAT_FLOAT
9748/*
9749 * "exp()" function
9750 */
9751 static void
9752f_exp(argvars, rettv)
9753 typval_T *argvars;
9754 typval_T *rettv;
9755{
9756 float_T f;
9757
9758 rettv->v_type = VAR_FLOAT;
9759 if (get_float_arg(argvars, &f) == OK)
9760 rettv->vval.v_float = exp(f);
9761 else
9762 rettv->vval.v_float = 0.0;
9763}
9764#endif
9765
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766/*
9767 * "expand()" function
9768 */
9769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009770f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009771 typval_T *argvars;
9772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773{
9774 char_u *s;
9775 int len;
9776 char_u *errormsg;
9777 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9778 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009779 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009781 rettv->v_type = VAR_STRING;
9782 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 if (*s == '%' || *s == '#' || *s == '<')
9784 {
9785 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009786 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 --emsg_off;
9788 }
9789 else
9790 {
9791 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009792 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009793 if (argvars[1].v_type != VAR_UNKNOWN
9794 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009796 if (!error)
9797 {
9798 ExpandInit(&xpc);
9799 xpc.xp_context = EXPAND_FILES;
9800 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009801 }
9802 else
9803 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 }
9805}
9806
9807/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009808 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009809 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009810 */
9811 static void
9812f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009813 typval_T *argvars;
9814 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009815{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009816 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009817 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009818 list_T *l1, *l2;
9819 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009820 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009821 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009822
Bram Moolenaare9a41262005-01-15 22:18:47 +00009823 l1 = argvars[0].vval.v_list;
9824 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009825 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9826 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009827 {
9828 if (argvars[2].v_type != VAR_UNKNOWN)
9829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009830 before = get_tv_number_chk(&argvars[2], &error);
9831 if (error)
9832 return; /* type error; errmsg already given */
9833
Bram Moolenaar758711c2005-02-02 23:11:38 +00009834 if (before == l1->lv_len)
9835 item = NULL;
9836 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009837 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009838 item = list_find(l1, before);
9839 if (item == NULL)
9840 {
9841 EMSGN(_(e_listidx), before);
9842 return;
9843 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009844 }
9845 }
9846 else
9847 item = NULL;
9848 list_extend(l1, l2, item);
9849
Bram Moolenaare9a41262005-01-15 22:18:47 +00009850 copy_tv(&argvars[0], rettv);
9851 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009852 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009853 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9854 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009855 dict_T *d1, *d2;
9856 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009857 char_u *action;
9858 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009859 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009860 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009861
9862 d1 = argvars[0].vval.v_dict;
9863 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009864 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9865 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009866 {
9867 /* Check the third argument. */
9868 if (argvars[2].v_type != VAR_UNKNOWN)
9869 {
9870 static char *(av[]) = {"keep", "force", "error"};
9871
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009872 action = get_tv_string_chk(&argvars[2]);
9873 if (action == NULL)
9874 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009875 for (i = 0; i < 3; ++i)
9876 if (STRCMP(action, av[i]) == 0)
9877 break;
9878 if (i == 3)
9879 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009880 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009881 return;
9882 }
9883 }
9884 else
9885 action = (char_u *)"force";
9886
9887 /* Go over all entries in the second dict and add them to the
9888 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009889 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009890 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009891 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009892 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009893 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009894 --todo;
9895 di1 = dict_find(d1, hi2->hi_key, -1);
9896 if (di1 == NULL)
9897 {
9898 di1 = dictitem_copy(HI2DI(hi2));
9899 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9900 dictitem_free(di1);
9901 }
9902 else if (*action == 'e')
9903 {
9904 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9905 break;
9906 }
9907 else if (*action == 'f')
9908 {
9909 clear_tv(&di1->di_tv);
9910 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9911 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009912 }
9913 }
9914
Bram Moolenaare9a41262005-01-15 22:18:47 +00009915 copy_tv(&argvars[0], rettv);
9916 }
9917 }
9918 else
9919 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009920}
9921
9922/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009923 * "feedkeys()" function
9924 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009925 static void
9926f_feedkeys(argvars, rettv)
9927 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009928 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009929{
9930 int remap = TRUE;
9931 char_u *keys, *flags;
9932 char_u nbuf[NUMBUFLEN];
9933 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009934 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009935
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009936 /* This is not allowed in the sandbox. If the commands would still be
9937 * executed in the sandbox it would be OK, but it probably happens later,
9938 * when "sandbox" is no longer set. */
9939 if (check_secure())
9940 return;
9941
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009942 keys = get_tv_string(&argvars[0]);
9943 if (*keys != NUL)
9944 {
9945 if (argvars[1].v_type != VAR_UNKNOWN)
9946 {
9947 flags = get_tv_string_buf(&argvars[1], nbuf);
9948 for ( ; *flags != NUL; ++flags)
9949 {
9950 switch (*flags)
9951 {
9952 case 'n': remap = FALSE; break;
9953 case 'm': remap = TRUE; break;
9954 case 't': typed = TRUE; break;
9955 }
9956 }
9957 }
9958
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009959 /* Need to escape K_SPECIAL and CSI before putting the string in the
9960 * typeahead buffer. */
9961 keys_esc = vim_strsave_escape_csi(keys);
9962 if (keys_esc != NULL)
9963 {
9964 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009965 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009966 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009967 if (vgetc_busy)
9968 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009969 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009970 }
9971}
9972
9973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 * "filereadable()" function
9975 */
9976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009978 typval_T *argvars;
9979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980{
Bram Moolenaarc236c162008-07-13 17:41:49 +00009981 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 char_u *p;
9983 int n;
9984
Bram Moolenaarc236c162008-07-13 17:41:49 +00009985#ifndef O_NONBLOCK
9986# define O_NONBLOCK 0
9987#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009988 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +00009989 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
9990 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 {
9992 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009993 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 }
9995 else
9996 n = FALSE;
9997
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999}
10000
10001/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010002 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 * rights to write into.
10004 */
10005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010007 typval_T *argvars;
10008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010010 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011}
10012
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010013static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010014
10015 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010016findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010017 typval_T *argvars;
10018 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010019 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010020{
10021#ifdef FEAT_SEARCHPATH
10022 char_u *fname;
10023 char_u *fresult = NULL;
10024 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10025 char_u *p;
10026 char_u pathbuf[NUMBUFLEN];
10027 int count = 1;
10028 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010029 int error = FALSE;
10030#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010031
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010032 rettv->vval.v_string = NULL;
10033 rettv->v_type = VAR_STRING;
10034
10035#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010036 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010037
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010038 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010039 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010040 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10041 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010042 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010043 else
10044 {
10045 if (*p != NUL)
10046 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010048 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010049 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010050 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010051 }
10052
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010053 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10054 error = TRUE;
10055
10056 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010057 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010058 do
10059 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010060 if (rettv->v_type == VAR_STRING)
10061 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010062 fresult = find_file_in_path_option(first ? fname : NULL,
10063 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010064 0, first, path,
10065 find_what,
10066 curbuf->b_ffname,
10067 find_what == FINDFILE_DIR
10068 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010069 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010070
10071 if (fresult != NULL && rettv->v_type == VAR_LIST)
10072 list_append_string(rettv->vval.v_list, fresult, -1);
10073
10074 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010075 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010076
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010077 if (rettv->v_type == VAR_STRING)
10078 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010079#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010080}
10081
Bram Moolenaar33570922005-01-25 22:26:29 +000010082static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10083static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010084
10085/*
10086 * Implementation of map() and filter().
10087 */
10088 static void
10089filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010090 typval_T *argvars;
10091 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010092 int map;
10093{
10094 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010095 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010096 listitem_T *li, *nli;
10097 list_T *l = NULL;
10098 dictitem_T *di;
10099 hashtab_T *ht;
10100 hashitem_T *hi;
10101 dict_T *d = NULL;
10102 typval_T save_val;
10103 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010104 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010105 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010106 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010107 int save_did_emsg;
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010108 int index = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010109
Bram Moolenaare9a41262005-01-15 22:18:47 +000010110 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010111 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010112 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010113 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010114 return;
10115 }
10116 else if (argvars[0].v_type == VAR_DICT)
10117 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010118 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010119 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010120 return;
10121 }
10122 else
10123 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010124 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010125 return;
10126 }
10127
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010128 expr = get_tv_string_buf_chk(&argvars[1], buf);
10129 /* On type errors, the preceding call has already displayed an error
10130 * message. Avoid a misleading error message for an empty string that
10131 * was not passed as argument. */
10132 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010133 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010134 prepare_vimvar(VV_VAL, &save_val);
10135 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010136
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010137 /* We reset "did_emsg" to be able to detect whether an error
10138 * occurred during evaluation of the expression. */
10139 save_did_emsg = did_emsg;
10140 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010141
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010142 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010143 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010145 vimvars[VV_KEY].vv_type = VAR_STRING;
10146
10147 ht = &d->dv_hashtab;
10148 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010149 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010150 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010152 if (!HASHITEM_EMPTY(hi))
10153 {
10154 --todo;
10155 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010156 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010157 break;
10158 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010159 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010160 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010161 break;
10162 if (!map && rem)
10163 dictitem_remove(d, di);
10164 clear_tv(&vimvars[VV_KEY].vv_tv);
10165 }
10166 }
10167 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010168 }
10169 else
10170 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010171 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010173 for (li = l->lv_first; li != NULL; li = nli)
10174 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010175 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010176 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010177 nli = li->li_next;
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010178 vimvars[VV_KEY].vv_nr = index;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010179 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010180 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010181 break;
10182 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010183 listitem_remove(l, li);
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010184 ++index;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010185 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010186 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010187
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010188 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010189 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010190
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010191 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010192 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010193
10194 copy_tv(&argvars[0], rettv);
10195}
10196
10197 static int
10198filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010199 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010200 char_u *expr;
10201 int map;
10202 int *remp;
10203{
Bram Moolenaar33570922005-01-25 22:26:29 +000010204 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010205 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010206 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010207
Bram Moolenaar33570922005-01-25 22:26:29 +000010208 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010209 s = expr;
10210 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010211 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010212 if (*s != NUL) /* check for trailing chars after expr */
10213 {
10214 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010215 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010216 }
10217 if (map)
10218 {
10219 /* map(): replace the list item value */
10220 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010221 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010222 *tv = rettv;
10223 }
10224 else
10225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010226 int error = FALSE;
10227
Bram Moolenaare9a41262005-01-15 22:18:47 +000010228 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010229 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010230 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 /* On type error, nothing has been removed; return FAIL to stop the
10232 * loop. The error message was given by get_tv_number_chk(). */
10233 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010234 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010235 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010236 retval = OK;
10237theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010238 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010239 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010240}
10241
10242/*
10243 * "filter()" function
10244 */
10245 static void
10246f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010247 typval_T *argvars;
10248 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010249{
10250 filter_map(argvars, rettv, FALSE);
10251}
10252
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010253/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010254 * "finddir({fname}[, {path}[, {count}]])" function
10255 */
10256 static void
10257f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010258 typval_T *argvars;
10259 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010260{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010261 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010262}
10263
10264/*
10265 * "findfile({fname}[, {path}[, {count}]])" function
10266 */
10267 static void
10268f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010269 typval_T *argvars;
10270 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010271{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010272 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010273}
10274
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010275#ifdef FEAT_FLOAT
10276/*
10277 * "float2nr({float})" function
10278 */
10279 static void
10280f_float2nr(argvars, rettv)
10281 typval_T *argvars;
10282 typval_T *rettv;
10283{
10284 float_T f;
10285
10286 if (get_float_arg(argvars, &f) == OK)
10287 {
10288 if (f < -0x7fffffff)
10289 rettv->vval.v_number = -0x7fffffff;
10290 else if (f > 0x7fffffff)
10291 rettv->vval.v_number = 0x7fffffff;
10292 else
10293 rettv->vval.v_number = (varnumber_T)f;
10294 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010295}
10296
10297/*
10298 * "floor({float})" function
10299 */
10300 static void
10301f_floor(argvars, rettv)
10302 typval_T *argvars;
10303 typval_T *rettv;
10304{
10305 float_T f;
10306
10307 rettv->v_type = VAR_FLOAT;
10308 if (get_float_arg(argvars, &f) == OK)
10309 rettv->vval.v_float = floor(f);
10310 else
10311 rettv->vval.v_float = 0.0;
10312}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010313
10314/*
10315 * "fmod()" function
10316 */
10317 static void
10318f_fmod(argvars, rettv)
10319 typval_T *argvars;
10320 typval_T *rettv;
10321{
10322 float_T fx, fy;
10323
10324 rettv->v_type = VAR_FLOAT;
10325 if (get_float_arg(argvars, &fx) == OK
10326 && get_float_arg(&argvars[1], &fy) == OK)
10327 rettv->vval.v_float = fmod(fx, fy);
10328 else
10329 rettv->vval.v_float = 0.0;
10330}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010331#endif
10332
Bram Moolenaar0d660222005-01-07 21:51:51 +000010333/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010334 * "fnameescape({string})" function
10335 */
10336 static void
10337f_fnameescape(argvars, rettv)
10338 typval_T *argvars;
10339 typval_T *rettv;
10340{
10341 rettv->vval.v_string = vim_strsave_fnameescape(
10342 get_tv_string(&argvars[0]), FALSE);
10343 rettv->v_type = VAR_STRING;
10344}
10345
10346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 * "fnamemodify({fname}, {mods})" function
10348 */
10349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010350f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010351 typval_T *argvars;
10352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353{
10354 char_u *fname;
10355 char_u *mods;
10356 int usedlen = 0;
10357 int len;
10358 char_u *fbuf = NULL;
10359 char_u buf[NUMBUFLEN];
10360
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010361 fname = get_tv_string_chk(&argvars[0]);
10362 mods = get_tv_string_buf_chk(&argvars[1], buf);
10363 if (fname == NULL || mods == NULL)
10364 fname = NULL;
10365 else
10366 {
10367 len = (int)STRLEN(fname);
10368 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010371 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010373 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010375 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 vim_free(fbuf);
10377}
10378
Bram Moolenaar33570922005-01-25 22:26:29 +000010379static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380
10381/*
10382 * "foldclosed()" function
10383 */
10384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010385foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010386 typval_T *argvars;
10387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 int end;
10389{
10390#ifdef FEAT_FOLDING
10391 linenr_T lnum;
10392 linenr_T first, last;
10393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010394 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10396 {
10397 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10398 {
10399 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010400 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010402 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 return;
10404 }
10405 }
10406#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010407 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408}
10409
10410/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010411 * "foldclosed()" function
10412 */
10413 static void
10414f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010415 typval_T *argvars;
10416 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010417{
10418 foldclosed_both(argvars, rettv, FALSE);
10419}
10420
10421/*
10422 * "foldclosedend()" function
10423 */
10424 static void
10425f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010426 typval_T *argvars;
10427 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010428{
10429 foldclosed_both(argvars, rettv, TRUE);
10430}
10431
10432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 * "foldlevel()" function
10434 */
10435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010436f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010437 typval_T *argvars;
10438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439{
10440#ifdef FEAT_FOLDING
10441 linenr_T lnum;
10442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010443 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010445 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447}
10448
10449/*
10450 * "foldtext()" function
10451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010453f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010454 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456{
10457#ifdef FEAT_FOLDING
10458 linenr_T lnum;
10459 char_u *s;
10460 char_u *r;
10461 int len;
10462 char *txt;
10463#endif
10464
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010465 rettv->v_type = VAR_STRING;
10466 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010468 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10469 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10470 <= curbuf->b_ml.ml_line_count
10471 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 {
10473 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010474 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10475 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476 {
10477 if (!linewhite(lnum))
10478 break;
10479 ++lnum;
10480 }
10481
10482 /* Find interesting text in this line. */
10483 s = skipwhite(ml_get(lnum));
10484 /* skip C comment-start */
10485 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010486 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010488 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010489 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010490 {
10491 s = skipwhite(ml_get(lnum + 1));
10492 if (*s == '*')
10493 s = skipwhite(s + 1);
10494 }
10495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 txt = _("+-%s%3ld lines: ");
10497 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010498 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 + 20 /* for %3ld */
10500 + STRLEN(s))); /* concatenated */
10501 if (r != NULL)
10502 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010503 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10504 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10505 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506 len = (int)STRLEN(r);
10507 STRCAT(r, s);
10508 /* remove 'foldmarker' and 'commentstring' */
10509 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010510 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 }
10512 }
10513#endif
10514}
10515
10516/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010517 * "foldtextresult(lnum)" function
10518 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010520f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010521 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010522 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010523{
10524#ifdef FEAT_FOLDING
10525 linenr_T lnum;
10526 char_u *text;
10527 char_u buf[51];
10528 foldinfo_T foldinfo;
10529 int fold_count;
10530#endif
10531
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010532 rettv->v_type = VAR_STRING;
10533 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010534#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010535 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 /* treat illegal types and illegal string values for {lnum} the same */
10537 if (lnum < 0)
10538 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010539 fold_count = foldedCount(curwin, lnum, &foldinfo);
10540 if (fold_count > 0)
10541 {
10542 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10543 &foldinfo, buf);
10544 if (text == buf)
10545 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010547 }
10548#endif
10549}
10550
10551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 * "foreground()" function
10553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010556 typval_T *argvars UNUSED;
10557 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559#ifdef FEAT_GUI
10560 if (gui.in_use)
10561 gui_mch_set_foreground();
10562#else
10563# ifdef WIN32
10564 win32_set_foreground();
10565# endif
10566#endif
10567}
10568
10569/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010570 * "function()" function
10571 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010573f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010574 typval_T *argvars;
10575 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010576{
10577 char_u *s;
10578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010579 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010580 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010581 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010582 /* Don't check an autoload name for existence here. */
10583 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010584 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010585 else
10586 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010587 rettv->vval.v_string = vim_strsave(s);
10588 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010589 }
10590}
10591
10592/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010593 * "garbagecollect()" function
10594 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010595 static void
10596f_garbagecollect(argvars, rettv)
10597 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010598 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010599{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010600 /* This is postponed until we are back at the toplevel, because we may be
10601 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10602 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010603
10604 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10605 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010606}
10607
10608/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010609 * "get()" function
10610 */
10611 static void
10612f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010613 typval_T *argvars;
10614 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010615{
Bram Moolenaar33570922005-01-25 22:26:29 +000010616 listitem_T *li;
10617 list_T *l;
10618 dictitem_T *di;
10619 dict_T *d;
10620 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010621
Bram Moolenaare9a41262005-01-15 22:18:47 +000010622 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010623 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010624 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010626 int error = FALSE;
10627
10628 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10629 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010630 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010631 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010632 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010633 else if (argvars[0].v_type == VAR_DICT)
10634 {
10635 if ((d = argvars[0].vval.v_dict) != NULL)
10636 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010637 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010638 if (di != NULL)
10639 tv = &di->di_tv;
10640 }
10641 }
10642 else
10643 EMSG2(_(e_listdictarg), "get()");
10644
10645 if (tv == NULL)
10646 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010647 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010648 copy_tv(&argvars[2], rettv);
10649 }
10650 else
10651 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010652}
10653
Bram Moolenaar342337a2005-07-21 21:11:17 +000010654static 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 +000010655
10656/*
10657 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010658 * Return a range (from start to end) of lines in rettv from the specified
10659 * buffer.
10660 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010661 */
10662 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010663get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010664 buf_T *buf;
10665 linenr_T start;
10666 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010667 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010668 typval_T *rettv;
10669{
10670 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010671
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010672 if (retlist && rettv_list_alloc(rettv) == FAIL)
10673 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010674
10675 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10676 return;
10677
10678 if (!retlist)
10679 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010680 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10681 p = ml_get_buf(buf, start, FALSE);
10682 else
10683 p = (char_u *)"";
10684
10685 rettv->v_type = VAR_STRING;
10686 rettv->vval.v_string = vim_strsave(p);
10687 }
10688 else
10689 {
10690 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010691 return;
10692
10693 if (start < 1)
10694 start = 1;
10695 if (end > buf->b_ml.ml_line_count)
10696 end = buf->b_ml.ml_line_count;
10697 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010698 if (list_append_string(rettv->vval.v_list,
10699 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010700 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010701 }
10702}
10703
10704/*
10705 * "getbufline()" function
10706 */
10707 static void
10708f_getbufline(argvars, rettv)
10709 typval_T *argvars;
10710 typval_T *rettv;
10711{
10712 linenr_T lnum;
10713 linenr_T end;
10714 buf_T *buf;
10715
10716 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10717 ++emsg_off;
10718 buf = get_buf_tv(&argvars[0]);
10719 --emsg_off;
10720
Bram Moolenaar661b1822005-07-28 22:36:45 +000010721 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010722 if (argvars[2].v_type == VAR_UNKNOWN)
10723 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010724 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010725 end = get_tv_lnum_buf(&argvars[2], buf);
10726
Bram Moolenaar342337a2005-07-21 21:11:17 +000010727 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010728}
10729
Bram Moolenaar0d660222005-01-07 21:51:51 +000010730/*
10731 * "getbufvar()" function
10732 */
10733 static void
10734f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010735 typval_T *argvars;
10736 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010737{
10738 buf_T *buf;
10739 buf_T *save_curbuf;
10740 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010741 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010743 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10744 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010745 ++emsg_off;
10746 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010747
10748 rettv->v_type = VAR_STRING;
10749 rettv->vval.v_string = NULL;
10750
10751 if (buf != NULL && varname != NULL)
10752 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010753 /* set curbuf to be our buf, temporarily */
10754 save_curbuf = curbuf;
10755 curbuf = buf;
10756
Bram Moolenaar0d660222005-01-07 21:51:51 +000010757 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010758 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010759 else
10760 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010761 if (*varname == NUL)
10762 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10763 * scope prefix before the NUL byte is required by
10764 * find_var_in_ht(). */
10765 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010766 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010767 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010768 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010769 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010770 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010771
10772 /* restore previous notion of curbuf */
10773 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010774 }
10775
10776 --emsg_off;
10777}
10778
10779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 * "getchar()" function
10781 */
10782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010783f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010784 typval_T *argvars;
10785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786{
10787 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010788 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010790 /* Position the cursor. Needed after a message that ends in a space. */
10791 windgoto(msg_row, msg_col);
10792
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 ++no_mapping;
10794 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010795 for (;;)
10796 {
10797 if (argvars[0].v_type == VAR_UNKNOWN)
10798 /* getchar(): blocking wait. */
10799 n = safe_vgetc();
10800 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10801 /* getchar(1): only check if char avail */
10802 n = vpeekc();
10803 else if (error || vpeekc() == NUL)
10804 /* illegal argument or getchar(0) and no char avail: return zero */
10805 n = 0;
10806 else
10807 /* getchar(0) and char avail: return char */
10808 n = safe_vgetc();
10809 if (n == K_IGNORE)
10810 continue;
10811 break;
10812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813 --no_mapping;
10814 --allow_keys;
10815
Bram Moolenaar219b8702006-11-01 14:32:36 +000010816 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10817 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10818 vimvars[VV_MOUSE_COL].vv_nr = 0;
10819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010820 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821 if (IS_SPECIAL(n) || mod_mask != 0)
10822 {
10823 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10824 int i = 0;
10825
10826 /* Turn a special key into three bytes, plus modifier. */
10827 if (mod_mask != 0)
10828 {
10829 temp[i++] = K_SPECIAL;
10830 temp[i++] = KS_MODIFIER;
10831 temp[i++] = mod_mask;
10832 }
10833 if (IS_SPECIAL(n))
10834 {
10835 temp[i++] = K_SPECIAL;
10836 temp[i++] = K_SECOND(n);
10837 temp[i++] = K_THIRD(n);
10838 }
10839#ifdef FEAT_MBYTE
10840 else if (has_mbyte)
10841 i += (*mb_char2bytes)(n, temp + i);
10842#endif
10843 else
10844 temp[i++] = n;
10845 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010846 rettv->v_type = VAR_STRING;
10847 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010848
10849#ifdef FEAT_MOUSE
10850 if (n == K_LEFTMOUSE
10851 || n == K_LEFTMOUSE_NM
10852 || n == K_LEFTDRAG
10853 || n == K_LEFTRELEASE
10854 || n == K_LEFTRELEASE_NM
10855 || n == K_MIDDLEMOUSE
10856 || n == K_MIDDLEDRAG
10857 || n == K_MIDDLERELEASE
10858 || n == K_RIGHTMOUSE
10859 || n == K_RIGHTDRAG
10860 || n == K_RIGHTRELEASE
10861 || n == K_X1MOUSE
10862 || n == K_X1DRAG
10863 || n == K_X1RELEASE
10864 || n == K_X2MOUSE
10865 || n == K_X2DRAG
10866 || n == K_X2RELEASE
10867 || n == K_MOUSEDOWN
10868 || n == K_MOUSEUP)
10869 {
10870 int row = mouse_row;
10871 int col = mouse_col;
10872 win_T *win;
10873 linenr_T lnum;
10874# ifdef FEAT_WINDOWS
10875 win_T *wp;
10876# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010877 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010878
10879 if (row >= 0 && col >= 0)
10880 {
10881 /* Find the window at the mouse coordinates and compute the
10882 * text position. */
10883 win = mouse_find_win(&row, &col);
10884 (void)mouse_comp_pos(win, &row, &col, &lnum);
10885# ifdef FEAT_WINDOWS
10886 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010887 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010888# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010889 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010890 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10891 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10892 }
10893 }
10894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 }
10896}
10897
10898/*
10899 * "getcharmod()" function
10900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010902f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010903 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010906 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907}
10908
10909/*
10910 * "getcmdline()" function
10911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010913f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010914 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917 rettv->v_type = VAR_STRING;
10918 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919}
10920
10921/*
10922 * "getcmdpos()" function
10923 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010925f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010926 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010929 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930}
10931
10932/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010933 * "getcmdtype()" function
10934 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010935 static void
10936f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010937 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010938 typval_T *rettv;
10939{
10940 rettv->v_type = VAR_STRING;
10941 rettv->vval.v_string = alloc(2);
10942 if (rettv->vval.v_string != NULL)
10943 {
10944 rettv->vval.v_string[0] = get_cmdline_type();
10945 rettv->vval.v_string[1] = NUL;
10946 }
10947}
10948
10949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 * "getcwd()" function
10951 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010953f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010954 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956{
10957 char_u cwd[MAXPATHL];
10958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010959 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010961 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 else
10963 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010966 if (rettv->vval.v_string != NULL)
10967 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968#endif
10969 }
10970}
10971
10972/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010973 * "getfontname()" function
10974 */
10975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010976f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010977 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010978 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010979{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010980 rettv->v_type = VAR_STRING;
10981 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010982#ifdef FEAT_GUI
10983 if (gui.in_use)
10984 {
10985 GuiFont font;
10986 char_u *name = NULL;
10987
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010988 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010989 {
10990 /* Get the "Normal" font. Either the name saved by
10991 * hl_set_font_name() or from the font ID. */
10992 font = gui.norm_font;
10993 name = hl_get_font_name();
10994 }
10995 else
10996 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010997 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010998 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10999 return;
11000 font = gui_mch_get_font(name, FALSE);
11001 if (font == NOFONT)
11002 return; /* Invalid font name, return empty string. */
11003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011005 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011006 gui_mch_free_font(font);
11007 }
11008#endif
11009}
11010
11011/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011012 * "getfperm({fname})" function
11013 */
11014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011015f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011016 typval_T *argvars;
11017 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011018{
11019 char_u *fname;
11020 struct stat st;
11021 char_u *perm = NULL;
11022 char_u flags[] = "rwx";
11023 int i;
11024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011027 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011028 if (mch_stat((char *)fname, &st) >= 0)
11029 {
11030 perm = vim_strsave((char_u *)"---------");
11031 if (perm != NULL)
11032 {
11033 for (i = 0; i < 9; i++)
11034 {
11035 if (st.st_mode & (1 << (8 - i)))
11036 perm[i] = flags[i % 3];
11037 }
11038 }
11039 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011041}
11042
11043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 * "getfsize({fname})" function
11045 */
11046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011047f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011048 typval_T *argvars;
11049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050{
11051 char_u *fname;
11052 struct stat st;
11053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011056 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057
11058 if (mch_stat((char *)fname, &st) >= 0)
11059 {
11060 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011061 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011063 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011064 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011065
11066 /* non-perfect check for overflow */
11067 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11068 rettv->vval.v_number = -2;
11069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 }
11071 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011072 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073}
11074
11075/*
11076 * "getftime({fname})" function
11077 */
11078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011079f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011080 typval_T *argvars;
11081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082{
11083 char_u *fname;
11084 struct stat st;
11085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011086 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087
11088 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011089 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011091 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092}
11093
11094/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011095 * "getftype({fname})" function
11096 */
11097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011098f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011099 typval_T *argvars;
11100 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011101{
11102 char_u *fname;
11103 struct stat st;
11104 char_u *type = NULL;
11105 char *t;
11106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011107 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011110 if (mch_lstat((char *)fname, &st) >= 0)
11111 {
11112#ifdef S_ISREG
11113 if (S_ISREG(st.st_mode))
11114 t = "file";
11115 else if (S_ISDIR(st.st_mode))
11116 t = "dir";
11117# ifdef S_ISLNK
11118 else if (S_ISLNK(st.st_mode))
11119 t = "link";
11120# endif
11121# ifdef S_ISBLK
11122 else if (S_ISBLK(st.st_mode))
11123 t = "bdev";
11124# endif
11125# ifdef S_ISCHR
11126 else if (S_ISCHR(st.st_mode))
11127 t = "cdev";
11128# endif
11129# ifdef S_ISFIFO
11130 else if (S_ISFIFO(st.st_mode))
11131 t = "fifo";
11132# endif
11133# ifdef S_ISSOCK
11134 else if (S_ISSOCK(st.st_mode))
11135 t = "fifo";
11136# endif
11137 else
11138 t = "other";
11139#else
11140# ifdef S_IFMT
11141 switch (st.st_mode & S_IFMT)
11142 {
11143 case S_IFREG: t = "file"; break;
11144 case S_IFDIR: t = "dir"; break;
11145# ifdef S_IFLNK
11146 case S_IFLNK: t = "link"; break;
11147# endif
11148# ifdef S_IFBLK
11149 case S_IFBLK: t = "bdev"; break;
11150# endif
11151# ifdef S_IFCHR
11152 case S_IFCHR: t = "cdev"; break;
11153# endif
11154# ifdef S_IFIFO
11155 case S_IFIFO: t = "fifo"; break;
11156# endif
11157# ifdef S_IFSOCK
11158 case S_IFSOCK: t = "socket"; break;
11159# endif
11160 default: t = "other";
11161 }
11162# else
11163 if (mch_isdir(fname))
11164 t = "dir";
11165 else
11166 t = "file";
11167# endif
11168#endif
11169 type = vim_strsave((char_u *)t);
11170 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011172}
11173
11174/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011175 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011176 */
11177 static void
11178f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011179 typval_T *argvars;
11180 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011181{
11182 linenr_T lnum;
11183 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011184 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011185
11186 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011187 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011188 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011189 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011190 retlist = FALSE;
11191 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011192 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011193 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011194 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011195 retlist = TRUE;
11196 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011197
Bram Moolenaar342337a2005-07-21 21:11:17 +000011198 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011199}
11200
11201/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011202 * "getmatches()" function
11203 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011204 static void
11205f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011206 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011207 typval_T *rettv;
11208{
11209#ifdef FEAT_SEARCH_EXTRA
11210 dict_T *dict;
11211 matchitem_T *cur = curwin->w_match_head;
11212
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011213 if (rettv_list_alloc(rettv) == OK)
11214 {
11215 while (cur != NULL)
11216 {
11217 dict = dict_alloc();
11218 if (dict == NULL)
11219 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011220 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11221 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11222 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11223 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11224 list_append_dict(rettv->vval.v_list, dict);
11225 cur = cur->next;
11226 }
11227 }
11228#endif
11229}
11230
11231/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011232 * "getpid()" function
11233 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011234 static void
11235f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011236 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011237 typval_T *rettv;
11238{
11239 rettv->vval.v_number = mch_get_pid();
11240}
11241
11242/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011243 * "getpos(string)" function
11244 */
11245 static void
11246f_getpos(argvars, rettv)
11247 typval_T *argvars;
11248 typval_T *rettv;
11249{
11250 pos_T *fp;
11251 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011252 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011253
11254 if (rettv_list_alloc(rettv) == OK)
11255 {
11256 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011257 fp = var2fpos(&argvars[0], TRUE, &fnum);
11258 if (fnum != -1)
11259 list_append_number(l, (varnumber_T)fnum);
11260 else
11261 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011262 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11263 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011264 list_append_number(l, (fp != NULL)
11265 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011266 : (varnumber_T)0);
11267 list_append_number(l,
11268#ifdef FEAT_VIRTUALEDIT
11269 (fp != NULL) ? (varnumber_T)fp->coladd :
11270#endif
11271 (varnumber_T)0);
11272 }
11273 else
11274 rettv->vval.v_number = FALSE;
11275}
11276
11277/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011278 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011279 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011280 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011281f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011282 typval_T *argvars UNUSED;
11283 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011284{
11285#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011286 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011287#endif
11288
Bram Moolenaar2641f772005-03-25 21:58:17 +000011289#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011290 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011291 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011292 wp = NULL;
11293 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11294 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011295 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011296 if (wp == NULL)
11297 return;
11298 }
11299
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011300 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011301 }
11302#endif
11303}
11304
11305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306 * "getreg()" function
11307 */
11308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011310 typval_T *argvars;
11311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312{
11313 char_u *strregname;
11314 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011315 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011316 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011318 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011320 strregname = get_tv_string_chk(&argvars[0]);
11321 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011322 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011323 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011326 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 regname = (strregname == NULL ? '"' : *strregname);
11328 if (regname == 0)
11329 regname = '"';
11330
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011331 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011332 rettv->vval.v_string = error ? NULL :
11333 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334}
11335
11336/*
11337 * "getregtype()" function
11338 */
11339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011340f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011341 typval_T *argvars;
11342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343{
11344 char_u *strregname;
11345 int regname;
11346 char_u buf[NUMBUFLEN + 2];
11347 long reglen = 0;
11348
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011349 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011350 {
11351 strregname = get_tv_string_chk(&argvars[0]);
11352 if (strregname == NULL) /* type error; errmsg already given */
11353 {
11354 rettv->v_type = VAR_STRING;
11355 rettv->vval.v_string = NULL;
11356 return;
11357 }
11358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359 else
11360 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011361 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362
11363 regname = (strregname == NULL ? '"' : *strregname);
11364 if (regname == 0)
11365 regname = '"';
11366
11367 buf[0] = NUL;
11368 buf[1] = NUL;
11369 switch (get_reg_type(regname, &reglen))
11370 {
11371 case MLINE: buf[0] = 'V'; break;
11372 case MCHAR: buf[0] = 'v'; break;
11373#ifdef FEAT_VISUAL
11374 case MBLOCK:
11375 buf[0] = Ctrl_V;
11376 sprintf((char *)buf + 1, "%ld", reglen + 1);
11377 break;
11378#endif
11379 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011380 rettv->v_type = VAR_STRING;
11381 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382}
11383
11384/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011385 * "gettabvar()" function
11386 */
11387 static void
11388f_gettabvar(argvars, rettv)
11389 typval_T *argvars;
11390 typval_T *rettv;
11391{
11392 tabpage_T *tp;
11393 dictitem_T *v;
11394 char_u *varname;
11395
11396 rettv->v_type = VAR_STRING;
11397 rettv->vval.v_string = NULL;
11398
11399 varname = get_tv_string_chk(&argvars[1]);
11400 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11401 if (tp != NULL && varname != NULL)
11402 {
11403 /* look up the variable */
11404 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11405 if (v != NULL)
11406 copy_tv(&v->di_tv, rettv);
11407 }
11408}
11409
11410/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011411 * "gettabwinvar()" function
11412 */
11413 static void
11414f_gettabwinvar(argvars, rettv)
11415 typval_T *argvars;
11416 typval_T *rettv;
11417{
11418 getwinvar(argvars, rettv, 1);
11419}
11420
11421/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 * "getwinposx()" function
11423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011425f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011426 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011429 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430#ifdef FEAT_GUI
11431 if (gui.in_use)
11432 {
11433 int x, y;
11434
11435 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437 }
11438#endif
11439}
11440
11441/*
11442 * "getwinposy()" function
11443 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011446 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450#ifdef FEAT_GUI
11451 if (gui.in_use)
11452 {
11453 int x, y;
11454
11455 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011456 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457 }
11458#endif
11459}
11460
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011461/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011462 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011463 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011464 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011465find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011466 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011467 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011468{
11469#ifdef FEAT_WINDOWS
11470 win_T *wp;
11471#endif
11472 int nr;
11473
11474 nr = get_tv_number_chk(vp, NULL);
11475
11476#ifdef FEAT_WINDOWS
11477 if (nr < 0)
11478 return NULL;
11479 if (nr == 0)
11480 return curwin;
11481
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011482 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11483 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011484 if (--nr <= 0)
11485 break;
11486 return wp;
11487#else
11488 if (nr == 0 || nr == 1)
11489 return curwin;
11490 return NULL;
11491#endif
11492}
11493
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494/*
11495 * "getwinvar()" function
11496 */
11497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011499 typval_T *argvars;
11500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011502 getwinvar(argvars, rettv, 0);
11503}
11504
11505/*
11506 * getwinvar() and gettabwinvar()
11507 */
11508 static void
11509getwinvar(argvars, rettv, off)
11510 typval_T *argvars;
11511 typval_T *rettv;
11512 int off; /* 1 for gettabwinvar() */
11513{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514 win_T *win, *oldcurwin;
11515 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011516 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011517 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011519#ifdef FEAT_WINDOWS
11520 if (off == 1)
11521 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11522 else
11523 tp = curtab;
11524#endif
11525 win = find_win_by_nr(&argvars[off], tp);
11526 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011527 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011529 rettv->v_type = VAR_STRING;
11530 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531
11532 if (win != NULL && varname != NULL)
11533 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011534 /* Set curwin to be our win, temporarily. Also set curbuf, so
11535 * that we can get buffer-local options. */
11536 oldcurwin = curwin;
11537 curwin = win;
11538 curbuf = win->w_buffer;
11539
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011541 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542 else
11543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011544 if (*varname == NUL)
11545 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11546 * scope prefix before the NUL byte is required by
11547 * find_var_in_ht(). */
11548 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011550 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011552 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011554
11555 /* restore previous notion of curwin */
11556 curwin = oldcurwin;
11557 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558 }
11559
11560 --emsg_off;
11561}
11562
11563/*
11564 * "glob()" function
11565 */
11566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011567f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011568 typval_T *argvars;
11569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011571 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011573 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011575 /* When the optional second argument is non-zero, don't remove matches
11576 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11577 if (argvars[1].v_type != VAR_UNKNOWN
11578 && get_tv_number_chk(&argvars[1], &error))
11579 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011580 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011581 if (!error)
11582 {
11583 ExpandInit(&xpc);
11584 xpc.xp_context = EXPAND_FILES;
11585 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11586 NULL, flags, WILD_ALL);
11587 }
11588 else
11589 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590}
11591
11592/*
11593 * "globpath()" function
11594 */
11595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011597 typval_T *argvars;
11598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011600 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011602 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011603 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011605 /* When the optional second argument is non-zero, don't remove matches
11606 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11607 if (argvars[2].v_type != VAR_UNKNOWN
11608 && get_tv_number_chk(&argvars[2], &error))
11609 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011610 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011611 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011612 rettv->vval.v_string = NULL;
11613 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011614 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11615 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616}
11617
11618/*
11619 * "has()" function
11620 */
11621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011623 typval_T *argvars;
11624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625{
11626 int i;
11627 char_u *name;
11628 int n = FALSE;
11629 static char *(has_list[]) =
11630 {
11631#ifdef AMIGA
11632 "amiga",
11633# ifdef FEAT_ARP
11634 "arp",
11635# endif
11636#endif
11637#ifdef __BEOS__
11638 "beos",
11639#endif
11640#ifdef MSDOS
11641# ifdef DJGPP
11642 "dos32",
11643# else
11644 "dos16",
11645# endif
11646#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011647#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648 "mac",
11649#endif
11650#if defined(MACOS_X_UNIX)
11651 "macunix",
11652#endif
11653#ifdef OS2
11654 "os2",
11655#endif
11656#ifdef __QNX__
11657 "qnx",
11658#endif
11659#ifdef RISCOS
11660 "riscos",
11661#endif
11662#ifdef UNIX
11663 "unix",
11664#endif
11665#ifdef VMS
11666 "vms",
11667#endif
11668#ifdef WIN16
11669 "win16",
11670#endif
11671#ifdef WIN32
11672 "win32",
11673#endif
11674#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11675 "win32unix",
11676#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011677#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678 "win64",
11679#endif
11680#ifdef EBCDIC
11681 "ebcdic",
11682#endif
11683#ifndef CASE_INSENSITIVE_FILENAME
11684 "fname_case",
11685#endif
11686#ifdef FEAT_ARABIC
11687 "arabic",
11688#endif
11689#ifdef FEAT_AUTOCMD
11690 "autocmd",
11691#endif
11692#ifdef FEAT_BEVAL
11693 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011694# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11695 "balloon_multiline",
11696# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697#endif
11698#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11699 "builtin_terms",
11700# ifdef ALL_BUILTIN_TCAPS
11701 "all_builtin_terms",
11702# endif
11703#endif
11704#ifdef FEAT_BYTEOFF
11705 "byte_offset",
11706#endif
11707#ifdef FEAT_CINDENT
11708 "cindent",
11709#endif
11710#ifdef FEAT_CLIENTSERVER
11711 "clientserver",
11712#endif
11713#ifdef FEAT_CLIPBOARD
11714 "clipboard",
11715#endif
11716#ifdef FEAT_CMDL_COMPL
11717 "cmdline_compl",
11718#endif
11719#ifdef FEAT_CMDHIST
11720 "cmdline_hist",
11721#endif
11722#ifdef FEAT_COMMENTS
11723 "comments",
11724#endif
11725#ifdef FEAT_CRYPT
11726 "cryptv",
11727#endif
11728#ifdef FEAT_CSCOPE
11729 "cscope",
11730#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011731#ifdef CURSOR_SHAPE
11732 "cursorshape",
11733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734#ifdef DEBUG
11735 "debug",
11736#endif
11737#ifdef FEAT_CON_DIALOG
11738 "dialog_con",
11739#endif
11740#ifdef FEAT_GUI_DIALOG
11741 "dialog_gui",
11742#endif
11743#ifdef FEAT_DIFF
11744 "diff",
11745#endif
11746#ifdef FEAT_DIGRAPHS
11747 "digraphs",
11748#endif
11749#ifdef FEAT_DND
11750 "dnd",
11751#endif
11752#ifdef FEAT_EMACS_TAGS
11753 "emacs_tags",
11754#endif
11755 "eval", /* always present, of course! */
11756#ifdef FEAT_EX_EXTRA
11757 "ex_extra",
11758#endif
11759#ifdef FEAT_SEARCH_EXTRA
11760 "extra_search",
11761#endif
11762#ifdef FEAT_FKMAP
11763 "farsi",
11764#endif
11765#ifdef FEAT_SEARCHPATH
11766 "file_in_path",
11767#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011768#if defined(UNIX) && !defined(USE_SYSTEM)
11769 "filterpipe",
11770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771#ifdef FEAT_FIND_ID
11772 "find_in_path",
11773#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011774#ifdef FEAT_FLOAT
11775 "float",
11776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011777#ifdef FEAT_FOLDING
11778 "folding",
11779#endif
11780#ifdef FEAT_FOOTER
11781 "footer",
11782#endif
11783#if !defined(USE_SYSTEM) && defined(UNIX)
11784 "fork",
11785#endif
11786#ifdef FEAT_GETTEXT
11787 "gettext",
11788#endif
11789#ifdef FEAT_GUI
11790 "gui",
11791#endif
11792#ifdef FEAT_GUI_ATHENA
11793# ifdef FEAT_GUI_NEXTAW
11794 "gui_neXtaw",
11795# else
11796 "gui_athena",
11797# endif
11798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799#ifdef FEAT_GUI_GTK
11800 "gui_gtk",
11801# ifdef HAVE_GTK2
11802 "gui_gtk2",
11803# endif
11804#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011805#ifdef FEAT_GUI_GNOME
11806 "gui_gnome",
11807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808#ifdef FEAT_GUI_MAC
11809 "gui_mac",
11810#endif
11811#ifdef FEAT_GUI_MOTIF
11812 "gui_motif",
11813#endif
11814#ifdef FEAT_GUI_PHOTON
11815 "gui_photon",
11816#endif
11817#ifdef FEAT_GUI_W16
11818 "gui_win16",
11819#endif
11820#ifdef FEAT_GUI_W32
11821 "gui_win32",
11822#endif
11823#ifdef FEAT_HANGULIN
11824 "hangul_input",
11825#endif
11826#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11827 "iconv",
11828#endif
11829#ifdef FEAT_INS_EXPAND
11830 "insert_expand",
11831#endif
11832#ifdef FEAT_JUMPLIST
11833 "jumplist",
11834#endif
11835#ifdef FEAT_KEYMAP
11836 "keymap",
11837#endif
11838#ifdef FEAT_LANGMAP
11839 "langmap",
11840#endif
11841#ifdef FEAT_LIBCALL
11842 "libcall",
11843#endif
11844#ifdef FEAT_LINEBREAK
11845 "linebreak",
11846#endif
11847#ifdef FEAT_LISP
11848 "lispindent",
11849#endif
11850#ifdef FEAT_LISTCMDS
11851 "listcmds",
11852#endif
11853#ifdef FEAT_LOCALMAP
11854 "localmap",
11855#endif
11856#ifdef FEAT_MENU
11857 "menu",
11858#endif
11859#ifdef FEAT_SESSION
11860 "mksession",
11861#endif
11862#ifdef FEAT_MODIFY_FNAME
11863 "modify_fname",
11864#endif
11865#ifdef FEAT_MOUSE
11866 "mouse",
11867#endif
11868#ifdef FEAT_MOUSESHAPE
11869 "mouseshape",
11870#endif
11871#if defined(UNIX) || defined(VMS)
11872# ifdef FEAT_MOUSE_DEC
11873 "mouse_dec",
11874# endif
11875# ifdef FEAT_MOUSE_GPM
11876 "mouse_gpm",
11877# endif
11878# ifdef FEAT_MOUSE_JSB
11879 "mouse_jsbterm",
11880# endif
11881# ifdef FEAT_MOUSE_NET
11882 "mouse_netterm",
11883# endif
11884# ifdef FEAT_MOUSE_PTERM
11885 "mouse_pterm",
11886# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011887# ifdef FEAT_SYSMOUSE
11888 "mouse_sysmouse",
11889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890# ifdef FEAT_MOUSE_XTERM
11891 "mouse_xterm",
11892# endif
11893#endif
11894#ifdef FEAT_MBYTE
11895 "multi_byte",
11896#endif
11897#ifdef FEAT_MBYTE_IME
11898 "multi_byte_ime",
11899#endif
11900#ifdef FEAT_MULTI_LANG
11901 "multi_lang",
11902#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011903#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011904#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011905 "mzscheme",
11906#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908#ifdef FEAT_OLE
11909 "ole",
11910#endif
11911#ifdef FEAT_OSFILETYPE
11912 "osfiletype",
11913#endif
11914#ifdef FEAT_PATH_EXTRA
11915 "path_extra",
11916#endif
11917#ifdef FEAT_PERL
11918#ifndef DYNAMIC_PERL
11919 "perl",
11920#endif
11921#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020011922#ifdef FEAT_PERSISTENT_UNDO
11923 "persistent_undo",
11924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925#ifdef FEAT_PYTHON
11926#ifndef DYNAMIC_PYTHON
11927 "python",
11928#endif
11929#endif
11930#ifdef FEAT_POSTSCRIPT
11931 "postscript",
11932#endif
11933#ifdef FEAT_PRINTER
11934 "printer",
11935#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011936#ifdef FEAT_PROFILE
11937 "profile",
11938#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011939#ifdef FEAT_RELTIME
11940 "reltime",
11941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942#ifdef FEAT_QUICKFIX
11943 "quickfix",
11944#endif
11945#ifdef FEAT_RIGHTLEFT
11946 "rightleft",
11947#endif
11948#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11949 "ruby",
11950#endif
11951#ifdef FEAT_SCROLLBIND
11952 "scrollbind",
11953#endif
11954#ifdef FEAT_CMDL_INFO
11955 "showcmd",
11956 "cmdline_info",
11957#endif
11958#ifdef FEAT_SIGNS
11959 "signs",
11960#endif
11961#ifdef FEAT_SMARTINDENT
11962 "smartindent",
11963#endif
11964#ifdef FEAT_SNIFF
11965 "sniff",
11966#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000011967#ifdef STARTUPTIME
11968 "startuptime",
11969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970#ifdef FEAT_STL_OPT
11971 "statusline",
11972#endif
11973#ifdef FEAT_SUN_WORKSHOP
11974 "sun_workshop",
11975#endif
11976#ifdef FEAT_NETBEANS_INTG
11977 "netbeans_intg",
11978#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011979#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011980 "spell",
11981#endif
11982#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983 "syntax",
11984#endif
11985#if defined(USE_SYSTEM) || !defined(UNIX)
11986 "system",
11987#endif
11988#ifdef FEAT_TAG_BINS
11989 "tag_binary",
11990#endif
11991#ifdef FEAT_TAG_OLDSTATIC
11992 "tag_old_static",
11993#endif
11994#ifdef FEAT_TAG_ANYWHITE
11995 "tag_any_white",
11996#endif
11997#ifdef FEAT_TCL
11998# ifndef DYNAMIC_TCL
11999 "tcl",
12000# endif
12001#endif
12002#ifdef TERMINFO
12003 "terminfo",
12004#endif
12005#ifdef FEAT_TERMRESPONSE
12006 "termresponse",
12007#endif
12008#ifdef FEAT_TEXTOBJ
12009 "textobjects",
12010#endif
12011#ifdef HAVE_TGETENT
12012 "tgetent",
12013#endif
12014#ifdef FEAT_TITLE
12015 "title",
12016#endif
12017#ifdef FEAT_TOOLBAR
12018 "toolbar",
12019#endif
12020#ifdef FEAT_USR_CMDS
12021 "user-commands", /* was accidentally included in 5.4 */
12022 "user_commands",
12023#endif
12024#ifdef FEAT_VIMINFO
12025 "viminfo",
12026#endif
12027#ifdef FEAT_VERTSPLIT
12028 "vertsplit",
12029#endif
12030#ifdef FEAT_VIRTUALEDIT
12031 "virtualedit",
12032#endif
12033#ifdef FEAT_VISUAL
12034 "visual",
12035#endif
12036#ifdef FEAT_VISUALEXTRA
12037 "visualextra",
12038#endif
12039#ifdef FEAT_VREPLACE
12040 "vreplace",
12041#endif
12042#ifdef FEAT_WILDIGN
12043 "wildignore",
12044#endif
12045#ifdef FEAT_WILDMENU
12046 "wildmenu",
12047#endif
12048#ifdef FEAT_WINDOWS
12049 "windows",
12050#endif
12051#ifdef FEAT_WAK
12052 "winaltkeys",
12053#endif
12054#ifdef FEAT_WRITEBACKUP
12055 "writebackup",
12056#endif
12057#ifdef FEAT_XIM
12058 "xim",
12059#endif
12060#ifdef FEAT_XFONTSET
12061 "xfontset",
12062#endif
12063#ifdef USE_XSMP
12064 "xsmp",
12065#endif
12066#ifdef USE_XSMP_INTERACT
12067 "xsmp_interact",
12068#endif
12069#ifdef FEAT_XCLIPBOARD
12070 "xterm_clipboard",
12071#endif
12072#ifdef FEAT_XTERM_SAVE
12073 "xterm_save",
12074#endif
12075#if defined(UNIX) && defined(FEAT_X11)
12076 "X11",
12077#endif
12078 NULL
12079 };
12080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012081 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 for (i = 0; has_list[i] != NULL; ++i)
12083 if (STRICMP(name, has_list[i]) == 0)
12084 {
12085 n = TRUE;
12086 break;
12087 }
12088
12089 if (n == FALSE)
12090 {
12091 if (STRNICMP(name, "patch", 5) == 0)
12092 n = has_patch(atoi((char *)name + 5));
12093 else if (STRICMP(name, "vim_starting") == 0)
12094 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012095#ifdef FEAT_MBYTE
12096 else if (STRICMP(name, "multi_byte_encoding") == 0)
12097 n = has_mbyte;
12098#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012099#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12100 else if (STRICMP(name, "balloon_multiline") == 0)
12101 n = multiline_balloon_available();
12102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103#ifdef DYNAMIC_TCL
12104 else if (STRICMP(name, "tcl") == 0)
12105 n = tcl_enabled(FALSE);
12106#endif
12107#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12108 else if (STRICMP(name, "iconv") == 0)
12109 n = iconv_enabled(FALSE);
12110#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012111#ifdef DYNAMIC_MZSCHEME
12112 else if (STRICMP(name, "mzscheme") == 0)
12113 n = mzscheme_enabled(FALSE);
12114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115#ifdef DYNAMIC_RUBY
12116 else if (STRICMP(name, "ruby") == 0)
12117 n = ruby_enabled(FALSE);
12118#endif
12119#ifdef DYNAMIC_PYTHON
12120 else if (STRICMP(name, "python") == 0)
12121 n = python_enabled(FALSE);
12122#endif
12123#ifdef DYNAMIC_PERL
12124 else if (STRICMP(name, "perl") == 0)
12125 n = perl_enabled(FALSE);
12126#endif
12127#ifdef FEAT_GUI
12128 else if (STRICMP(name, "gui_running") == 0)
12129 n = (gui.in_use || gui.starting);
12130# ifdef FEAT_GUI_W32
12131 else if (STRICMP(name, "gui_win32s") == 0)
12132 n = gui_is_win32s();
12133# endif
12134# ifdef FEAT_BROWSE
12135 else if (STRICMP(name, "browse") == 0)
12136 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12137# endif
12138#endif
12139#ifdef FEAT_SYN_HL
12140 else if (STRICMP(name, "syntax_items") == 0)
12141 n = syntax_present(curbuf);
12142#endif
12143#if defined(WIN3264)
12144 else if (STRICMP(name, "win95") == 0)
12145 n = mch_windows95();
12146#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012147#ifdef FEAT_NETBEANS_INTG
12148 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012149 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 }
12152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012153 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154}
12155
12156/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012157 * "has_key()" function
12158 */
12159 static void
12160f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012161 typval_T *argvars;
12162 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012163{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012164 if (argvars[0].v_type != VAR_DICT)
12165 {
12166 EMSG(_(e_dictreq));
12167 return;
12168 }
12169 if (argvars[0].vval.v_dict == NULL)
12170 return;
12171
12172 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012173 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012174}
12175
12176/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012177 * "haslocaldir()" function
12178 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012179 static void
12180f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012181 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012182 typval_T *rettv;
12183{
12184 rettv->vval.v_number = (curwin->w_localdir != NULL);
12185}
12186
12187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188 * "hasmapto()" function
12189 */
12190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012191f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012192 typval_T *argvars;
12193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194{
12195 char_u *name;
12196 char_u *mode;
12197 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012198 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012200 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012201 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202 mode = (char_u *)"nvo";
12203 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012204 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012205 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012206 if (argvars[2].v_type != VAR_UNKNOWN)
12207 abbr = get_tv_number(&argvars[2]);
12208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209
Bram Moolenaar2c932302006-03-18 21:42:09 +000012210 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012211 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012213 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214}
12215
12216/*
12217 * "histadd()" function
12218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012220f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012221 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223{
12224#ifdef FEAT_CMDHIST
12225 int histype;
12226 char_u *str;
12227 char_u buf[NUMBUFLEN];
12228#endif
12229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012230 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231 if (check_restricted() || check_secure())
12232 return;
12233#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012234 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12235 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236 if (histype >= 0)
12237 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012238 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239 if (*str != NUL)
12240 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012241 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012243 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244 return;
12245 }
12246 }
12247#endif
12248}
12249
12250/*
12251 * "histdel()" function
12252 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012254f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012255 typval_T *argvars UNUSED;
12256 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257{
12258#ifdef FEAT_CMDHIST
12259 int n;
12260 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012261 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012263 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12264 if (str == NULL)
12265 n = 0;
12266 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012268 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012269 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012271 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012272 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273 else
12274 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012275 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012276 get_tv_string_buf(&argvars[1], buf));
12277 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278#endif
12279}
12280
12281/*
12282 * "histget()" function
12283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012285f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012286 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288{
12289#ifdef FEAT_CMDHIST
12290 int type;
12291 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012292 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012294 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12295 if (str == NULL)
12296 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012298 {
12299 type = get_histtype(str);
12300 if (argvars[1].v_type == VAR_UNKNOWN)
12301 idx = get_history_idx(type);
12302 else
12303 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12304 /* -1 on type error */
12305 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012308 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012310 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311}
12312
12313/*
12314 * "histnr()" function
12315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012317f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012318 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320{
12321 int i;
12322
12323#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012324 char_u *history = get_tv_string_chk(&argvars[0]);
12325
12326 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327 if (i >= HIST_CMD && i < HIST_COUNT)
12328 i = get_history_idx(i);
12329 else
12330#endif
12331 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012332 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333}
12334
12335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336 * "highlightID(name)" function
12337 */
12338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012339f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012340 typval_T *argvars;
12341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012343 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344}
12345
12346/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012347 * "highlight_exists()" function
12348 */
12349 static void
12350f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012351 typval_T *argvars;
12352 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012353{
12354 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12355}
12356
12357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358 * "hostname()" function
12359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012361f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012362 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364{
12365 char_u hostname[256];
12366
12367 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012368 rettv->v_type = VAR_STRING;
12369 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370}
12371
12372/*
12373 * iconv() function
12374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012376f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012377 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379{
12380#ifdef FEAT_MBYTE
12381 char_u buf1[NUMBUFLEN];
12382 char_u buf2[NUMBUFLEN];
12383 char_u *from, *to, *str;
12384 vimconv_T vimconv;
12385#endif
12386
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012387 rettv->v_type = VAR_STRING;
12388 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389
12390#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012391 str = get_tv_string(&argvars[0]);
12392 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12393 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394 vimconv.vc_type = CONV_NONE;
12395 convert_setup(&vimconv, from, to);
12396
12397 /* If the encodings are equal, no conversion needed. */
12398 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012399 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012401 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012402
12403 convert_setup(&vimconv, NULL, NULL);
12404 vim_free(from);
12405 vim_free(to);
12406#endif
12407}
12408
12409/*
12410 * "indent()" function
12411 */
12412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012413f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012414 typval_T *argvars;
12415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416{
12417 linenr_T lnum;
12418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012419 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012421 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012423 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424}
12425
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012426/*
12427 * "index()" function
12428 */
12429 static void
12430f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012431 typval_T *argvars;
12432 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012433{
Bram Moolenaar33570922005-01-25 22:26:29 +000012434 list_T *l;
12435 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012436 long idx = 0;
12437 int ic = FALSE;
12438
12439 rettv->vval.v_number = -1;
12440 if (argvars[0].v_type != VAR_LIST)
12441 {
12442 EMSG(_(e_listreq));
12443 return;
12444 }
12445 l = argvars[0].vval.v_list;
12446 if (l != NULL)
12447 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012448 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012449 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012451 int error = FALSE;
12452
Bram Moolenaar758711c2005-02-02 23:11:38 +000012453 /* Start at specified item. Use the cached index that list_find()
12454 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012455 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012456 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012457 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012458 ic = get_tv_number_chk(&argvars[3], &error);
12459 if (error)
12460 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012461 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012462
Bram Moolenaar758711c2005-02-02 23:11:38 +000012463 for ( ; item != NULL; item = item->li_next, ++idx)
12464 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012465 {
12466 rettv->vval.v_number = idx;
12467 break;
12468 }
12469 }
12470}
12471
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472static int inputsecret_flag = 0;
12473
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012474static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12475
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012477 * This function is used by f_input() and f_inputdialog() functions. The third
12478 * argument to f_input() specifies the type of completion to use at the
12479 * prompt. The third argument to f_inputdialog() specifies the value to return
12480 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481 */
12482 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012483get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012484 typval_T *argvars;
12485 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012486 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012488 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489 char_u *p = NULL;
12490 int c;
12491 char_u buf[NUMBUFLEN];
12492 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012493 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012494 int xp_type = EXPAND_NOTHING;
12495 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012497 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012498 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499
12500#ifdef NO_CONSOLE_INPUT
12501 /* While starting up, there is no place to enter text. */
12502 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504#endif
12505
12506 cmd_silent = FALSE; /* Want to see the prompt. */
12507 if (prompt != NULL)
12508 {
12509 /* Only the part of the message after the last NL is considered as
12510 * prompt for the command line */
12511 p = vim_strrchr(prompt, '\n');
12512 if (p == NULL)
12513 p = prompt;
12514 else
12515 {
12516 ++p;
12517 c = *p;
12518 *p = NUL;
12519 msg_start();
12520 msg_clr_eos();
12521 msg_puts_attr(prompt, echo_attr);
12522 msg_didout = FALSE;
12523 msg_starthere();
12524 *p = c;
12525 }
12526 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012528 if (argvars[1].v_type != VAR_UNKNOWN)
12529 {
12530 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12531 if (defstr != NULL)
12532 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012534 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012535 {
12536 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012537 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012538 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012539
Bram Moolenaar4463f292005-09-25 22:20:24 +000012540 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012541
Bram Moolenaar4463f292005-09-25 22:20:24 +000012542 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12543 if (xp_name == NULL)
12544 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012545
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012546 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012547
Bram Moolenaar4463f292005-09-25 22:20:24 +000012548 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12549 &xp_arg) == FAIL)
12550 return;
12551 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012552 }
12553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012554 if (defstr != NULL)
12555 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012556 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12557 xp_type, xp_arg);
12558
12559 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012561 /* since the user typed this, no need to wait for return */
12562 need_wait_return = FALSE;
12563 msg_didout = FALSE;
12564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565 cmd_silent = cmd_silent_save;
12566}
12567
12568/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012569 * "input()" function
12570 * Also handles inputsecret() when inputsecret is set.
12571 */
12572 static void
12573f_input(argvars, rettv)
12574 typval_T *argvars;
12575 typval_T *rettv;
12576{
12577 get_user_input(argvars, rettv, FALSE);
12578}
12579
12580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581 * "inputdialog()" function
12582 */
12583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012584f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012585 typval_T *argvars;
12586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587{
12588#if defined(FEAT_GUI_TEXTDIALOG)
12589 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12590 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12591 {
12592 char_u *message;
12593 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012594 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012596 message = get_tv_string_chk(&argvars[0]);
12597 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012598 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012599 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600 else
12601 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012602 if (message != NULL && defstr != NULL
12603 && do_dialog(VIM_QUESTION, NULL, message,
12604 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012605 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606 else
12607 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012608 if (message != NULL && defstr != NULL
12609 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012610 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012611 rettv->vval.v_string = vim_strsave(
12612 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012614 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012616 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617 }
12618 else
12619#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012620 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621}
12622
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012623/*
12624 * "inputlist()" function
12625 */
12626 static void
12627f_inputlist(argvars, rettv)
12628 typval_T *argvars;
12629 typval_T *rettv;
12630{
12631 listitem_T *li;
12632 int selected;
12633 int mouse_used;
12634
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012635#ifdef NO_CONSOLE_INPUT
12636 /* While starting up, there is no place to enter text. */
12637 if (no_console_input())
12638 return;
12639#endif
12640 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12641 {
12642 EMSG2(_(e_listarg), "inputlist()");
12643 return;
12644 }
12645
12646 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012647 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012648 lines_left = Rows; /* avoid more prompt */
12649 msg_scroll = TRUE;
12650 msg_clr_eos();
12651
12652 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12653 {
12654 msg_puts(get_tv_string(&li->li_tv));
12655 msg_putchar('\n');
12656 }
12657
12658 /* Ask for choice. */
12659 selected = prompt_for_number(&mouse_used);
12660 if (mouse_used)
12661 selected -= lines_left;
12662
12663 rettv->vval.v_number = selected;
12664}
12665
12666
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12668
12669/*
12670 * "inputrestore()" function
12671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012673f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012674 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676{
12677 if (ga_userinput.ga_len > 0)
12678 {
12679 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12681 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012682 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683 }
12684 else if (p_verbose > 1)
12685 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012686 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012687 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 }
12689}
12690
12691/*
12692 * "inputsave()" function
12693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012695f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012696 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012699 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700 if (ga_grow(&ga_userinput, 1) == OK)
12701 {
12702 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12703 + ga_userinput.ga_len);
12704 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012705 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706 }
12707 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012708 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709}
12710
12711/*
12712 * "inputsecret()" function
12713 */
12714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012715f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012716 typval_T *argvars;
12717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718{
12719 ++cmdline_star;
12720 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012721 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722 --cmdline_star;
12723 --inputsecret_flag;
12724}
12725
12726/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012727 * "insert()" function
12728 */
12729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012730f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012731 typval_T *argvars;
12732 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012733{
12734 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012735 listitem_T *item;
12736 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012737 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012738
12739 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012740 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012741 else if ((l = argvars[0].vval.v_list) != NULL
12742 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012743 {
12744 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012745 before = get_tv_number_chk(&argvars[2], &error);
12746 if (error)
12747 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012748
Bram Moolenaar758711c2005-02-02 23:11:38 +000012749 if (before == l->lv_len)
12750 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012751 else
12752 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012753 item = list_find(l, before);
12754 if (item == NULL)
12755 {
12756 EMSGN(_(e_listidx), before);
12757 l = NULL;
12758 }
12759 }
12760 if (l != NULL)
12761 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012762 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012763 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012764 }
12765 }
12766}
12767
12768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769 * "isdirectory()" function
12770 */
12771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012773 typval_T *argvars;
12774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777}
12778
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012779/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012780 * "islocked()" function
12781 */
12782 static void
12783f_islocked(argvars, rettv)
12784 typval_T *argvars;
12785 typval_T *rettv;
12786{
12787 lval_T lv;
12788 char_u *end;
12789 dictitem_T *di;
12790
12791 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012792 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12793 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012794 if (end != NULL && lv.ll_name != NULL)
12795 {
12796 if (*end != NUL)
12797 EMSG(_(e_trailing));
12798 else
12799 {
12800 if (lv.ll_tv == NULL)
12801 {
12802 if (check_changedtick(lv.ll_name))
12803 rettv->vval.v_number = 1; /* always locked */
12804 else
12805 {
12806 di = find_var(lv.ll_name, NULL);
12807 if (di != NULL)
12808 {
12809 /* Consider a variable locked when:
12810 * 1. the variable itself is locked
12811 * 2. the value of the variable is locked.
12812 * 3. the List or Dict value is locked.
12813 */
12814 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12815 || tv_islocked(&di->di_tv));
12816 }
12817 }
12818 }
12819 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012820 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012821 else if (lv.ll_newkey != NULL)
12822 EMSG2(_(e_dictkey), lv.ll_newkey);
12823 else if (lv.ll_list != NULL)
12824 /* List item. */
12825 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12826 else
12827 /* Dictionary item. */
12828 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12829 }
12830 }
12831
12832 clear_lval(&lv);
12833}
12834
Bram Moolenaar33570922005-01-25 22:26:29 +000012835static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012836
12837/*
12838 * Turn a dict into a list:
12839 * "what" == 0: list of keys
12840 * "what" == 1: list of values
12841 * "what" == 2: list of items
12842 */
12843 static void
12844dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012845 typval_T *argvars;
12846 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012847 int what;
12848{
Bram Moolenaar33570922005-01-25 22:26:29 +000012849 list_T *l2;
12850 dictitem_T *di;
12851 hashitem_T *hi;
12852 listitem_T *li;
12853 listitem_T *li2;
12854 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012855 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012856
Bram Moolenaar8c711452005-01-14 21:53:12 +000012857 if (argvars[0].v_type != VAR_DICT)
12858 {
12859 EMSG(_(e_dictreq));
12860 return;
12861 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012862 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012863 return;
12864
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012865 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012866 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012867
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012868 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012869 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012870 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012871 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012872 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012873 --todo;
12874 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012875
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012876 li = listitem_alloc();
12877 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012878 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012879 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012880
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012881 if (what == 0)
12882 {
12883 /* keys() */
12884 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012885 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012886 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12887 }
12888 else if (what == 1)
12889 {
12890 /* values() */
12891 copy_tv(&di->di_tv, &li->li_tv);
12892 }
12893 else
12894 {
12895 /* items() */
12896 l2 = list_alloc();
12897 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012898 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012899 li->li_tv.vval.v_list = l2;
12900 if (l2 == NULL)
12901 break;
12902 ++l2->lv_refcount;
12903
12904 li2 = listitem_alloc();
12905 if (li2 == NULL)
12906 break;
12907 list_append(l2, li2);
12908 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012909 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012910 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12911
12912 li2 = listitem_alloc();
12913 if (li2 == NULL)
12914 break;
12915 list_append(l2, li2);
12916 copy_tv(&di->di_tv, &li2->li_tv);
12917 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012918 }
12919 }
12920}
12921
12922/*
12923 * "items(dict)" function
12924 */
12925 static void
12926f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012927 typval_T *argvars;
12928 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012929{
12930 dict_list(argvars, rettv, 2);
12931}
12932
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012934 * "join()" function
12935 */
12936 static void
12937f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012938 typval_T *argvars;
12939 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012940{
12941 garray_T ga;
12942 char_u *sep;
12943
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012944 if (argvars[0].v_type != VAR_LIST)
12945 {
12946 EMSG(_(e_listreq));
12947 return;
12948 }
12949 if (argvars[0].vval.v_list == NULL)
12950 return;
12951 if (argvars[1].v_type == VAR_UNKNOWN)
12952 sep = (char_u *)" ";
12953 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012954 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012955
12956 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012957
12958 if (sep != NULL)
12959 {
12960 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012961 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012962 ga_append(&ga, NUL);
12963 rettv->vval.v_string = (char_u *)ga.ga_data;
12964 }
12965 else
12966 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012967}
12968
12969/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012970 * "keys()" function
12971 */
12972 static void
12973f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012974 typval_T *argvars;
12975 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012976{
12977 dict_list(argvars, rettv, 0);
12978}
12979
12980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981 * "last_buffer_nr()" function.
12982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012984f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012985 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987{
12988 int n = 0;
12989 buf_T *buf;
12990
12991 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12992 if (n < buf->b_fnum)
12993 n = buf->b_fnum;
12994
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012995 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012996}
12997
12998/*
12999 * "len()" function
13000 */
13001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013002f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013003 typval_T *argvars;
13004 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013005{
13006 switch (argvars[0].v_type)
13007 {
13008 case VAR_STRING:
13009 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013010 rettv->vval.v_number = (varnumber_T)STRLEN(
13011 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013012 break;
13013 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013014 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013015 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013016 case VAR_DICT:
13017 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13018 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013019 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013020 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013021 break;
13022 }
13023}
13024
Bram Moolenaar33570922005-01-25 22:26:29 +000013025static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013026
13027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013028libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013029 typval_T *argvars;
13030 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013031 int type;
13032{
13033#ifdef FEAT_LIBCALL
13034 char_u *string_in;
13035 char_u **string_result;
13036 int nr_result;
13037#endif
13038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013039 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013040 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013041 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013042
13043 if (check_restricted() || check_secure())
13044 return;
13045
13046#ifdef FEAT_LIBCALL
13047 /* The first two args must be strings, otherwise its meaningless */
13048 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13049 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013050 string_in = NULL;
13051 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013052 string_in = argvars[2].vval.v_string;
13053 if (type == VAR_NUMBER)
13054 string_result = NULL;
13055 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013056 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013057 if (mch_libcall(argvars[0].vval.v_string,
13058 argvars[1].vval.v_string,
13059 string_in,
13060 argvars[2].vval.v_number,
13061 string_result,
13062 &nr_result) == OK
13063 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013064 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013065 }
13066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067}
13068
13069/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013070 * "libcall()" function
13071 */
13072 static void
13073f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013074 typval_T *argvars;
13075 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013076{
13077 libcall_common(argvars, rettv, VAR_STRING);
13078}
13079
13080/*
13081 * "libcallnr()" function
13082 */
13083 static void
13084f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013085 typval_T *argvars;
13086 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013087{
13088 libcall_common(argvars, rettv, VAR_NUMBER);
13089}
13090
13091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092 * "line(string)" function
13093 */
13094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013095f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013096 typval_T *argvars;
13097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013098{
13099 linenr_T lnum = 0;
13100 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013101 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013103 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104 if (fp != NULL)
13105 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013106 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107}
13108
13109/*
13110 * "line2byte(lnum)" function
13111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013113f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013114 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116{
13117#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013118 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119#else
13120 linenr_T lnum;
13121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013122 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013124 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013126 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13127 if (rettv->vval.v_number >= 0)
13128 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129#endif
13130}
13131
13132/*
13133 * "lispindent(lnum)" function
13134 */
13135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013136f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013137 typval_T *argvars;
13138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139{
13140#ifdef FEAT_LISP
13141 pos_T pos;
13142 linenr_T lnum;
13143
13144 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013145 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13147 {
13148 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013149 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150 curwin->w_cursor = pos;
13151 }
13152 else
13153#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013154 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155}
13156
13157/*
13158 * "localtime()" function
13159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013161f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013162 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013165 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166}
13167
Bram Moolenaar33570922005-01-25 22:26:29 +000013168static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169
13170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013171get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013172 typval_T *argvars;
13173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 int exact;
13175{
13176 char_u *keys;
13177 char_u *which;
13178 char_u buf[NUMBUFLEN];
13179 char_u *keys_buf = NULL;
13180 char_u *rhs;
13181 int mode;
13182 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013183 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184
13185 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013186 rettv->v_type = VAR_STRING;
13187 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013189 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190 if (*keys == NUL)
13191 return;
13192
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013193 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013195 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013196 if (argvars[2].v_type != VAR_UNKNOWN)
13197 abbr = get_tv_number(&argvars[2]);
13198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199 else
13200 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013201 if (which == NULL)
13202 return;
13203
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 mode = get_map_mode(&which, 0);
13205
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013206 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013207 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208 vim_free(keys_buf);
13209 if (rhs != NULL)
13210 {
13211 ga_init(&ga);
13212 ga.ga_itemsize = 1;
13213 ga.ga_growsize = 40;
13214
13215 while (*rhs != NUL)
13216 ga_concat(&ga, str2special(&rhs, FALSE));
13217
13218 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013219 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220 }
13221}
13222
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013223#ifdef FEAT_FLOAT
13224/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013225 * "log()" function
13226 */
13227 static void
13228f_log(argvars, rettv)
13229 typval_T *argvars;
13230 typval_T *rettv;
13231{
13232 float_T f;
13233
13234 rettv->v_type = VAR_FLOAT;
13235 if (get_float_arg(argvars, &f) == OK)
13236 rettv->vval.v_float = log(f);
13237 else
13238 rettv->vval.v_float = 0.0;
13239}
13240
13241/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013242 * "log10()" function
13243 */
13244 static void
13245f_log10(argvars, rettv)
13246 typval_T *argvars;
13247 typval_T *rettv;
13248{
13249 float_T f;
13250
13251 rettv->v_type = VAR_FLOAT;
13252 if (get_float_arg(argvars, &f) == OK)
13253 rettv->vval.v_float = log10(f);
13254 else
13255 rettv->vval.v_float = 0.0;
13256}
13257#endif
13258
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013260 * "map()" function
13261 */
13262 static void
13263f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013264 typval_T *argvars;
13265 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013266{
13267 filter_map(argvars, rettv, TRUE);
13268}
13269
13270/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013271 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 */
13273 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013274f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013275 typval_T *argvars;
13276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013278 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279}
13280
13281/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013282 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283 */
13284 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013285f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013286 typval_T *argvars;
13287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013289 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290}
13291
Bram Moolenaar33570922005-01-25 22:26:29 +000013292static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293
13294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013295find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013296 typval_T *argvars;
13297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 int type;
13299{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013300 char_u *str = NULL;
13301 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302 char_u *pat;
13303 regmatch_T regmatch;
13304 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013305 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306 char_u *save_cpo;
13307 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013308 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013309 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013310 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013311 list_T *l = NULL;
13312 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013313 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013314 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315
13316 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13317 save_cpo = p_cpo;
13318 p_cpo = (char_u *)"";
13319
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013320 rettv->vval.v_number = -1;
13321 if (type == 3)
13322 {
13323 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013324 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013325 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013326 }
13327 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013328 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013329 rettv->v_type = VAR_STRING;
13330 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013333 if (argvars[0].v_type == VAR_LIST)
13334 {
13335 if ((l = argvars[0].vval.v_list) == NULL)
13336 goto theend;
13337 li = l->lv_first;
13338 }
13339 else
13340 expr = str = get_tv_string(&argvars[0]);
13341
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013342 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13343 if (pat == NULL)
13344 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013345
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013346 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013348 int error = FALSE;
13349
13350 start = get_tv_number_chk(&argvars[2], &error);
13351 if (error)
13352 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013353 if (l != NULL)
13354 {
13355 li = list_find(l, start);
13356 if (li == NULL)
13357 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013358 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013359 }
13360 else
13361 {
13362 if (start < 0)
13363 start = 0;
13364 if (start > (long)STRLEN(str))
13365 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013366 /* When "count" argument is there ignore matches before "start",
13367 * otherwise skip part of the string. Differs when pattern is "^"
13368 * or "\<". */
13369 if (argvars[3].v_type != VAR_UNKNOWN)
13370 startcol = start;
13371 else
13372 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013373 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013374
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013375 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013376 nth = get_tv_number_chk(&argvars[3], &error);
13377 if (error)
13378 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 }
13380
13381 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13382 if (regmatch.regprog != NULL)
13383 {
13384 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013385
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013386 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013387 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013388 if (l != NULL)
13389 {
13390 if (li == NULL)
13391 {
13392 match = FALSE;
13393 break;
13394 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013395 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013396 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013397 if (str == NULL)
13398 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013399 }
13400
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013401 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013402
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013403 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013404 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013405 if (l == NULL && !match)
13406 break;
13407
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013408 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013409 if (l != NULL)
13410 {
13411 li = li->li_next;
13412 ++idx;
13413 }
13414 else
13415 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013416#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013417 startcol = (colnr_T)(regmatch.startp[0]
13418 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013419#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013420 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013421#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013422 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013423 }
13424
13425 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013427 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013428 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013429 int i;
13430
13431 /* return list with matched string and submatches */
13432 for (i = 0; i < NSUBEXP; ++i)
13433 {
13434 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013435 {
13436 if (list_append_string(rettv->vval.v_list,
13437 (char_u *)"", 0) == FAIL)
13438 break;
13439 }
13440 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013441 regmatch.startp[i],
13442 (int)(regmatch.endp[i] - regmatch.startp[i]))
13443 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013444 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013445 }
13446 }
13447 else if (type == 2)
13448 {
13449 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013450 if (l != NULL)
13451 copy_tv(&li->li_tv, rettv);
13452 else
13453 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013455 }
13456 else if (l != NULL)
13457 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 else
13459 {
13460 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013461 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462 (varnumber_T)(regmatch.startp[0] - str);
13463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013464 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013466 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 }
13468 }
13469 vim_free(regmatch.regprog);
13470 }
13471
13472theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013473 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474 p_cpo = save_cpo;
13475}
13476
13477/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013478 * "match()" function
13479 */
13480 static void
13481f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013482 typval_T *argvars;
13483 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013484{
13485 find_some_match(argvars, rettv, 1);
13486}
13487
13488/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013489 * "matchadd()" function
13490 */
13491 static void
13492f_matchadd(argvars, rettv)
13493 typval_T *argvars;
13494 typval_T *rettv;
13495{
13496#ifdef FEAT_SEARCH_EXTRA
13497 char_u buf[NUMBUFLEN];
13498 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13499 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13500 int prio = 10; /* default priority */
13501 int id = -1;
13502 int error = FALSE;
13503
13504 rettv->vval.v_number = -1;
13505
13506 if (grp == NULL || pat == NULL)
13507 return;
13508 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013509 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013510 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013511 if (argvars[3].v_type != VAR_UNKNOWN)
13512 id = get_tv_number_chk(&argvars[3], &error);
13513 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013514 if (error == TRUE)
13515 return;
13516 if (id >= 1 && id <= 3)
13517 {
13518 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13519 return;
13520 }
13521
13522 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13523#endif
13524}
13525
13526/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013527 * "matcharg()" function
13528 */
13529 static void
13530f_matcharg(argvars, rettv)
13531 typval_T *argvars;
13532 typval_T *rettv;
13533{
13534 if (rettv_list_alloc(rettv) == OK)
13535 {
13536#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013537 int id = get_tv_number(&argvars[0]);
13538 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013539
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013540 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013541 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013542 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13543 {
13544 list_append_string(rettv->vval.v_list,
13545 syn_id2name(m->hlg_id), -1);
13546 list_append_string(rettv->vval.v_list, m->pattern, -1);
13547 }
13548 else
13549 {
13550 list_append_string(rettv->vval.v_list, NUL, -1);
13551 list_append_string(rettv->vval.v_list, NUL, -1);
13552 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013553 }
13554#endif
13555 }
13556}
13557
13558/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013559 * "matchdelete()" function
13560 */
13561 static void
13562f_matchdelete(argvars, rettv)
13563 typval_T *argvars;
13564 typval_T *rettv;
13565{
13566#ifdef FEAT_SEARCH_EXTRA
13567 rettv->vval.v_number = match_delete(curwin,
13568 (int)get_tv_number(&argvars[0]), TRUE);
13569#endif
13570}
13571
13572/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013573 * "matchend()" function
13574 */
13575 static void
13576f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013577 typval_T *argvars;
13578 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013579{
13580 find_some_match(argvars, rettv, 0);
13581}
13582
13583/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013584 * "matchlist()" function
13585 */
13586 static void
13587f_matchlist(argvars, rettv)
13588 typval_T *argvars;
13589 typval_T *rettv;
13590{
13591 find_some_match(argvars, rettv, 3);
13592}
13593
13594/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013595 * "matchstr()" function
13596 */
13597 static void
13598f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013599 typval_T *argvars;
13600 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013601{
13602 find_some_match(argvars, rettv, 2);
13603}
13604
Bram Moolenaar33570922005-01-25 22:26:29 +000013605static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013606
13607 static void
13608max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013609 typval_T *argvars;
13610 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013611 int domax;
13612{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013613 long n = 0;
13614 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013615 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013616
13617 if (argvars[0].v_type == VAR_LIST)
13618 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013619 list_T *l;
13620 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013621
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013622 l = argvars[0].vval.v_list;
13623 if (l != NULL)
13624 {
13625 li = l->lv_first;
13626 if (li != NULL)
13627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013628 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013629 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013630 {
13631 li = li->li_next;
13632 if (li == NULL)
13633 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013634 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013635 if (domax ? i > n : i < n)
13636 n = i;
13637 }
13638 }
13639 }
13640 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013641 else if (argvars[0].v_type == VAR_DICT)
13642 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013643 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013644 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013645 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013646 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013647
13648 d = argvars[0].vval.v_dict;
13649 if (d != NULL)
13650 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013651 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013652 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013653 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013654 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013655 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013656 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013657 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013658 if (first)
13659 {
13660 n = i;
13661 first = FALSE;
13662 }
13663 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013664 n = i;
13665 }
13666 }
13667 }
13668 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013669 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013670 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013671 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013672}
13673
13674/*
13675 * "max()" function
13676 */
13677 static void
13678f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013679 typval_T *argvars;
13680 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013681{
13682 max_min(argvars, rettv, TRUE);
13683}
13684
13685/*
13686 * "min()" function
13687 */
13688 static void
13689f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013690 typval_T *argvars;
13691 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013692{
13693 max_min(argvars, rettv, FALSE);
13694}
13695
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013696static int mkdir_recurse __ARGS((char_u *dir, int prot));
13697
13698/*
13699 * Create the directory in which "dir" is located, and higher levels when
13700 * needed.
13701 */
13702 static int
13703mkdir_recurse(dir, prot)
13704 char_u *dir;
13705 int prot;
13706{
13707 char_u *p;
13708 char_u *updir;
13709 int r = FAIL;
13710
13711 /* Get end of directory name in "dir".
13712 * We're done when it's "/" or "c:/". */
13713 p = gettail_sep(dir);
13714 if (p <= get_past_head(dir))
13715 return OK;
13716
13717 /* If the directory exists we're done. Otherwise: create it.*/
13718 updir = vim_strnsave(dir, (int)(p - dir));
13719 if (updir == NULL)
13720 return FAIL;
13721 if (mch_isdir(updir))
13722 r = OK;
13723 else if (mkdir_recurse(updir, prot) == OK)
13724 r = vim_mkdir_emsg(updir, prot);
13725 vim_free(updir);
13726 return r;
13727}
13728
13729#ifdef vim_mkdir
13730/*
13731 * "mkdir()" function
13732 */
13733 static void
13734f_mkdir(argvars, rettv)
13735 typval_T *argvars;
13736 typval_T *rettv;
13737{
13738 char_u *dir;
13739 char_u buf[NUMBUFLEN];
13740 int prot = 0755;
13741
13742 rettv->vval.v_number = FAIL;
13743 if (check_restricted() || check_secure())
13744 return;
13745
13746 dir = get_tv_string_buf(&argvars[0], buf);
13747 if (argvars[1].v_type != VAR_UNKNOWN)
13748 {
13749 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013750 prot = get_tv_number_chk(&argvars[2], NULL);
13751 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013752 mkdir_recurse(dir, prot);
13753 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013754 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013755}
13756#endif
13757
Bram Moolenaar0d660222005-01-07 21:51:51 +000013758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759 * "mode()" function
13760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013762f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013763 typval_T *argvars;
13764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013766 char_u buf[3];
13767
13768 buf[1] = NUL;
13769 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770
13771#ifdef FEAT_VISUAL
13772 if (VIsual_active)
13773 {
13774 if (VIsual_select)
13775 buf[0] = VIsual_mode + 's' - 'v';
13776 else
13777 buf[0] = VIsual_mode;
13778 }
13779 else
13780#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013781 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13782 || State == CONFIRM)
13783 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013785 if (State == ASKMORE)
13786 buf[1] = 'm';
13787 else if (State == CONFIRM)
13788 buf[1] = '?';
13789 }
13790 else if (State == EXTERNCMD)
13791 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792 else if (State & INSERT)
13793 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013794#ifdef FEAT_VREPLACE
13795 if (State & VREPLACE_FLAG)
13796 {
13797 buf[0] = 'R';
13798 buf[1] = 'v';
13799 }
13800 else
13801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802 if (State & REPLACE_FLAG)
13803 buf[0] = 'R';
13804 else
13805 buf[0] = 'i';
13806 }
13807 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013808 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013810 if (exmode_active)
13811 buf[1] = 'v';
13812 }
13813 else if (exmode_active)
13814 {
13815 buf[0] = 'c';
13816 buf[1] = 'e';
13817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013819 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013821 if (finish_op)
13822 buf[1] = 'o';
13823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013825 /* Clear out the minor mode when the argument is not a non-zero number or
13826 * non-empty string. */
13827 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013828 buf[1] = NUL;
13829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013830 rettv->vval.v_string = vim_strsave(buf);
13831 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832}
13833
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013834#ifdef FEAT_MZSCHEME
13835/*
13836 * "mzeval()" function
13837 */
13838 static void
13839f_mzeval(argvars, rettv)
13840 typval_T *argvars;
13841 typval_T *rettv;
13842{
13843 char_u *str;
13844 char_u buf[NUMBUFLEN];
13845
13846 str = get_tv_string_buf(&argvars[0], buf);
13847 do_mzeval(str, rettv);
13848}
13849#endif
13850
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013852 * "nextnonblank()" function
13853 */
13854 static void
13855f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013856 typval_T *argvars;
13857 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013858{
13859 linenr_T lnum;
13860
13861 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13862 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013863 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013864 {
13865 lnum = 0;
13866 break;
13867 }
13868 if (*skipwhite(ml_get(lnum)) != NUL)
13869 break;
13870 }
13871 rettv->vval.v_number = lnum;
13872}
13873
13874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 * "nr2char()" function
13876 */
13877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013878f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013879 typval_T *argvars;
13880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881{
13882 char_u buf[NUMBUFLEN];
13883
13884#ifdef FEAT_MBYTE
13885 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013886 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887 else
13888#endif
13889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013890 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891 buf[1] = NUL;
13892 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013893 rettv->v_type = VAR_STRING;
13894 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013895}
13896
13897/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013898 * "pathshorten()" function
13899 */
13900 static void
13901f_pathshorten(argvars, rettv)
13902 typval_T *argvars;
13903 typval_T *rettv;
13904{
13905 char_u *p;
13906
13907 rettv->v_type = VAR_STRING;
13908 p = get_tv_string_chk(&argvars[0]);
13909 if (p == NULL)
13910 rettv->vval.v_string = NULL;
13911 else
13912 {
13913 p = vim_strsave(p);
13914 rettv->vval.v_string = p;
13915 if (p != NULL)
13916 shorten_dir(p);
13917 }
13918}
13919
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013920#ifdef FEAT_FLOAT
13921/*
13922 * "pow()" function
13923 */
13924 static void
13925f_pow(argvars, rettv)
13926 typval_T *argvars;
13927 typval_T *rettv;
13928{
13929 float_T fx, fy;
13930
13931 rettv->v_type = VAR_FLOAT;
13932 if (get_float_arg(argvars, &fx) == OK
13933 && get_float_arg(&argvars[1], &fy) == OK)
13934 rettv->vval.v_float = pow(fx, fy);
13935 else
13936 rettv->vval.v_float = 0.0;
13937}
13938#endif
13939
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013940/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013941 * "prevnonblank()" function
13942 */
13943 static void
13944f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013945 typval_T *argvars;
13946 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013947{
13948 linenr_T lnum;
13949
13950 lnum = get_tv_lnum(argvars);
13951 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13952 lnum = 0;
13953 else
13954 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13955 --lnum;
13956 rettv->vval.v_number = lnum;
13957}
13958
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013959#ifdef HAVE_STDARG_H
13960/* This dummy va_list is here because:
13961 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13962 * - locally in the function results in a "used before set" warning
13963 * - using va_start() to initialize it gives "function with fixed args" error */
13964static va_list ap;
13965#endif
13966
Bram Moolenaar8c711452005-01-14 21:53:12 +000013967/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013968 * "printf()" function
13969 */
13970 static void
13971f_printf(argvars, rettv)
13972 typval_T *argvars;
13973 typval_T *rettv;
13974{
13975 rettv->v_type = VAR_STRING;
13976 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013977#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013978 {
13979 char_u buf[NUMBUFLEN];
13980 int len;
13981 char_u *s;
13982 int saved_did_emsg = did_emsg;
13983 char *fmt;
13984
13985 /* Get the required length, allocate the buffer and do it for real. */
13986 did_emsg = FALSE;
13987 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013988 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013989 if (!did_emsg)
13990 {
13991 s = alloc(len + 1);
13992 if (s != NULL)
13993 {
13994 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013995 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013996 }
13997 }
13998 did_emsg |= saved_did_emsg;
13999 }
14000#endif
14001}
14002
14003/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014004 * "pumvisible()" function
14005 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014006 static void
14007f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014008 typval_T *argvars UNUSED;
14009 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014010{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014011#ifdef FEAT_INS_EXPAND
14012 if (pum_visible())
14013 rettv->vval.v_number = 1;
14014#endif
14015}
14016
14017/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014018 * "range()" function
14019 */
14020 static void
14021f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014022 typval_T *argvars;
14023 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014024{
14025 long start;
14026 long end;
14027 long stride = 1;
14028 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014029 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014030
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014031 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014032 if (argvars[1].v_type == VAR_UNKNOWN)
14033 {
14034 end = start - 1;
14035 start = 0;
14036 }
14037 else
14038 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014039 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014040 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014041 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014042 }
14043
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014044 if (error)
14045 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014046 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014047 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014048 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014049 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014050 else
14051 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014052 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014053 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014054 if (list_append_number(rettv->vval.v_list,
14055 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014056 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014057 }
14058}
14059
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014060/*
14061 * "readfile()" function
14062 */
14063 static void
14064f_readfile(argvars, rettv)
14065 typval_T *argvars;
14066 typval_T *rettv;
14067{
14068 int binary = FALSE;
14069 char_u *fname;
14070 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014071 listitem_T *li;
14072#define FREAD_SIZE 200 /* optimized for text lines */
14073 char_u buf[FREAD_SIZE];
14074 int readlen; /* size of last fread() */
14075 int buflen; /* nr of valid chars in buf[] */
14076 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14077 int tolist; /* first byte in buf[] still to be put in list */
14078 int chop; /* how many CR to chop off */
14079 char_u *prev = NULL; /* previously read bytes, if any */
14080 int prevlen = 0; /* length of "prev" if not NULL */
14081 char_u *s;
14082 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014083 long maxline = MAXLNUM;
14084 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014085
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014086 if (argvars[1].v_type != VAR_UNKNOWN)
14087 {
14088 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14089 binary = TRUE;
14090 if (argvars[2].v_type != VAR_UNKNOWN)
14091 maxline = get_tv_number(&argvars[2]);
14092 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014093
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014094 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014095 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014096
14097 /* Always open the file in binary mode, library functions have a mind of
14098 * their own about CR-LF conversion. */
14099 fname = get_tv_string(&argvars[0]);
14100 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14101 {
14102 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14103 return;
14104 }
14105
14106 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014107 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014108 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014109 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014110 buflen = filtd + readlen;
14111 tolist = 0;
14112 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14113 {
14114 if (buf[filtd] == '\n' || readlen <= 0)
14115 {
14116 /* Only when in binary mode add an empty list item when the
14117 * last line ends in a '\n'. */
14118 if (!binary && readlen == 0 && filtd == 0)
14119 break;
14120
14121 /* Found end-of-line or end-of-file: add a text line to the
14122 * list. */
14123 chop = 0;
14124 if (!binary)
14125 while (filtd - chop - 1 >= tolist
14126 && buf[filtd - chop - 1] == '\r')
14127 ++chop;
14128 len = filtd - tolist - chop;
14129 if (prev == NULL)
14130 s = vim_strnsave(buf + tolist, len);
14131 else
14132 {
14133 s = alloc((unsigned)(prevlen + len + 1));
14134 if (s != NULL)
14135 {
14136 mch_memmove(s, prev, prevlen);
14137 vim_free(prev);
14138 prev = NULL;
14139 mch_memmove(s + prevlen, buf + tolist, len);
14140 s[prevlen + len] = NUL;
14141 }
14142 }
14143 tolist = filtd + 1;
14144
14145 li = listitem_alloc();
14146 if (li == NULL)
14147 {
14148 vim_free(s);
14149 break;
14150 }
14151 li->li_tv.v_type = VAR_STRING;
14152 li->li_tv.v_lock = 0;
14153 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014154 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014155
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014156 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014157 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014158 if (readlen <= 0)
14159 break;
14160 }
14161 else if (buf[filtd] == NUL)
14162 buf[filtd] = '\n';
14163 }
14164 if (readlen <= 0)
14165 break;
14166
14167 if (tolist == 0)
14168 {
14169 /* "buf" is full, need to move text to an allocated buffer */
14170 if (prev == NULL)
14171 {
14172 prev = vim_strnsave(buf, buflen);
14173 prevlen = buflen;
14174 }
14175 else
14176 {
14177 s = alloc((unsigned)(prevlen + buflen));
14178 if (s != NULL)
14179 {
14180 mch_memmove(s, prev, prevlen);
14181 mch_memmove(s + prevlen, buf, buflen);
14182 vim_free(prev);
14183 prev = s;
14184 prevlen += buflen;
14185 }
14186 }
14187 filtd = 0;
14188 }
14189 else
14190 {
14191 mch_memmove(buf, buf + tolist, buflen - tolist);
14192 filtd -= tolist;
14193 }
14194 }
14195
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014196 /*
14197 * For a negative line count use only the lines at the end of the file,
14198 * free the rest.
14199 */
14200 if (maxline < 0)
14201 while (cnt > -maxline)
14202 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014203 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014204 --cnt;
14205 }
14206
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014207 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014208 fclose(fd);
14209}
14210
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014211#if defined(FEAT_RELTIME)
14212static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14213
14214/*
14215 * Convert a List to proftime_T.
14216 * Return FAIL when there is something wrong.
14217 */
14218 static int
14219list2proftime(arg, tm)
14220 typval_T *arg;
14221 proftime_T *tm;
14222{
14223 long n1, n2;
14224 int error = FALSE;
14225
14226 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14227 || arg->vval.v_list->lv_len != 2)
14228 return FAIL;
14229 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14230 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14231# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014232 tm->HighPart = n1;
14233 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014234# else
14235 tm->tv_sec = n1;
14236 tm->tv_usec = n2;
14237# endif
14238 return error ? FAIL : OK;
14239}
14240#endif /* FEAT_RELTIME */
14241
14242/*
14243 * "reltime()" function
14244 */
14245 static void
14246f_reltime(argvars, rettv)
14247 typval_T *argvars;
14248 typval_T *rettv;
14249{
14250#ifdef FEAT_RELTIME
14251 proftime_T res;
14252 proftime_T start;
14253
14254 if (argvars[0].v_type == VAR_UNKNOWN)
14255 {
14256 /* No arguments: get current time. */
14257 profile_start(&res);
14258 }
14259 else if (argvars[1].v_type == VAR_UNKNOWN)
14260 {
14261 if (list2proftime(&argvars[0], &res) == FAIL)
14262 return;
14263 profile_end(&res);
14264 }
14265 else
14266 {
14267 /* Two arguments: compute the difference. */
14268 if (list2proftime(&argvars[0], &start) == FAIL
14269 || list2proftime(&argvars[1], &res) == FAIL)
14270 return;
14271 profile_sub(&res, &start);
14272 }
14273
14274 if (rettv_list_alloc(rettv) == OK)
14275 {
14276 long n1, n2;
14277
14278# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014279 n1 = res.HighPart;
14280 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014281# else
14282 n1 = res.tv_sec;
14283 n2 = res.tv_usec;
14284# endif
14285 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14286 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14287 }
14288#endif
14289}
14290
14291/*
14292 * "reltimestr()" function
14293 */
14294 static void
14295f_reltimestr(argvars, rettv)
14296 typval_T *argvars;
14297 typval_T *rettv;
14298{
14299#ifdef FEAT_RELTIME
14300 proftime_T tm;
14301#endif
14302
14303 rettv->v_type = VAR_STRING;
14304 rettv->vval.v_string = NULL;
14305#ifdef FEAT_RELTIME
14306 if (list2proftime(&argvars[0], &tm) == OK)
14307 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14308#endif
14309}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014310
Bram Moolenaar0d660222005-01-07 21:51:51 +000014311#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14312static void make_connection __ARGS((void));
14313static int check_connection __ARGS((void));
14314
14315 static void
14316make_connection()
14317{
14318 if (X_DISPLAY == NULL
14319# ifdef FEAT_GUI
14320 && !gui.in_use
14321# endif
14322 )
14323 {
14324 x_force_connect = TRUE;
14325 setup_term_clip();
14326 x_force_connect = FALSE;
14327 }
14328}
14329
14330 static int
14331check_connection()
14332{
14333 make_connection();
14334 if (X_DISPLAY == NULL)
14335 {
14336 EMSG(_("E240: No connection to Vim server"));
14337 return FAIL;
14338 }
14339 return OK;
14340}
14341#endif
14342
14343#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014344static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014345
14346 static void
14347remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014348 typval_T *argvars;
14349 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014350 int expr;
14351{
14352 char_u *server_name;
14353 char_u *keys;
14354 char_u *r = NULL;
14355 char_u buf[NUMBUFLEN];
14356# ifdef WIN32
14357 HWND w;
14358# else
14359 Window w;
14360# endif
14361
14362 if (check_restricted() || check_secure())
14363 return;
14364
14365# ifdef FEAT_X11
14366 if (check_connection() == FAIL)
14367 return;
14368# endif
14369
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014370 server_name = get_tv_string_chk(&argvars[0]);
14371 if (server_name == NULL)
14372 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014373 keys = get_tv_string_buf(&argvars[1], buf);
14374# ifdef WIN32
14375 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14376# else
14377 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14378 < 0)
14379# endif
14380 {
14381 if (r != NULL)
14382 EMSG(r); /* sending worked but evaluation failed */
14383 else
14384 EMSG2(_("E241: Unable to send to %s"), server_name);
14385 return;
14386 }
14387
14388 rettv->vval.v_string = r;
14389
14390 if (argvars[2].v_type != VAR_UNKNOWN)
14391 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014392 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014393 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014394 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014395
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014396 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014397 v.di_tv.v_type = VAR_STRING;
14398 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014399 idvar = get_tv_string_chk(&argvars[2]);
14400 if (idvar != NULL)
14401 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014402 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014403 }
14404}
14405#endif
14406
14407/*
14408 * "remote_expr()" function
14409 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014410 static void
14411f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014412 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014413 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014414{
14415 rettv->v_type = VAR_STRING;
14416 rettv->vval.v_string = NULL;
14417#ifdef FEAT_CLIENTSERVER
14418 remote_common(argvars, rettv, TRUE);
14419#endif
14420}
14421
14422/*
14423 * "remote_foreground()" function
14424 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014425 static void
14426f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014427 typval_T *argvars UNUSED;
14428 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014429{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014430#ifdef FEAT_CLIENTSERVER
14431# ifdef WIN32
14432 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014433 {
14434 char_u *server_name = get_tv_string_chk(&argvars[0]);
14435
14436 if (server_name != NULL)
14437 serverForeground(server_name);
14438 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014439# else
14440 /* Send a foreground() expression to the server. */
14441 argvars[1].v_type = VAR_STRING;
14442 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14443 argvars[2].v_type = VAR_UNKNOWN;
14444 remote_common(argvars, rettv, TRUE);
14445 vim_free(argvars[1].vval.v_string);
14446# endif
14447#endif
14448}
14449
Bram Moolenaar0d660222005-01-07 21:51:51 +000014450 static void
14451f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014452 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014453 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014454{
14455#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014456 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014457 char_u *s = NULL;
14458# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014459 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014460# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014461 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014462
14463 if (check_restricted() || check_secure())
14464 {
14465 rettv->vval.v_number = -1;
14466 return;
14467 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014468 serverid = get_tv_string_chk(&argvars[0]);
14469 if (serverid == NULL)
14470 {
14471 rettv->vval.v_number = -1;
14472 return; /* type error; errmsg already given */
14473 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014474# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014475 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014476 if (n == 0)
14477 rettv->vval.v_number = -1;
14478 else
14479 {
14480 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14481 rettv->vval.v_number = (s != NULL);
14482 }
14483# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014484 if (check_connection() == FAIL)
14485 return;
14486
14487 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014488 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014489# endif
14490
14491 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14492 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014493 char_u *retvar;
14494
Bram Moolenaar33570922005-01-25 22:26:29 +000014495 v.di_tv.v_type = VAR_STRING;
14496 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014497 retvar = get_tv_string_chk(&argvars[1]);
14498 if (retvar != NULL)
14499 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014500 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014501 }
14502#else
14503 rettv->vval.v_number = -1;
14504#endif
14505}
14506
Bram Moolenaar0d660222005-01-07 21:51:51 +000014507 static void
14508f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014509 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014510 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014511{
14512 char_u *r = NULL;
14513
14514#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014515 char_u *serverid = get_tv_string_chk(&argvars[0]);
14516
14517 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014518 {
14519# ifdef WIN32
14520 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014521 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014522
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014523 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014524 if (n != 0)
14525 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14526 if (r == NULL)
14527# else
14528 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014529 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014530# endif
14531 EMSG(_("E277: Unable to read a server reply"));
14532 }
14533#endif
14534 rettv->v_type = VAR_STRING;
14535 rettv->vval.v_string = r;
14536}
14537
14538/*
14539 * "remote_send()" function
14540 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014541 static void
14542f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014543 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014544 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014545{
14546 rettv->v_type = VAR_STRING;
14547 rettv->vval.v_string = NULL;
14548#ifdef FEAT_CLIENTSERVER
14549 remote_common(argvars, rettv, FALSE);
14550#endif
14551}
14552
14553/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014554 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014555 */
14556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014557f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014558 typval_T *argvars;
14559 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014560{
Bram Moolenaar33570922005-01-25 22:26:29 +000014561 list_T *l;
14562 listitem_T *item, *item2;
14563 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014564 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014565 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014566 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014567 dict_T *d;
14568 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014569
Bram Moolenaar8c711452005-01-14 21:53:12 +000014570 if (argvars[0].v_type == VAR_DICT)
14571 {
14572 if (argvars[2].v_type != VAR_UNKNOWN)
14573 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014574 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014575 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014576 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014577 key = get_tv_string_chk(&argvars[1]);
14578 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014580 di = dict_find(d, key, -1);
14581 if (di == NULL)
14582 EMSG2(_(e_dictkey), key);
14583 else
14584 {
14585 *rettv = di->di_tv;
14586 init_tv(&di->di_tv);
14587 dictitem_remove(d, di);
14588 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014589 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014590 }
14591 }
14592 else if (argvars[0].v_type != VAR_LIST)
14593 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014594 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014595 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014597 int error = FALSE;
14598
14599 idx = get_tv_number_chk(&argvars[1], &error);
14600 if (error)
14601 ; /* type error: do nothing, errmsg already given */
14602 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014603 EMSGN(_(e_listidx), idx);
14604 else
14605 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014606 if (argvars[2].v_type == VAR_UNKNOWN)
14607 {
14608 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014609 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014610 *rettv = item->li_tv;
14611 vim_free(item);
14612 }
14613 else
14614 {
14615 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014616 end = get_tv_number_chk(&argvars[2], &error);
14617 if (error)
14618 ; /* type error: do nothing */
14619 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014620 EMSGN(_(e_listidx), end);
14621 else
14622 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014623 int cnt = 0;
14624
14625 for (li = item; li != NULL; li = li->li_next)
14626 {
14627 ++cnt;
14628 if (li == item2)
14629 break;
14630 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014631 if (li == NULL) /* didn't find "item2" after "item" */
14632 EMSG(_(e_invrange));
14633 else
14634 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014635 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014636 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014637 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014638 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014639 l->lv_first = item;
14640 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014641 item->li_prev = NULL;
14642 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014643 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014644 }
14645 }
14646 }
14647 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014648 }
14649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650}
14651
14652/*
14653 * "rename({from}, {to})" function
14654 */
14655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014656f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014657 typval_T *argvars;
14658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659{
14660 char_u buf[NUMBUFLEN];
14661
14662 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014663 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014665 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14666 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667}
14668
14669/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014670 * "repeat()" function
14671 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014672 static void
14673f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014674 typval_T *argvars;
14675 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014676{
14677 char_u *p;
14678 int n;
14679 int slen;
14680 int len;
14681 char_u *r;
14682 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014683
14684 n = get_tv_number(&argvars[1]);
14685 if (argvars[0].v_type == VAR_LIST)
14686 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014687 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014688 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014689 if (list_extend(rettv->vval.v_list,
14690 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014691 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014692 }
14693 else
14694 {
14695 p = get_tv_string(&argvars[0]);
14696 rettv->v_type = VAR_STRING;
14697 rettv->vval.v_string = NULL;
14698
14699 slen = (int)STRLEN(p);
14700 len = slen * n;
14701 if (len <= 0)
14702 return;
14703
14704 r = alloc(len + 1);
14705 if (r != NULL)
14706 {
14707 for (i = 0; i < n; i++)
14708 mch_memmove(r + i * slen, p, (size_t)slen);
14709 r[len] = NUL;
14710 }
14711
14712 rettv->vval.v_string = r;
14713 }
14714}
14715
14716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717 * "resolve()" function
14718 */
14719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014720f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014721 typval_T *argvars;
14722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723{
14724 char_u *p;
14725
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014726 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727#ifdef FEAT_SHORTCUT
14728 {
14729 char_u *v = NULL;
14730
14731 v = mch_resolve_shortcut(p);
14732 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014733 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014735 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736 }
14737#else
14738# ifdef HAVE_READLINK
14739 {
14740 char_u buf[MAXPATHL + 1];
14741 char_u *cpy;
14742 int len;
14743 char_u *remain = NULL;
14744 char_u *q;
14745 int is_relative_to_current = FALSE;
14746 int has_trailing_pathsep = FALSE;
14747 int limit = 100;
14748
14749 p = vim_strsave(p);
14750
14751 if (p[0] == '.' && (vim_ispathsep(p[1])
14752 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14753 is_relative_to_current = TRUE;
14754
14755 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014756 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757 has_trailing_pathsep = TRUE;
14758
14759 q = getnextcomp(p);
14760 if (*q != NUL)
14761 {
14762 /* Separate the first path component in "p", and keep the
14763 * remainder (beginning with the path separator). */
14764 remain = vim_strsave(q - 1);
14765 q[-1] = NUL;
14766 }
14767
14768 for (;;)
14769 {
14770 for (;;)
14771 {
14772 len = readlink((char *)p, (char *)buf, MAXPATHL);
14773 if (len <= 0)
14774 break;
14775 buf[len] = NUL;
14776
14777 if (limit-- == 0)
14778 {
14779 vim_free(p);
14780 vim_free(remain);
14781 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014782 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014783 goto fail;
14784 }
14785
14786 /* Ensure that the result will have a trailing path separator
14787 * if the argument has one. */
14788 if (remain == NULL && has_trailing_pathsep)
14789 add_pathsep(buf);
14790
14791 /* Separate the first path component in the link value and
14792 * concatenate the remainders. */
14793 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14794 if (*q != NUL)
14795 {
14796 if (remain == NULL)
14797 remain = vim_strsave(q - 1);
14798 else
14799 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014800 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014801 if (cpy != NULL)
14802 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803 vim_free(remain);
14804 remain = cpy;
14805 }
14806 }
14807 q[-1] = NUL;
14808 }
14809
14810 q = gettail(p);
14811 if (q > p && *q == NUL)
14812 {
14813 /* Ignore trailing path separator. */
14814 q[-1] = NUL;
14815 q = gettail(p);
14816 }
14817 if (q > p && !mch_isFullName(buf))
14818 {
14819 /* symlink is relative to directory of argument */
14820 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14821 if (cpy != NULL)
14822 {
14823 STRCPY(cpy, p);
14824 STRCPY(gettail(cpy), buf);
14825 vim_free(p);
14826 p = cpy;
14827 }
14828 }
14829 else
14830 {
14831 vim_free(p);
14832 p = vim_strsave(buf);
14833 }
14834 }
14835
14836 if (remain == NULL)
14837 break;
14838
14839 /* Append the first path component of "remain" to "p". */
14840 q = getnextcomp(remain + 1);
14841 len = q - remain - (*q != NUL);
14842 cpy = vim_strnsave(p, STRLEN(p) + len);
14843 if (cpy != NULL)
14844 {
14845 STRNCAT(cpy, remain, len);
14846 vim_free(p);
14847 p = cpy;
14848 }
14849 /* Shorten "remain". */
14850 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014851 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 else
14853 {
14854 vim_free(remain);
14855 remain = NULL;
14856 }
14857 }
14858
14859 /* If the result is a relative path name, make it explicitly relative to
14860 * the current directory if and only if the argument had this form. */
14861 if (!vim_ispathsep(*p))
14862 {
14863 if (is_relative_to_current
14864 && *p != NUL
14865 && !(p[0] == '.'
14866 && (p[1] == NUL
14867 || vim_ispathsep(p[1])
14868 || (p[1] == '.'
14869 && (p[2] == NUL
14870 || vim_ispathsep(p[2]))))))
14871 {
14872 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014873 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014874 if (cpy != NULL)
14875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014876 vim_free(p);
14877 p = cpy;
14878 }
14879 }
14880 else if (!is_relative_to_current)
14881 {
14882 /* Strip leading "./". */
14883 q = p;
14884 while (q[0] == '.' && vim_ispathsep(q[1]))
14885 q += 2;
14886 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014887 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888 }
14889 }
14890
14891 /* Ensure that the result will have no trailing path separator
14892 * if the argument had none. But keep "/" or "//". */
14893 if (!has_trailing_pathsep)
14894 {
14895 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014896 if (after_pathsep(p, q))
14897 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898 }
14899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014900 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901 }
14902# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014903 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904# endif
14905#endif
14906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014907 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908
14909#ifdef HAVE_READLINK
14910fail:
14911#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014912 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014913}
14914
14915/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014916 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917 */
14918 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014919f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014920 typval_T *argvars;
14921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922{
Bram Moolenaar33570922005-01-25 22:26:29 +000014923 list_T *l;
14924 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925
Bram Moolenaar0d660222005-01-07 21:51:51 +000014926 if (argvars[0].v_type != VAR_LIST)
14927 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014928 else if ((l = argvars[0].vval.v_list) != NULL
14929 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014930 {
14931 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014932 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014933 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014934 while (li != NULL)
14935 {
14936 ni = li->li_prev;
14937 list_append(l, li);
14938 li = ni;
14939 }
14940 rettv->vval.v_list = l;
14941 rettv->v_type = VAR_LIST;
14942 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014943 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014945}
14946
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014947#define SP_NOMOVE 0x01 /* don't move cursor */
14948#define SP_REPEAT 0x02 /* repeat to find outer pair */
14949#define SP_RETCOUNT 0x04 /* return matchcount */
14950#define SP_SETPCMARK 0x08 /* set previous context mark */
14951#define SP_START 0x10 /* accept match at start position */
14952#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14953#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014954
Bram Moolenaar33570922005-01-25 22:26:29 +000014955static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014956
14957/*
14958 * Get flags for a search function.
14959 * Possibly sets "p_ws".
14960 * Returns BACKWARD, FORWARD or zero (for an error).
14961 */
14962 static int
14963get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014964 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014965 int *flagsp;
14966{
14967 int dir = FORWARD;
14968 char_u *flags;
14969 char_u nbuf[NUMBUFLEN];
14970 int mask;
14971
14972 if (varp->v_type != VAR_UNKNOWN)
14973 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014974 flags = get_tv_string_buf_chk(varp, nbuf);
14975 if (flags == NULL)
14976 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014977 while (*flags != NUL)
14978 {
14979 switch (*flags)
14980 {
14981 case 'b': dir = BACKWARD; break;
14982 case 'w': p_ws = TRUE; break;
14983 case 'W': p_ws = FALSE; break;
14984 default: mask = 0;
14985 if (flagsp != NULL)
14986 switch (*flags)
14987 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014988 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014989 case 'e': mask = SP_END; break;
14990 case 'm': mask = SP_RETCOUNT; break;
14991 case 'n': mask = SP_NOMOVE; break;
14992 case 'p': mask = SP_SUBPAT; break;
14993 case 'r': mask = SP_REPEAT; break;
14994 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014995 }
14996 if (mask == 0)
14997 {
14998 EMSG2(_(e_invarg2), flags);
14999 dir = 0;
15000 }
15001 else
15002 *flagsp |= mask;
15003 }
15004 if (dir == 0)
15005 break;
15006 ++flags;
15007 }
15008 }
15009 return dir;
15010}
15011
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015013 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015015 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015016search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015017 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015018 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015019 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015021 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015022 char_u *pat;
15023 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015024 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025 int save_p_ws = p_ws;
15026 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015027 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015028 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015029 proftime_T tm;
15030#ifdef FEAT_RELTIME
15031 long time_limit = 0;
15032#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015033 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015034 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015036 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015037 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015038 if (dir == 0)
15039 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015040 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015041 if (flags & SP_START)
15042 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015043 if (flags & SP_END)
15044 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015045
Bram Moolenaar76929292008-01-06 19:07:36 +000015046 /* Optional arguments: line number to stop searching and timeout. */
15047 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015048 {
15049 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15050 if (lnum_stop < 0)
15051 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015052#ifdef FEAT_RELTIME
15053 if (argvars[3].v_type != VAR_UNKNOWN)
15054 {
15055 time_limit = get_tv_number_chk(&argvars[3], NULL);
15056 if (time_limit < 0)
15057 goto theend;
15058 }
15059#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015060 }
15061
Bram Moolenaar76929292008-01-06 19:07:36 +000015062#ifdef FEAT_RELTIME
15063 /* Set the time limit, if there is one. */
15064 profile_setlimit(time_limit, &tm);
15065#endif
15066
Bram Moolenaar231334e2005-07-25 20:46:57 +000015067 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015068 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015069 * Check to make sure only those flags are set.
15070 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15071 * flags cannot be set. Check for that condition also.
15072 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015073 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015074 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015076 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015077 goto theend;
15078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015079
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015080 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015081 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015082 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015083 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015085 if (flags & SP_SUBPAT)
15086 retval = subpatnum;
15087 else
15088 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015089 if (flags & SP_SETPCMARK)
15090 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015091 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015092 if (match_pos != NULL)
15093 {
15094 /* Store the match cursor position */
15095 match_pos->lnum = pos.lnum;
15096 match_pos->col = pos.col + 1;
15097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098 /* "/$" will put the cursor after the end of the line, may need to
15099 * correct that here */
15100 check_cursor();
15101 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015102
15103 /* If 'n' flag is used: restore cursor position. */
15104 if (flags & SP_NOMOVE)
15105 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015106 else
15107 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015108theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015110
15111 return retval;
15112}
15113
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015114#ifdef FEAT_FLOAT
15115/*
15116 * "round({float})" function
15117 */
15118 static void
15119f_round(argvars, rettv)
15120 typval_T *argvars;
15121 typval_T *rettv;
15122{
15123 float_T f;
15124
15125 rettv->v_type = VAR_FLOAT;
15126 if (get_float_arg(argvars, &f) == OK)
15127 /* round() is not in C90, use ceil() or floor() instead. */
15128 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15129 else
15130 rettv->vval.v_float = 0.0;
15131}
15132#endif
15133
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015134/*
15135 * "search()" function
15136 */
15137 static void
15138f_search(argvars, rettv)
15139 typval_T *argvars;
15140 typval_T *rettv;
15141{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015142 int flags = 0;
15143
15144 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145}
15146
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015148 * "searchdecl()" function
15149 */
15150 static void
15151f_searchdecl(argvars, rettv)
15152 typval_T *argvars;
15153 typval_T *rettv;
15154{
15155 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015156 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015157 int error = FALSE;
15158 char_u *name;
15159
15160 rettv->vval.v_number = 1; /* default: FAIL */
15161
15162 name = get_tv_string_chk(&argvars[0]);
15163 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015164 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015165 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015166 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15167 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15168 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015169 if (!error && name != NULL)
15170 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015171 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015172}
15173
15174/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015175 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015177 static int
15178searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015179 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015180 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181{
15182 char_u *spat, *mpat, *epat;
15183 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015184 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185 int dir;
15186 int flags = 0;
15187 char_u nbuf1[NUMBUFLEN];
15188 char_u nbuf2[NUMBUFLEN];
15189 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015190 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015191 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015192 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015193
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015195 spat = get_tv_string_chk(&argvars[0]);
15196 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15197 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15198 if (spat == NULL || mpat == NULL || epat == NULL)
15199 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015200
Bram Moolenaar071d4272004-06-13 20:20:40 +000015201 /* Handle the optional fourth argument: flags */
15202 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015203 if (dir == 0)
15204 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015205
15206 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015207 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15208 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015209 if ((flags & (SP_END | SP_SUBPAT)) != 0
15210 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015211 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015212 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015213 goto theend;
15214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015215
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015216 /* Using 'r' implies 'W', otherwise it doesn't work. */
15217 if (flags & SP_REPEAT)
15218 p_ws = FALSE;
15219
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015220 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015221 if (argvars[3].v_type == VAR_UNKNOWN
15222 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223 skip = (char_u *)"";
15224 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015226 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015227 if (argvars[5].v_type != VAR_UNKNOWN)
15228 {
15229 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15230 if (lnum_stop < 0)
15231 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015232#ifdef FEAT_RELTIME
15233 if (argvars[6].v_type != VAR_UNKNOWN)
15234 {
15235 time_limit = get_tv_number_chk(&argvars[6], NULL);
15236 if (time_limit < 0)
15237 goto theend;
15238 }
15239#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015240 }
15241 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015242 if (skip == NULL)
15243 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015245 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015246 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015247
15248theend:
15249 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015250
15251 return retval;
15252}
15253
15254/*
15255 * "searchpair()" function
15256 */
15257 static void
15258f_searchpair(argvars, rettv)
15259 typval_T *argvars;
15260 typval_T *rettv;
15261{
15262 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15263}
15264
15265/*
15266 * "searchpairpos()" function
15267 */
15268 static void
15269f_searchpairpos(argvars, rettv)
15270 typval_T *argvars;
15271 typval_T *rettv;
15272{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015273 pos_T match_pos;
15274 int lnum = 0;
15275 int col = 0;
15276
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015277 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015278 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015279
15280 if (searchpair_cmn(argvars, &match_pos) > 0)
15281 {
15282 lnum = match_pos.lnum;
15283 col = match_pos.col;
15284 }
15285
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015286 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15287 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015288}
15289
15290/*
15291 * Search for a start/middle/end thing.
15292 * Used by searchpair(), see its documentation for the details.
15293 * Returns 0 or -1 for no match,
15294 */
15295 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015296do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15297 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015298 char_u *spat; /* start pattern */
15299 char_u *mpat; /* middle pattern */
15300 char_u *epat; /* end pattern */
15301 int dir; /* BACKWARD or FORWARD */
15302 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015303 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015304 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015305 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015306 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015307{
15308 char_u *save_cpo;
15309 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15310 long retval = 0;
15311 pos_T pos;
15312 pos_T firstpos;
15313 pos_T foundpos;
15314 pos_T save_cursor;
15315 pos_T save_pos;
15316 int n;
15317 int r;
15318 int nest = 1;
15319 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015320 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015321 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015322
15323 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15324 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015325 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015326
Bram Moolenaar76929292008-01-06 19:07:36 +000015327#ifdef FEAT_RELTIME
15328 /* Set the time limit, if there is one. */
15329 profile_setlimit(time_limit, &tm);
15330#endif
15331
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015332 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15333 * start/middle/end (pat3, for the top pair). */
15334 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15335 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15336 if (pat2 == NULL || pat3 == NULL)
15337 goto theend;
15338 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15339 if (*mpat == NUL)
15340 STRCPY(pat3, pat2);
15341 else
15342 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15343 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015344 if (flags & SP_START)
15345 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015346
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347 save_cursor = curwin->w_cursor;
15348 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015349 clearpos(&firstpos);
15350 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351 pat = pat3;
15352 for (;;)
15353 {
15354 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015355 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15357 /* didn't find it or found the first match again: FAIL */
15358 break;
15359
15360 if (firstpos.lnum == 0)
15361 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015362 if (equalpos(pos, foundpos))
15363 {
15364 /* Found the same position again. Can happen with a pattern that
15365 * has "\zs" at the end and searching backwards. Advance one
15366 * character and try again. */
15367 if (dir == BACKWARD)
15368 decl(&pos);
15369 else
15370 incl(&pos);
15371 }
15372 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015374 /* clear the start flag to avoid getting stuck here */
15375 options &= ~SEARCH_START;
15376
Bram Moolenaar071d4272004-06-13 20:20:40 +000015377 /* If the skip pattern matches, ignore this match. */
15378 if (*skip != NUL)
15379 {
15380 save_pos = curwin->w_cursor;
15381 curwin->w_cursor = pos;
15382 r = eval_to_bool(skip, &err, NULL, FALSE);
15383 curwin->w_cursor = save_pos;
15384 if (err)
15385 {
15386 /* Evaluating {skip} caused an error, break here. */
15387 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015388 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389 break;
15390 }
15391 if (r)
15392 continue;
15393 }
15394
15395 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15396 {
15397 /* Found end when searching backwards or start when searching
15398 * forward: nested pair. */
15399 ++nest;
15400 pat = pat2; /* nested, don't search for middle */
15401 }
15402 else
15403 {
15404 /* Found end when searching forward or start when searching
15405 * backward: end of (nested) pair; or found middle in outer pair. */
15406 if (--nest == 1)
15407 pat = pat3; /* outer level, search for middle */
15408 }
15409
15410 if (nest == 0)
15411 {
15412 /* Found the match: return matchcount or line number. */
15413 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015414 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015416 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015417 if (flags & SP_SETPCMARK)
15418 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419 curwin->w_cursor = pos;
15420 if (!(flags & SP_REPEAT))
15421 break;
15422 nest = 1; /* search for next unmatched */
15423 }
15424 }
15425
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015426 if (match_pos != NULL)
15427 {
15428 /* Store the match cursor position */
15429 match_pos->lnum = curwin->w_cursor.lnum;
15430 match_pos->col = curwin->w_cursor.col + 1;
15431 }
15432
Bram Moolenaar071d4272004-06-13 20:20:40 +000015433 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015434 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435 curwin->w_cursor = save_cursor;
15436
15437theend:
15438 vim_free(pat2);
15439 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015440 if (p_cpo == empty_option)
15441 p_cpo = save_cpo;
15442 else
15443 /* Darn, evaluating the {skip} expression changed the value. */
15444 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015445
15446 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447}
15448
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015449/*
15450 * "searchpos()" function
15451 */
15452 static void
15453f_searchpos(argvars, rettv)
15454 typval_T *argvars;
15455 typval_T *rettv;
15456{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015457 pos_T match_pos;
15458 int lnum = 0;
15459 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015460 int n;
15461 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015462
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015463 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015464 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015465
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015466 n = search_cmn(argvars, &match_pos, &flags);
15467 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015468 {
15469 lnum = match_pos.lnum;
15470 col = match_pos.col;
15471 }
15472
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015473 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15474 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015475 if (flags & SP_SUBPAT)
15476 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015477}
15478
15479
Bram Moolenaar0d660222005-01-07 21:51:51 +000015480 static void
15481f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015482 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015485#ifdef FEAT_CLIENTSERVER
15486 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015487 char_u *server = get_tv_string_chk(&argvars[0]);
15488 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489
Bram Moolenaar0d660222005-01-07 21:51:51 +000015490 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015491 if (server == NULL || reply == NULL)
15492 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015493 if (check_restricted() || check_secure())
15494 return;
15495# ifdef FEAT_X11
15496 if (check_connection() == FAIL)
15497 return;
15498# endif
15499
15500 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015502 EMSG(_("E258: Unable to send to client"));
15503 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015505 rettv->vval.v_number = 0;
15506#else
15507 rettv->vval.v_number = -1;
15508#endif
15509}
15510
Bram Moolenaar0d660222005-01-07 21:51:51 +000015511 static void
15512f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015513 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015514 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015515{
15516 char_u *r = NULL;
15517
15518#ifdef FEAT_CLIENTSERVER
15519# ifdef WIN32
15520 r = serverGetVimNames();
15521# else
15522 make_connection();
15523 if (X_DISPLAY != NULL)
15524 r = serverGetVimNames(X_DISPLAY);
15525# endif
15526#endif
15527 rettv->v_type = VAR_STRING;
15528 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529}
15530
15531/*
15532 * "setbufvar()" function
15533 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015535f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015536 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015537 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538{
15539 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015542 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543 char_u nbuf[NUMBUFLEN];
15544
15545 if (check_restricted() || check_secure())
15546 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015547 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15548 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015549 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550 varp = &argvars[2];
15551
15552 if (buf != NULL && varname != NULL && varp != NULL)
15553 {
15554 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556
15557 if (*varname == '&')
15558 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015559 long numval;
15560 char_u *strval;
15561 int error = FALSE;
15562
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015564 numval = get_tv_number_chk(varp, &error);
15565 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015566 if (!error && strval != NULL)
15567 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568 }
15569 else
15570 {
15571 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15572 if (bufvarname != NULL)
15573 {
15574 STRCPY(bufvarname, "b:");
15575 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015576 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577 vim_free(bufvarname);
15578 }
15579 }
15580
15581 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584}
15585
15586/*
15587 * "setcmdpos()" function
15588 */
15589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015590f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015591 typval_T *argvars;
15592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015594 int pos = (int)get_tv_number(&argvars[0]) - 1;
15595
15596 if (pos >= 0)
15597 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598}
15599
15600/*
15601 * "setline()" function
15602 */
15603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015604f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015605 typval_T *argvars;
15606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607{
15608 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015609 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015610 list_T *l = NULL;
15611 listitem_T *li = NULL;
15612 long added = 0;
15613 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015615 lnum = get_tv_lnum(&argvars[0]);
15616 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015618 l = argvars[1].vval.v_list;
15619 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015621 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015622 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015623
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015624 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015625 for (;;)
15626 {
15627 if (l != NULL)
15628 {
15629 /* list argument, get next string */
15630 if (li == NULL)
15631 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015632 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015633 li = li->li_next;
15634 }
15635
15636 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015637 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015638 break;
15639 if (lnum <= curbuf->b_ml.ml_line_count)
15640 {
15641 /* existing line, replace it */
15642 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15643 {
15644 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015645 if (lnum == curwin->w_cursor.lnum)
15646 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015647 rettv->vval.v_number = 0; /* OK */
15648 }
15649 }
15650 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15651 {
15652 /* lnum is one past the last line, append the line */
15653 ++added;
15654 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15655 rettv->vval.v_number = 0; /* OK */
15656 }
15657
15658 if (l == NULL) /* only one string argument */
15659 break;
15660 ++lnum;
15661 }
15662
15663 if (added > 0)
15664 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665}
15666
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015667static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15668
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015670 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015671 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015672 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015673set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015674 win_T *wp UNUSED;
15675 typval_T *list_arg UNUSED;
15676 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015677 typval_T *rettv;
15678{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015679#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015680 char_u *act;
15681 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015682#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015683
Bram Moolenaar2641f772005-03-25 21:58:17 +000015684 rettv->vval.v_number = -1;
15685
15686#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015687 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015688 EMSG(_(e_listreq));
15689 else
15690 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015691 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015692
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015693 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015694 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015695 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015696 if (act == NULL)
15697 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015698 if (*act == 'a' || *act == 'r')
15699 action = *act;
15700 }
15701
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015702 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015703 rettv->vval.v_number = 0;
15704 }
15705#endif
15706}
15707
15708/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015709 * "setloclist()" function
15710 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015711 static void
15712f_setloclist(argvars, rettv)
15713 typval_T *argvars;
15714 typval_T *rettv;
15715{
15716 win_T *win;
15717
15718 rettv->vval.v_number = -1;
15719
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015720 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015721 if (win != NULL)
15722 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15723}
15724
15725/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015726 * "setmatches()" function
15727 */
15728 static void
15729f_setmatches(argvars, rettv)
15730 typval_T *argvars;
15731 typval_T *rettv;
15732{
15733#ifdef FEAT_SEARCH_EXTRA
15734 list_T *l;
15735 listitem_T *li;
15736 dict_T *d;
15737
15738 rettv->vval.v_number = -1;
15739 if (argvars[0].v_type != VAR_LIST)
15740 {
15741 EMSG(_(e_listreq));
15742 return;
15743 }
15744 if ((l = argvars[0].vval.v_list) != NULL)
15745 {
15746
15747 /* To some extent make sure that we are dealing with a list from
15748 * "getmatches()". */
15749 li = l->lv_first;
15750 while (li != NULL)
15751 {
15752 if (li->li_tv.v_type != VAR_DICT
15753 || (d = li->li_tv.vval.v_dict) == NULL)
15754 {
15755 EMSG(_(e_invarg));
15756 return;
15757 }
15758 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15759 && dict_find(d, (char_u *)"pattern", -1) != NULL
15760 && dict_find(d, (char_u *)"priority", -1) != NULL
15761 && dict_find(d, (char_u *)"id", -1) != NULL))
15762 {
15763 EMSG(_(e_invarg));
15764 return;
15765 }
15766 li = li->li_next;
15767 }
15768
15769 clear_matches(curwin);
15770 li = l->lv_first;
15771 while (li != NULL)
15772 {
15773 d = li->li_tv.vval.v_dict;
15774 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15775 get_dict_string(d, (char_u *)"pattern", FALSE),
15776 (int)get_dict_number(d, (char_u *)"priority"),
15777 (int)get_dict_number(d, (char_u *)"id"));
15778 li = li->li_next;
15779 }
15780 rettv->vval.v_number = 0;
15781 }
15782#endif
15783}
15784
15785/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015786 * "setpos()" function
15787 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015788 static void
15789f_setpos(argvars, rettv)
15790 typval_T *argvars;
15791 typval_T *rettv;
15792{
15793 pos_T pos;
15794 int fnum;
15795 char_u *name;
15796
Bram Moolenaar08250432008-02-13 11:42:46 +000015797 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015798 name = get_tv_string_chk(argvars);
15799 if (name != NULL)
15800 {
15801 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15802 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015803 if (--pos.col < 0)
15804 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015805 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015806 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015807 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015808 if (fnum == curbuf->b_fnum)
15809 {
15810 curwin->w_cursor = pos;
15811 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015812 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015813 }
15814 else
15815 EMSG(_(e_invarg));
15816 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015817 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15818 {
15819 /* set mark */
15820 if (setmark_pos(name[1], &pos, fnum) == OK)
15821 rettv->vval.v_number = 0;
15822 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015823 else
15824 EMSG(_(e_invarg));
15825 }
15826 }
15827}
15828
15829/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015830 * "setqflist()" function
15831 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015832 static void
15833f_setqflist(argvars, rettv)
15834 typval_T *argvars;
15835 typval_T *rettv;
15836{
15837 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15838}
15839
15840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841 * "setreg()" function
15842 */
15843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015844f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015845 typval_T *argvars;
15846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015847{
15848 int regname;
15849 char_u *strregname;
15850 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015851 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852 int append;
15853 char_u yank_type;
15854 long block_len;
15855
15856 block_len = -1;
15857 yank_type = MAUTO;
15858 append = FALSE;
15859
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015860 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015861 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015863 if (strregname == NULL)
15864 return; /* type error; errmsg already given */
15865 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866 if (regname == 0 || regname == '@')
15867 regname = '"';
15868 else if (regname == '=')
15869 return;
15870
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015871 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015873 stropt = get_tv_string_chk(&argvars[2]);
15874 if (stropt == NULL)
15875 return; /* type error */
15876 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877 switch (*stropt)
15878 {
15879 case 'a': case 'A': /* append */
15880 append = TRUE;
15881 break;
15882 case 'v': case 'c': /* character-wise selection */
15883 yank_type = MCHAR;
15884 break;
15885 case 'V': case 'l': /* line-wise selection */
15886 yank_type = MLINE;
15887 break;
15888#ifdef FEAT_VISUAL
15889 case 'b': case Ctrl_V: /* block-wise selection */
15890 yank_type = MBLOCK;
15891 if (VIM_ISDIGIT(stropt[1]))
15892 {
15893 ++stropt;
15894 block_len = getdigits(&stropt) - 1;
15895 --stropt;
15896 }
15897 break;
15898#endif
15899 }
15900 }
15901
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015902 strval = get_tv_string_chk(&argvars[1]);
15903 if (strval != NULL)
15904 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015905 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015906 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907}
15908
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015909/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020015910 * "settabvar()" function
15911 */
15912 static void
15913f_settabvar(argvars, rettv)
15914 typval_T *argvars;
15915 typval_T *rettv;
15916{
15917 tabpage_T *save_curtab;
15918 char_u *varname, *tabvarname;
15919 typval_T *varp;
15920 tabpage_T *tp;
15921
15922 rettv->vval.v_number = 0;
15923
15924 if (check_restricted() || check_secure())
15925 return;
15926
15927 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15928 varname = get_tv_string_chk(&argvars[1]);
15929 varp = &argvars[2];
15930
15931 if (tp != NULL && varname != NULL && varp != NULL)
15932 {
15933 save_curtab = curtab;
15934 goto_tabpage_tp(tp);
15935
15936 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
15937 if (tabvarname != NULL)
15938 {
15939 STRCPY(tabvarname, "t:");
15940 STRCPY(tabvarname + 2, varname);
15941 set_var(tabvarname, varp, TRUE);
15942 vim_free(tabvarname);
15943 }
15944
15945 /* Restore current tabpage */
15946 if (valid_tabpage(save_curtab))
15947 goto_tabpage_tp(save_curtab);
15948 }
15949}
15950
15951/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015952 * "settabwinvar()" function
15953 */
15954 static void
15955f_settabwinvar(argvars, rettv)
15956 typval_T *argvars;
15957 typval_T *rettv;
15958{
15959 setwinvar(argvars, rettv, 1);
15960}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015961
15962/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015963 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015966f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015967 typval_T *argvars;
15968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015969{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015970 setwinvar(argvars, rettv, 0);
15971}
15972
15973/*
15974 * "setwinvar()" and "settabwinvar()" functions
15975 */
15976 static void
15977setwinvar(argvars, rettv, off)
15978 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015979 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015980 int off;
15981{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015982 win_T *win;
15983#ifdef FEAT_WINDOWS
15984 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015985 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986#endif
15987 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015988 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015989 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015990 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015991
15992 if (check_restricted() || check_secure())
15993 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015994
15995#ifdef FEAT_WINDOWS
15996 if (off == 1)
15997 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15998 else
15999 tp = curtab;
16000#endif
16001 win = find_win_by_nr(&argvars[off], tp);
16002 varname = get_tv_string_chk(&argvars[off + 1]);
16003 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004
16005 if (win != NULL && varname != NULL && varp != NULL)
16006 {
16007#ifdef FEAT_WINDOWS
16008 /* set curwin to be our win, temporarily */
16009 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016010 save_curtab = curtab;
16011 goto_tabpage_tp(tp);
16012 if (!win_valid(win))
16013 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016014 curwin = win;
16015 curbuf = curwin->w_buffer;
16016#endif
16017
16018 if (*varname == '&')
16019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016020 long numval;
16021 char_u *strval;
16022 int error = FALSE;
16023
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016025 numval = get_tv_number_chk(varp, &error);
16026 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016027 if (!error && strval != NULL)
16028 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029 }
16030 else
16031 {
16032 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16033 if (winvarname != NULL)
16034 {
16035 STRCPY(winvarname, "w:");
16036 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016037 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038 vim_free(winvarname);
16039 }
16040 }
16041
16042#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016043 /* Restore current tabpage and window, if still valid (autocomands can
16044 * make them invalid). */
16045 if (valid_tabpage(save_curtab))
16046 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047 if (win_valid(save_curwin))
16048 {
16049 curwin = save_curwin;
16050 curbuf = curwin->w_buffer;
16051 }
16052#endif
16053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054}
16055
16056/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016057 * "shellescape({string})" function
16058 */
16059 static void
16060f_shellescape(argvars, rettv)
16061 typval_T *argvars;
16062 typval_T *rettv;
16063{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016064 rettv->vval.v_string = vim_strsave_shellescape(
16065 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016066 rettv->v_type = VAR_STRING;
16067}
16068
16069/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016070 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071 */
16072 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016073f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016074 typval_T *argvars;
16075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016076{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016077 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078
Bram Moolenaar0d660222005-01-07 21:51:51 +000016079 p = get_tv_string(&argvars[0]);
16080 rettv->vval.v_string = vim_strsave(p);
16081 simplify_filename(rettv->vval.v_string); /* simplify in place */
16082 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083}
16084
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016085#ifdef FEAT_FLOAT
16086/*
16087 * "sin()" function
16088 */
16089 static void
16090f_sin(argvars, rettv)
16091 typval_T *argvars;
16092 typval_T *rettv;
16093{
16094 float_T f;
16095
16096 rettv->v_type = VAR_FLOAT;
16097 if (get_float_arg(argvars, &f) == OK)
16098 rettv->vval.v_float = sin(f);
16099 else
16100 rettv->vval.v_float = 0.0;
16101}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016102
16103/*
16104 * "sinh()" function
16105 */
16106 static void
16107f_sinh(argvars, rettv)
16108 typval_T *argvars;
16109 typval_T *rettv;
16110{
16111 float_T f;
16112
16113 rettv->v_type = VAR_FLOAT;
16114 if (get_float_arg(argvars, &f) == OK)
16115 rettv->vval.v_float = sinh(f);
16116 else
16117 rettv->vval.v_float = 0.0;
16118}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016119#endif
16120
Bram Moolenaar0d660222005-01-07 21:51:51 +000016121static int
16122#ifdef __BORLANDC__
16123 _RTLENTRYF
16124#endif
16125 item_compare __ARGS((const void *s1, const void *s2));
16126static int
16127#ifdef __BORLANDC__
16128 _RTLENTRYF
16129#endif
16130 item_compare2 __ARGS((const void *s1, const void *s2));
16131
16132static int item_compare_ic;
16133static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016134static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016135#define ITEM_COMPARE_FAIL 999
16136
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016138 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016139 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016140 static int
16141#ifdef __BORLANDC__
16142_RTLENTRYF
16143#endif
16144item_compare(s1, s2)
16145 const void *s1;
16146 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016148 char_u *p1, *p2;
16149 char_u *tofree1, *tofree2;
16150 int res;
16151 char_u numbuf1[NUMBUFLEN];
16152 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016154 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16155 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016156 if (p1 == NULL)
16157 p1 = (char_u *)"";
16158 if (p2 == NULL)
16159 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016160 if (item_compare_ic)
16161 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016163 res = STRCMP(p1, p2);
16164 vim_free(tofree1);
16165 vim_free(tofree2);
16166 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167}
16168
16169 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016170#ifdef __BORLANDC__
16171_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016173item_compare2(s1, s2)
16174 const void *s1;
16175 const void *s2;
16176{
16177 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016178 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016179 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016180 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016182 /* shortcut after failure in previous call; compare all items equal */
16183 if (item_compare_func_err)
16184 return 0;
16185
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016186 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16187 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016188 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16189 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016190
16191 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016192 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016193 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016194 clear_tv(&argv[0]);
16195 clear_tv(&argv[1]);
16196
16197 if (res == FAIL)
16198 res = ITEM_COMPARE_FAIL;
16199 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016200 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16201 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016202 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016203 clear_tv(&rettv);
16204 return res;
16205}
16206
16207/*
16208 * "sort({list})" function
16209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016211f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016212 typval_T *argvars;
16213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214{
Bram Moolenaar33570922005-01-25 22:26:29 +000016215 list_T *l;
16216 listitem_T *li;
16217 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016218 long len;
16219 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220
Bram Moolenaar0d660222005-01-07 21:51:51 +000016221 if (argvars[0].v_type != VAR_LIST)
16222 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223 else
16224 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016225 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016226 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016227 return;
16228 rettv->vval.v_list = l;
16229 rettv->v_type = VAR_LIST;
16230 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231
Bram Moolenaar0d660222005-01-07 21:51:51 +000016232 len = list_len(l);
16233 if (len <= 1)
16234 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235
Bram Moolenaar0d660222005-01-07 21:51:51 +000016236 item_compare_ic = FALSE;
16237 item_compare_func = NULL;
16238 if (argvars[1].v_type != VAR_UNKNOWN)
16239 {
16240 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016241 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016242 else
16243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016244 int error = FALSE;
16245
16246 i = get_tv_number_chk(&argvars[1], &error);
16247 if (error)
16248 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016249 if (i == 1)
16250 item_compare_ic = TRUE;
16251 else
16252 item_compare_func = get_tv_string(&argvars[1]);
16253 }
16254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255
Bram Moolenaar0d660222005-01-07 21:51:51 +000016256 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016257 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016258 if (ptrs == NULL)
16259 return;
16260 i = 0;
16261 for (li = l->lv_first; li != NULL; li = li->li_next)
16262 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016264 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016265 /* test the compare function */
16266 if (item_compare_func != NULL
16267 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16268 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016269 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016271 {
16272 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016273 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016274 item_compare_func == NULL ? item_compare : item_compare2);
16275
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016276 if (!item_compare_func_err)
16277 {
16278 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016279 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016280 l->lv_len = 0;
16281 for (i = 0; i < len; ++i)
16282 list_append(l, ptrs[i]);
16283 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016284 }
16285
16286 vim_free(ptrs);
16287 }
16288}
16289
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016290/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016291 * "soundfold({word})" function
16292 */
16293 static void
16294f_soundfold(argvars, rettv)
16295 typval_T *argvars;
16296 typval_T *rettv;
16297{
16298 char_u *s;
16299
16300 rettv->v_type = VAR_STRING;
16301 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016302#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016303 rettv->vval.v_string = eval_soundfold(s);
16304#else
16305 rettv->vval.v_string = vim_strsave(s);
16306#endif
16307}
16308
16309/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016310 * "spellbadword()" function
16311 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016312 static void
16313f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016314 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016315 typval_T *rettv;
16316{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016317 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016318 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016319 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016320
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016321 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016322 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016323
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016324#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016325 if (argvars[0].v_type == VAR_UNKNOWN)
16326 {
16327 /* Find the start and length of the badly spelled word. */
16328 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16329 if (len != 0)
16330 word = ml_get_cursor();
16331 }
16332 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16333 {
16334 char_u *str = get_tv_string_chk(&argvars[0]);
16335 int capcol = -1;
16336
16337 if (str != NULL)
16338 {
16339 /* Check the argument for spelling. */
16340 while (*str != NUL)
16341 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016342 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016343 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016344 {
16345 word = str;
16346 break;
16347 }
16348 str += len;
16349 }
16350 }
16351 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016352#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016353
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016354 list_append_string(rettv->vval.v_list, word, len);
16355 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016356 attr == HLF_SPB ? "bad" :
16357 attr == HLF_SPR ? "rare" :
16358 attr == HLF_SPL ? "local" :
16359 attr == HLF_SPC ? "caps" :
16360 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016361}
16362
16363/*
16364 * "spellsuggest()" function
16365 */
16366 static void
16367f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016368 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016369 typval_T *rettv;
16370{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016371#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016372 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016373 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016374 int maxcount;
16375 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016376 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016377 listitem_T *li;
16378 int need_capital = FALSE;
16379#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016380
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016381 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016382 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016383
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016384#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016385 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16386 {
16387 str = get_tv_string(&argvars[0]);
16388 if (argvars[1].v_type != VAR_UNKNOWN)
16389 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016390 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016391 if (maxcount <= 0)
16392 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016393 if (argvars[2].v_type != VAR_UNKNOWN)
16394 {
16395 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16396 if (typeerr)
16397 return;
16398 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016399 }
16400 else
16401 maxcount = 25;
16402
Bram Moolenaar4770d092006-01-12 23:22:24 +000016403 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016404
16405 for (i = 0; i < ga.ga_len; ++i)
16406 {
16407 str = ((char_u **)ga.ga_data)[i];
16408
16409 li = listitem_alloc();
16410 if (li == NULL)
16411 vim_free(str);
16412 else
16413 {
16414 li->li_tv.v_type = VAR_STRING;
16415 li->li_tv.v_lock = 0;
16416 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016417 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016418 }
16419 }
16420 ga_clear(&ga);
16421 }
16422#endif
16423}
16424
Bram Moolenaar0d660222005-01-07 21:51:51 +000016425 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016426f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016427 typval_T *argvars;
16428 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016429{
16430 char_u *str;
16431 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016432 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016433 regmatch_T regmatch;
16434 char_u patbuf[NUMBUFLEN];
16435 char_u *save_cpo;
16436 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016437 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016438 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016439 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016440
16441 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16442 save_cpo = p_cpo;
16443 p_cpo = (char_u *)"";
16444
16445 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016446 if (argvars[1].v_type != VAR_UNKNOWN)
16447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016448 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16449 if (pat == NULL)
16450 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016451 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016452 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016453 }
16454 if (pat == NULL || *pat == NUL)
16455 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016456
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016457 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016459 if (typeerr)
16460 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016461
Bram Moolenaar0d660222005-01-07 21:51:51 +000016462 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16463 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016465 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016466 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016467 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016468 if (*str == NUL)
16469 match = FALSE; /* empty item at the end */
16470 else
16471 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016472 if (match)
16473 end = regmatch.startp[0];
16474 else
16475 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016476 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16477 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016478 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016479 if (list_append_string(rettv->vval.v_list, str,
16480 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016481 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016482 }
16483 if (!match)
16484 break;
16485 /* Advance to just after the match. */
16486 if (regmatch.endp[0] > str)
16487 col = 0;
16488 else
16489 {
16490 /* Don't get stuck at the same match. */
16491#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016492 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016493#else
16494 col = 1;
16495#endif
16496 }
16497 str = regmatch.endp[0];
16498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499
Bram Moolenaar0d660222005-01-07 21:51:51 +000016500 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502
Bram Moolenaar0d660222005-01-07 21:51:51 +000016503 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016504}
16505
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016506#ifdef FEAT_FLOAT
16507/*
16508 * "sqrt()" function
16509 */
16510 static void
16511f_sqrt(argvars, rettv)
16512 typval_T *argvars;
16513 typval_T *rettv;
16514{
16515 float_T f;
16516
16517 rettv->v_type = VAR_FLOAT;
16518 if (get_float_arg(argvars, &f) == OK)
16519 rettv->vval.v_float = sqrt(f);
16520 else
16521 rettv->vval.v_float = 0.0;
16522}
16523
16524/*
16525 * "str2float()" function
16526 */
16527 static void
16528f_str2float(argvars, rettv)
16529 typval_T *argvars;
16530 typval_T *rettv;
16531{
16532 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16533
16534 if (*p == '+')
16535 p = skipwhite(p + 1);
16536 (void)string2float(p, &rettv->vval.v_float);
16537 rettv->v_type = VAR_FLOAT;
16538}
16539#endif
16540
Bram Moolenaar2c932302006-03-18 21:42:09 +000016541/*
16542 * "str2nr()" function
16543 */
16544 static void
16545f_str2nr(argvars, rettv)
16546 typval_T *argvars;
16547 typval_T *rettv;
16548{
16549 int base = 10;
16550 char_u *p;
16551 long n;
16552
16553 if (argvars[1].v_type != VAR_UNKNOWN)
16554 {
16555 base = get_tv_number(&argvars[1]);
16556 if (base != 8 && base != 10 && base != 16)
16557 {
16558 EMSG(_(e_invarg));
16559 return;
16560 }
16561 }
16562
16563 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016564 if (*p == '+')
16565 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016566 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16567 rettv->vval.v_number = n;
16568}
16569
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570#ifdef HAVE_STRFTIME
16571/*
16572 * "strftime({format}[, {time}])" function
16573 */
16574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016575f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016576 typval_T *argvars;
16577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578{
16579 char_u result_buf[256];
16580 struct tm *curtime;
16581 time_t seconds;
16582 char_u *p;
16583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016584 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016586 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016587 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588 seconds = time(NULL);
16589 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016590 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591 curtime = localtime(&seconds);
16592 /* MSVC returns NULL for an invalid value of seconds. */
16593 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016594 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595 else
16596 {
16597# ifdef FEAT_MBYTE
16598 vimconv_T conv;
16599 char_u *enc;
16600
16601 conv.vc_type = CONV_NONE;
16602 enc = enc_locale();
16603 convert_setup(&conv, p_enc, enc);
16604 if (conv.vc_type != CONV_NONE)
16605 p = string_convert(&conv, p, NULL);
16606# endif
16607 if (p != NULL)
16608 (void)strftime((char *)result_buf, sizeof(result_buf),
16609 (char *)p, curtime);
16610 else
16611 result_buf[0] = NUL;
16612
16613# ifdef FEAT_MBYTE
16614 if (conv.vc_type != CONV_NONE)
16615 vim_free(p);
16616 convert_setup(&conv, enc, p_enc);
16617 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016618 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619 else
16620# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016621 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622
16623# ifdef FEAT_MBYTE
16624 /* Release conversion descriptors */
16625 convert_setup(&conv, NULL, NULL);
16626 vim_free(enc);
16627# endif
16628 }
16629}
16630#endif
16631
16632/*
16633 * "stridx()" function
16634 */
16635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016636f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016637 typval_T *argvars;
16638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639{
16640 char_u buf[NUMBUFLEN];
16641 char_u *needle;
16642 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016643 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016645 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016647 needle = get_tv_string_chk(&argvars[1]);
16648 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016649 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016650 if (needle == NULL || haystack == NULL)
16651 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652
Bram Moolenaar33570922005-01-25 22:26:29 +000016653 if (argvars[2].v_type != VAR_UNKNOWN)
16654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016655 int error = FALSE;
16656
16657 start_idx = get_tv_number_chk(&argvars[2], &error);
16658 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016659 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016660 if (start_idx >= 0)
16661 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016662 }
16663
16664 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16665 if (pos != NULL)
16666 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667}
16668
16669/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016670 * "string()" function
16671 */
16672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016673f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016674 typval_T *argvars;
16675 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016676{
16677 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016678 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016680 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016681 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016682 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016683 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016684 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685}
16686
16687/*
16688 * "strlen()" function
16689 */
16690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016691f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016692 typval_T *argvars;
16693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016694{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016695 rettv->vval.v_number = (varnumber_T)(STRLEN(
16696 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697}
16698
16699/*
16700 * "strpart()" function
16701 */
16702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016703f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016704 typval_T *argvars;
16705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706{
16707 char_u *p;
16708 int n;
16709 int len;
16710 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016711 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016713 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714 slen = (int)STRLEN(p);
16715
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016716 n = get_tv_number_chk(&argvars[1], &error);
16717 if (error)
16718 len = 0;
16719 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016720 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 else
16722 len = slen - n; /* default len: all bytes that are available. */
16723
16724 /*
16725 * Only return the overlap between the specified part and the actual
16726 * string.
16727 */
16728 if (n < 0)
16729 {
16730 len += n;
16731 n = 0;
16732 }
16733 else if (n > slen)
16734 n = slen;
16735 if (len < 0)
16736 len = 0;
16737 else if (n + len > slen)
16738 len = slen - n;
16739
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016740 rettv->v_type = VAR_STRING;
16741 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016742}
16743
16744/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016745 * "strridx()" function
16746 */
16747 static void
16748f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016749 typval_T *argvars;
16750 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016751{
16752 char_u buf[NUMBUFLEN];
16753 char_u *needle;
16754 char_u *haystack;
16755 char_u *rest;
16756 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016757 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016758
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016759 needle = get_tv_string_chk(&argvars[1]);
16760 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016761
16762 rettv->vval.v_number = -1;
16763 if (needle == NULL || haystack == NULL)
16764 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016765
16766 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016767 if (argvars[2].v_type != VAR_UNKNOWN)
16768 {
16769 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016770 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016771 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016772 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016773 }
16774 else
16775 end_idx = haystack_len;
16776
Bram Moolenaar0d660222005-01-07 21:51:51 +000016777 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016778 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016779 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016780 lastmatch = haystack + end_idx;
16781 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016782 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016783 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016784 for (rest = haystack; *rest != '\0'; ++rest)
16785 {
16786 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016787 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016788 break;
16789 lastmatch = rest;
16790 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016791 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016792
16793 if (lastmatch == NULL)
16794 rettv->vval.v_number = -1;
16795 else
16796 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16797}
16798
16799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800 * "strtrans()" function
16801 */
16802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016803f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016804 typval_T *argvars;
16805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016807 rettv->v_type = VAR_STRING;
16808 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809}
16810
16811/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016812 * "submatch()" function
16813 */
16814 static void
16815f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016816 typval_T *argvars;
16817 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016818{
16819 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016820 rettv->vval.v_string =
16821 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016822}
16823
16824/*
16825 * "substitute()" function
16826 */
16827 static void
16828f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016829 typval_T *argvars;
16830 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016831{
16832 char_u patbuf[NUMBUFLEN];
16833 char_u subbuf[NUMBUFLEN];
16834 char_u flagsbuf[NUMBUFLEN];
16835
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016836 char_u *str = get_tv_string_chk(&argvars[0]);
16837 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16838 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16839 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16840
Bram Moolenaar0d660222005-01-07 21:51:51 +000016841 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016842 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16843 rettv->vval.v_string = NULL;
16844 else
16845 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016846}
16847
16848/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016849 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016852f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016853 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855{
16856 int id = 0;
16857#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016858 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016859 long col;
16860 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016861 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016863 lnum = get_tv_lnum(argvars); /* -1 on type error */
16864 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16865 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016867 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016868 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016869 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870#endif
16871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016872 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873}
16874
16875/*
16876 * "synIDattr(id, what [, mode])" function
16877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016879f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016880 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882{
16883 char_u *p = NULL;
16884#ifdef FEAT_SYN_HL
16885 int id;
16886 char_u *what;
16887 char_u *mode;
16888 char_u modebuf[NUMBUFLEN];
16889 int modec;
16890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016891 id = get_tv_number(&argvars[0]);
16892 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016893 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016895 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896 modec = TOLOWER_ASC(mode[0]);
16897 if (modec != 't' && modec != 'c'
16898#ifdef FEAT_GUI
16899 && modec != 'g'
16900#endif
16901 )
16902 modec = 0; /* replace invalid with current */
16903 }
16904 else
16905 {
16906#ifdef FEAT_GUI
16907 if (gui.in_use)
16908 modec = 'g';
16909 else
16910#endif
16911 if (t_colors > 1)
16912 modec = 'c';
16913 else
16914 modec = 't';
16915 }
16916
16917
16918 switch (TOLOWER_ASC(what[0]))
16919 {
16920 case 'b':
16921 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16922 p = highlight_color(id, what, modec);
16923 else /* bold */
16924 p = highlight_has_attr(id, HL_BOLD, modec);
16925 break;
16926
Bram Moolenaar12682fd2010-03-10 13:43:49 +010016927 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928 p = highlight_color(id, what, modec);
16929 break;
16930
16931 case 'i':
16932 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16933 p = highlight_has_attr(id, HL_INVERSE, modec);
16934 else /* italic */
16935 p = highlight_has_attr(id, HL_ITALIC, modec);
16936 break;
16937
16938 case 'n': /* name */
16939 p = get_highlight_name(NULL, id - 1);
16940 break;
16941
16942 case 'r': /* reverse */
16943 p = highlight_has_attr(id, HL_INVERSE, modec);
16944 break;
16945
Bram Moolenaar6f507d62008-11-28 10:16:05 +000016946 case 's':
16947 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
16948 p = highlight_color(id, what, modec);
16949 else /* standout */
16950 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951 break;
16952
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016953 case 'u':
16954 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16955 /* underline */
16956 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16957 else
16958 /* undercurl */
16959 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960 break;
16961 }
16962
16963 if (p != NULL)
16964 p = vim_strsave(p);
16965#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016966 rettv->v_type = VAR_STRING;
16967 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968}
16969
16970/*
16971 * "synIDtrans(id)" function
16972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016974f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016975 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977{
16978 int id;
16979
16980#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016981 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982
16983 if (id > 0)
16984 id = syn_get_final_id(id);
16985 else
16986#endif
16987 id = 0;
16988
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016989 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990}
16991
16992/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016993 * "synstack(lnum, col)" function
16994 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016995 static void
16996f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016997 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016998 typval_T *rettv;
16999{
17000#ifdef FEAT_SYN_HL
17001 long lnum;
17002 long col;
17003 int i;
17004 int id;
17005#endif
17006
17007 rettv->v_type = VAR_LIST;
17008 rettv->vval.v_list = NULL;
17009
17010#ifdef FEAT_SYN_HL
17011 lnum = get_tv_lnum(argvars); /* -1 on type error */
17012 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17013
17014 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar6cad8bd2008-09-10 13:39:10 +000017015 && col >= 0 && (col == 0 || col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017016 && rettv_list_alloc(rettv) != FAIL)
17017 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017018 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017019 for (i = 0; ; ++i)
17020 {
17021 id = syn_get_stack_item(i);
17022 if (id < 0)
17023 break;
17024 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17025 break;
17026 }
17027 }
17028#endif
17029}
17030
17031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017032 * "system()" function
17033 */
17034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017035f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017036 typval_T *argvars;
17037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017039 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017041 char_u *infile = NULL;
17042 char_u buf[NUMBUFLEN];
17043 int err = FALSE;
17044 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017046 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017047 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017048
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017049 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017050 {
17051 /*
17052 * Write the string to a temp file, to be used for input of the shell
17053 * command.
17054 */
17055 if ((infile = vim_tempname('i')) == NULL)
17056 {
17057 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017058 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017059 }
17060
17061 fd = mch_fopen((char *)infile, WRITEBIN);
17062 if (fd == NULL)
17063 {
17064 EMSG2(_(e_notopen), infile);
17065 goto done;
17066 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017067 p = get_tv_string_buf_chk(&argvars[1], buf);
17068 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017069 {
17070 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017071 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017072 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017073 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17074 err = TRUE;
17075 if (fclose(fd) != 0)
17076 err = TRUE;
17077 if (err)
17078 {
17079 EMSG(_("E677: Error writing temp file"));
17080 goto done;
17081 }
17082 }
17083
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017084 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17085 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017086
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087#ifdef USE_CR
17088 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017089 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090 {
17091 char_u *s;
17092
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017093 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094 {
17095 if (*s == CAR)
17096 *s = NL;
17097 }
17098 }
17099#else
17100# ifdef USE_CRNL
17101 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017102 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103 {
17104 char_u *s, *d;
17105
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017106 d = res;
17107 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017108 {
17109 if (s[0] == CAR && s[1] == NL)
17110 ++s;
17111 *d++ = *s;
17112 }
17113 *d = NUL;
17114 }
17115# endif
17116#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017117
17118done:
17119 if (infile != NULL)
17120 {
17121 mch_remove(infile);
17122 vim_free(infile);
17123 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017124 rettv->v_type = VAR_STRING;
17125 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126}
17127
17128/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017129 * "tabpagebuflist()" function
17130 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017131 static void
17132f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017133 typval_T *argvars UNUSED;
17134 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017135{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017136#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017137 tabpage_T *tp;
17138 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017139
17140 if (argvars[0].v_type == VAR_UNKNOWN)
17141 wp = firstwin;
17142 else
17143 {
17144 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17145 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017146 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017147 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017148 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017149 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017150 for (; wp != NULL; wp = wp->w_next)
17151 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017152 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017153 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017154 }
17155#endif
17156}
17157
17158
17159/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017160 * "tabpagenr()" function
17161 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017162 static void
17163f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017164 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017165 typval_T *rettv;
17166{
17167 int nr = 1;
17168#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017169 char_u *arg;
17170
17171 if (argvars[0].v_type != VAR_UNKNOWN)
17172 {
17173 arg = get_tv_string_chk(&argvars[0]);
17174 nr = 0;
17175 if (arg != NULL)
17176 {
17177 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017178 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017179 else
17180 EMSG2(_(e_invexpr2), arg);
17181 }
17182 }
17183 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017184 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017185#endif
17186 rettv->vval.v_number = nr;
17187}
17188
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017189
17190#ifdef FEAT_WINDOWS
17191static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17192
17193/*
17194 * Common code for tabpagewinnr() and winnr().
17195 */
17196 static int
17197get_winnr(tp, argvar)
17198 tabpage_T *tp;
17199 typval_T *argvar;
17200{
17201 win_T *twin;
17202 int nr = 1;
17203 win_T *wp;
17204 char_u *arg;
17205
17206 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17207 if (argvar->v_type != VAR_UNKNOWN)
17208 {
17209 arg = get_tv_string_chk(argvar);
17210 if (arg == NULL)
17211 nr = 0; /* type error; errmsg already given */
17212 else if (STRCMP(arg, "$") == 0)
17213 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17214 else if (STRCMP(arg, "#") == 0)
17215 {
17216 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17217 if (twin == NULL)
17218 nr = 0;
17219 }
17220 else
17221 {
17222 EMSG2(_(e_invexpr2), arg);
17223 nr = 0;
17224 }
17225 }
17226
17227 if (nr > 0)
17228 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17229 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017230 {
17231 if (wp == NULL)
17232 {
17233 /* didn't find it in this tabpage */
17234 nr = 0;
17235 break;
17236 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017237 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017238 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017239 return nr;
17240}
17241#endif
17242
17243/*
17244 * "tabpagewinnr()" function
17245 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017246 static void
17247f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017248 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017249 typval_T *rettv;
17250{
17251 int nr = 1;
17252#ifdef FEAT_WINDOWS
17253 tabpage_T *tp;
17254
17255 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17256 if (tp == NULL)
17257 nr = 0;
17258 else
17259 nr = get_winnr(tp, &argvars[1]);
17260#endif
17261 rettv->vval.v_number = nr;
17262}
17263
17264
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017265/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017266 * "tagfiles()" function
17267 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017268 static void
17269f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017270 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017271 typval_T *rettv;
17272{
17273 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017274 tagname_T tn;
17275 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017276
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017277 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017278 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017279
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017280 for (first = TRUE; ; first = FALSE)
17281 if (get_tagfname(&tn, first, fname) == FAIL
17282 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017283 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017284 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017285}
17286
17287/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017288 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017289 */
17290 static void
17291f_taglist(argvars, rettv)
17292 typval_T *argvars;
17293 typval_T *rettv;
17294{
17295 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017296
17297 tag_pattern = get_tv_string(&argvars[0]);
17298
17299 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017300 if (*tag_pattern == NUL)
17301 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017302
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017303 if (rettv_list_alloc(rettv) == OK)
17304 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017305}
17306
17307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308 * "tempname()" function
17309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017311f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017312 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314{
17315 static int x = 'A';
17316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017317 rettv->v_type = VAR_STRING;
17318 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319
17320 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17321 * names. Skip 'I' and 'O', they are used for shell redirection. */
17322 do
17323 {
17324 if (x == 'Z')
17325 x = '0';
17326 else if (x == '9')
17327 x = 'A';
17328 else
17329 {
17330#ifdef EBCDIC
17331 if (x == 'I')
17332 x = 'J';
17333 else if (x == 'R')
17334 x = 'S';
17335 else
17336#endif
17337 ++x;
17338 }
17339 } while (x == 'I' || x == 'O');
17340}
17341
17342/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017343 * "test(list)" function: Just checking the walls...
17344 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017345 static void
17346f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017347 typval_T *argvars UNUSED;
17348 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017349{
17350 /* Used for unit testing. Change the code below to your liking. */
17351#if 0
17352 listitem_T *li;
17353 list_T *l;
17354 char_u *bad, *good;
17355
17356 if (argvars[0].v_type != VAR_LIST)
17357 return;
17358 l = argvars[0].vval.v_list;
17359 if (l == NULL)
17360 return;
17361 li = l->lv_first;
17362 if (li == NULL)
17363 return;
17364 bad = get_tv_string(&li->li_tv);
17365 li = li->li_next;
17366 if (li == NULL)
17367 return;
17368 good = get_tv_string(&li->li_tv);
17369 rettv->vval.v_number = test_edit_score(bad, good);
17370#endif
17371}
17372
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017373#ifdef FEAT_FLOAT
17374/*
17375 * "tan()" function
17376 */
17377 static void
17378f_tan(argvars, rettv)
17379 typval_T *argvars;
17380 typval_T *rettv;
17381{
17382 float_T f;
17383
17384 rettv->v_type = VAR_FLOAT;
17385 if (get_float_arg(argvars, &f) == OK)
17386 rettv->vval.v_float = tan(f);
17387 else
17388 rettv->vval.v_float = 0.0;
17389}
17390
17391/*
17392 * "tanh()" function
17393 */
17394 static void
17395f_tanh(argvars, rettv)
17396 typval_T *argvars;
17397 typval_T *rettv;
17398{
17399 float_T f;
17400
17401 rettv->v_type = VAR_FLOAT;
17402 if (get_float_arg(argvars, &f) == OK)
17403 rettv->vval.v_float = tanh(f);
17404 else
17405 rettv->vval.v_float = 0.0;
17406}
17407#endif
17408
Bram Moolenaard52d9742005-08-21 22:20:28 +000017409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410 * "tolower(string)" function
17411 */
17412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017413f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017414 typval_T *argvars;
17415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416{
17417 char_u *p;
17418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017419 p = vim_strsave(get_tv_string(&argvars[0]));
17420 rettv->v_type = VAR_STRING;
17421 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422
17423 if (p != NULL)
17424 while (*p != NUL)
17425 {
17426#ifdef FEAT_MBYTE
17427 int l;
17428
17429 if (enc_utf8)
17430 {
17431 int c, lc;
17432
17433 c = utf_ptr2char(p);
17434 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017435 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436 /* TODO: reallocate string when byte count changes. */
17437 if (utf_char2len(lc) == l)
17438 utf_char2bytes(lc, p);
17439 p += l;
17440 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017441 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442 p += l; /* skip multi-byte character */
17443 else
17444#endif
17445 {
17446 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17447 ++p;
17448 }
17449 }
17450}
17451
17452/*
17453 * "toupper(string)" function
17454 */
17455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017456f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017457 typval_T *argvars;
17458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017460 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017461 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462}
17463
17464/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017465 * "tr(string, fromstr, tostr)" function
17466 */
17467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017468f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017469 typval_T *argvars;
17470 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017471{
17472 char_u *instr;
17473 char_u *fromstr;
17474 char_u *tostr;
17475 char_u *p;
17476#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017477 int inlen;
17478 int fromlen;
17479 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017480 int idx;
17481 char_u *cpstr;
17482 int cplen;
17483 int first = TRUE;
17484#endif
17485 char_u buf[NUMBUFLEN];
17486 char_u buf2[NUMBUFLEN];
17487 garray_T ga;
17488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017489 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017490 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17491 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017492
17493 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017494 rettv->v_type = VAR_STRING;
17495 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017496 if (fromstr == NULL || tostr == NULL)
17497 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017498 ga_init2(&ga, (int)sizeof(char), 80);
17499
17500#ifdef FEAT_MBYTE
17501 if (!has_mbyte)
17502#endif
17503 /* not multi-byte: fromstr and tostr must be the same length */
17504 if (STRLEN(fromstr) != STRLEN(tostr))
17505 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017506#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017507error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017508#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017509 EMSG2(_(e_invarg2), fromstr);
17510 ga_clear(&ga);
17511 return;
17512 }
17513
17514 /* fromstr and tostr have to contain the same number of chars */
17515 while (*instr != NUL)
17516 {
17517#ifdef FEAT_MBYTE
17518 if (has_mbyte)
17519 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017520 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017521 cpstr = instr;
17522 cplen = inlen;
17523 idx = 0;
17524 for (p = fromstr; *p != NUL; p += fromlen)
17525 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017526 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017527 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17528 {
17529 for (p = tostr; *p != NUL; p += tolen)
17530 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017531 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017532 if (idx-- == 0)
17533 {
17534 cplen = tolen;
17535 cpstr = p;
17536 break;
17537 }
17538 }
17539 if (*p == NUL) /* tostr is shorter than fromstr */
17540 goto error;
17541 break;
17542 }
17543 ++idx;
17544 }
17545
17546 if (first && cpstr == instr)
17547 {
17548 /* Check that fromstr and tostr have the same number of
17549 * (multi-byte) characters. Done only once when a character
17550 * of instr doesn't appear in fromstr. */
17551 first = FALSE;
17552 for (p = tostr; *p != NUL; p += tolen)
17553 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017554 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017555 --idx;
17556 }
17557 if (idx != 0)
17558 goto error;
17559 }
17560
17561 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017562 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017563 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017564
17565 instr += inlen;
17566 }
17567 else
17568#endif
17569 {
17570 /* When not using multi-byte chars we can do it faster. */
17571 p = vim_strchr(fromstr, *instr);
17572 if (p != NULL)
17573 ga_append(&ga, tostr[p - fromstr]);
17574 else
17575 ga_append(&ga, *instr);
17576 ++instr;
17577 }
17578 }
17579
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017580 /* add a terminating NUL */
17581 ga_grow(&ga, 1);
17582 ga_append(&ga, NUL);
17583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017584 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017585}
17586
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017587#ifdef FEAT_FLOAT
17588/*
17589 * "trunc({float})" function
17590 */
17591 static void
17592f_trunc(argvars, rettv)
17593 typval_T *argvars;
17594 typval_T *rettv;
17595{
17596 float_T f;
17597
17598 rettv->v_type = VAR_FLOAT;
17599 if (get_float_arg(argvars, &f) == OK)
17600 /* trunc() is not in C90, use floor() or ceil() instead. */
17601 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17602 else
17603 rettv->vval.v_float = 0.0;
17604}
17605#endif
17606
Bram Moolenaar8299df92004-07-10 09:47:34 +000017607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608 * "type(expr)" function
17609 */
17610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017611f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017612 typval_T *argvars;
17613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017615 int n;
17616
17617 switch (argvars[0].v_type)
17618 {
17619 case VAR_NUMBER: n = 0; break;
17620 case VAR_STRING: n = 1; break;
17621 case VAR_FUNC: n = 2; break;
17622 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017623 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017624#ifdef FEAT_FLOAT
17625 case VAR_FLOAT: n = 5; break;
17626#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017627 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17628 }
17629 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630}
17631
17632/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017633 * "undofile(name)" function
17634 */
17635 static void
17636f_undofile(argvars, rettv)
17637 typval_T *argvars;
17638 typval_T *rettv;
17639{
17640 rettv->v_type = VAR_STRING;
17641#ifdef FEAT_PERSISTENT_UNDO
17642 rettv->vval.v_string = u_get_undo_file_name(get_tv_string(&argvars[0]),
17643 FALSE);
17644#else
17645 rettv->vval.v_string = NULL;
17646#endif
17647}
17648
17649/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017650 * "values(dict)" function
17651 */
17652 static void
17653f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017654 typval_T *argvars;
17655 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017656{
17657 dict_list(argvars, rettv, 1);
17658}
17659
17660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 * "virtcol(string)" function
17662 */
17663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017664f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017665 typval_T *argvars;
17666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667{
17668 colnr_T vcol = 0;
17669 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017670 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017672 fp = var2fpos(&argvars[0], FALSE, &fnum);
17673 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17674 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 {
17676 getvvcol(curwin, fp, NULL, NULL, &vcol);
17677 ++vcol;
17678 }
17679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017680 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681}
17682
17683/*
17684 * "visualmode()" function
17685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017687f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017688 typval_T *argvars UNUSED;
17689 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690{
17691#ifdef FEAT_VISUAL
17692 char_u str[2];
17693
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017694 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695 str[0] = curbuf->b_visual_mode_eval;
17696 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017697 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698
17699 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017700 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702#endif
17703}
17704
17705/*
17706 * "winbufnr(nr)" function
17707 */
17708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017709f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017710 typval_T *argvars;
17711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712{
17713 win_T *wp;
17714
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017715 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017717 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017719 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720}
17721
17722/*
17723 * "wincol()" function
17724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017726f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017727 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017728 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729{
17730 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017731 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732}
17733
17734/*
17735 * "winheight(nr)" function
17736 */
17737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017738f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017739 typval_T *argvars;
17740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741{
17742 win_T *wp;
17743
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017744 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017745 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017746 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017748 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749}
17750
17751/*
17752 * "winline()" function
17753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017755f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017756 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758{
17759 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017760 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761}
17762
17763/*
17764 * "winnr()" function
17765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017767f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017768 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770{
17771 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017772
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017774 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017775#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017776 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777}
17778
17779/*
17780 * "winrestcmd()" function
17781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017783f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017784 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786{
17787#ifdef FEAT_WINDOWS
17788 win_T *wp;
17789 int winnr = 1;
17790 garray_T ga;
17791 char_u buf[50];
17792
17793 ga_init2(&ga, (int)sizeof(char), 70);
17794 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17795 {
17796 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17797 ga_concat(&ga, buf);
17798# ifdef FEAT_VERTSPLIT
17799 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17800 ga_concat(&ga, buf);
17801# endif
17802 ++winnr;
17803 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017804 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017806 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017808 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017809#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017810 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811}
17812
17813/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017814 * "winrestview()" function
17815 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017816 static void
17817f_winrestview(argvars, rettv)
17818 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017819 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017820{
17821 dict_T *dict;
17822
17823 if (argvars[0].v_type != VAR_DICT
17824 || (dict = argvars[0].vval.v_dict) == NULL)
17825 EMSG(_(e_invarg));
17826 else
17827 {
17828 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17829 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17830#ifdef FEAT_VIRTUALEDIT
17831 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17832#endif
17833 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017834 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017835
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017836 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017837#ifdef FEAT_DIFF
17838 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17839#endif
17840 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17841 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17842
17843 check_cursor();
17844 changed_cline_bef_curs();
17845 invalidate_botline();
17846 redraw_later(VALID);
17847
17848 if (curwin->w_topline == 0)
17849 curwin->w_topline = 1;
17850 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17851 curwin->w_topline = curbuf->b_ml.ml_line_count;
17852#ifdef FEAT_DIFF
17853 check_topfill(curwin, TRUE);
17854#endif
17855 }
17856}
17857
17858/*
17859 * "winsaveview()" function
17860 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017861 static void
17862f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017863 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017864 typval_T *rettv;
17865{
17866 dict_T *dict;
17867
17868 dict = dict_alloc();
17869 if (dict == NULL)
17870 return;
17871 rettv->v_type = VAR_DICT;
17872 rettv->vval.v_dict = dict;
17873 ++dict->dv_refcount;
17874
17875 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17876 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17877#ifdef FEAT_VIRTUALEDIT
17878 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17879#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017880 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017881 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17882
17883 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17884#ifdef FEAT_DIFF
17885 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17886#endif
17887 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17888 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17889}
17890
17891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 * "winwidth(nr)" function
17893 */
17894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017895f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017896 typval_T *argvars;
17897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898{
17899 win_T *wp;
17900
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017901 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017903 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904 else
17905#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017906 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017907#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017908 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909#endif
17910}
17911
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017913 * "writefile()" function
17914 */
17915 static void
17916f_writefile(argvars, rettv)
17917 typval_T *argvars;
17918 typval_T *rettv;
17919{
17920 int binary = FALSE;
17921 char_u *fname;
17922 FILE *fd;
17923 listitem_T *li;
17924 char_u *s;
17925 int ret = 0;
17926 int c;
17927
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017928 if (check_restricted() || check_secure())
17929 return;
17930
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017931 if (argvars[0].v_type != VAR_LIST)
17932 {
17933 EMSG2(_(e_listarg), "writefile()");
17934 return;
17935 }
17936 if (argvars[0].vval.v_list == NULL)
17937 return;
17938
17939 if (argvars[2].v_type != VAR_UNKNOWN
17940 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17941 binary = TRUE;
17942
17943 /* Always open the file in binary mode, library functions have a mind of
17944 * their own about CR-LF conversion. */
17945 fname = get_tv_string(&argvars[1]);
17946 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17947 {
17948 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17949 ret = -1;
17950 }
17951 else
17952 {
17953 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17954 li = li->li_next)
17955 {
17956 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17957 {
17958 if (*s == '\n')
17959 c = putc(NUL, fd);
17960 else
17961 c = putc(*s, fd);
17962 if (c == EOF)
17963 {
17964 ret = -1;
17965 break;
17966 }
17967 }
17968 if (!binary || li->li_next != NULL)
17969 if (putc('\n', fd) == EOF)
17970 {
17971 ret = -1;
17972 break;
17973 }
17974 if (ret < 0)
17975 {
17976 EMSG(_(e_write));
17977 break;
17978 }
17979 }
17980 fclose(fd);
17981 }
17982
17983 rettv->vval.v_number = ret;
17984}
17985
17986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017988 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989 */
17990 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017991var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017992 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017993 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017994 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017996 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017998 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017999
Bram Moolenaara5525202006-03-02 22:52:09 +000018000 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018001 if (varp->v_type == VAR_LIST)
18002 {
18003 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018004 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018005 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018006 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018007
18008 l = varp->vval.v_list;
18009 if (l == NULL)
18010 return NULL;
18011
18012 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018013 pos.lnum = list_find_nr(l, 0L, &error);
18014 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018015 return NULL; /* invalid line number */
18016
18017 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018018 pos.col = list_find_nr(l, 1L, &error);
18019 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018020 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018021 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018022
18023 /* We accept "$" for the column number: last column. */
18024 li = list_find(l, 1L);
18025 if (li != NULL && li->li_tv.v_type == VAR_STRING
18026 && li->li_tv.vval.v_string != NULL
18027 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18028 pos.col = len + 1;
18029
Bram Moolenaara5525202006-03-02 22:52:09 +000018030 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018031 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018032 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018033 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018034
Bram Moolenaara5525202006-03-02 22:52:09 +000018035#ifdef FEAT_VIRTUALEDIT
18036 /* Get the virtual offset. Defaults to zero. */
18037 pos.coladd = list_find_nr(l, 2L, &error);
18038 if (error)
18039 pos.coladd = 0;
18040#endif
18041
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018042 return &pos;
18043 }
18044
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018045 name = get_tv_string_chk(varp);
18046 if (name == NULL)
18047 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018048 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018050#ifdef FEAT_VISUAL
18051 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18052 {
18053 if (VIsual_active)
18054 return &VIsual;
18055 return &curwin->w_cursor;
18056 }
18057#endif
18058 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018060 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18062 return NULL;
18063 return pp;
18064 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018065
18066#ifdef FEAT_VIRTUALEDIT
18067 pos.coladd = 0;
18068#endif
18069
Bram Moolenaar477933c2007-07-17 14:32:23 +000018070 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018071 {
18072 pos.col = 0;
18073 if (name[1] == '0') /* "w0": first visible line */
18074 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018075 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018076 pos.lnum = curwin->w_topline;
18077 return &pos;
18078 }
18079 else if (name[1] == '$') /* "w$": last visible line */
18080 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018081 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018082 pos.lnum = curwin->w_botline - 1;
18083 return &pos;
18084 }
18085 }
18086 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018088 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089 {
18090 pos.lnum = curbuf->b_ml.ml_line_count;
18091 pos.col = 0;
18092 }
18093 else
18094 {
18095 pos.lnum = curwin->w_cursor.lnum;
18096 pos.col = (colnr_T)STRLEN(ml_get_curline());
18097 }
18098 return &pos;
18099 }
18100 return NULL;
18101}
18102
18103/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018104 * Convert list in "arg" into a position and optional file number.
18105 * When "fnump" is NULL there is no file number, only 3 items.
18106 * Note that the column is passed on as-is, the caller may want to decrement
18107 * it to use 1 for the first column.
18108 * Return FAIL when conversion is not possible, doesn't check the position for
18109 * validity.
18110 */
18111 static int
18112list2fpos(arg, posp, fnump)
18113 typval_T *arg;
18114 pos_T *posp;
18115 int *fnump;
18116{
18117 list_T *l = arg->vval.v_list;
18118 long i = 0;
18119 long n;
18120
Bram Moolenaarbde35262006-07-23 20:12:24 +000018121 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18122 * when "fnump" isn't NULL and "coladd" is optional. */
18123 if (arg->v_type != VAR_LIST
18124 || l == NULL
18125 || l->lv_len < (fnump == NULL ? 2 : 3)
18126 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018127 return FAIL;
18128
18129 if (fnump != NULL)
18130 {
18131 n = list_find_nr(l, i++, NULL); /* fnum */
18132 if (n < 0)
18133 return FAIL;
18134 if (n == 0)
18135 n = curbuf->b_fnum; /* current buffer */
18136 *fnump = n;
18137 }
18138
18139 n = list_find_nr(l, i++, NULL); /* lnum */
18140 if (n < 0)
18141 return FAIL;
18142 posp->lnum = n;
18143
18144 n = list_find_nr(l, i++, NULL); /* col */
18145 if (n < 0)
18146 return FAIL;
18147 posp->col = n;
18148
18149#ifdef FEAT_VIRTUALEDIT
18150 n = list_find_nr(l, i, NULL);
18151 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018152 posp->coladd = 0;
18153 else
18154 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018155#endif
18156
18157 return OK;
18158}
18159
18160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 * Get the length of an environment variable name.
18162 * Advance "arg" to the first character after the name.
18163 * Return 0 for error.
18164 */
18165 static int
18166get_env_len(arg)
18167 char_u **arg;
18168{
18169 char_u *p;
18170 int len;
18171
18172 for (p = *arg; vim_isIDc(*p); ++p)
18173 ;
18174 if (p == *arg) /* no name found */
18175 return 0;
18176
18177 len = (int)(p - *arg);
18178 *arg = p;
18179 return len;
18180}
18181
18182/*
18183 * Get the length of the name of a function or internal variable.
18184 * "arg" is advanced to the first non-white character after the name.
18185 * Return 0 if something is wrong.
18186 */
18187 static int
18188get_id_len(arg)
18189 char_u **arg;
18190{
18191 char_u *p;
18192 int len;
18193
18194 /* Find the end of the name. */
18195 for (p = *arg; eval_isnamec(*p); ++p)
18196 ;
18197 if (p == *arg) /* no name found */
18198 return 0;
18199
18200 len = (int)(p - *arg);
18201 *arg = skipwhite(p);
18202
18203 return len;
18204}
18205
18206/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018207 * Get the length of the name of a variable or function.
18208 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018210 * Return -1 if curly braces expansion failed.
18211 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 * If the name contains 'magic' {}'s, expand them and return the
18213 * expanded name in an allocated string via 'alias' - caller must free.
18214 */
18215 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018216get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 char_u **arg;
18218 char_u **alias;
18219 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018220 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221{
18222 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223 char_u *p;
18224 char_u *expr_start;
18225 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226
18227 *alias = NULL; /* default to no alias */
18228
18229 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18230 && (*arg)[2] == (int)KE_SNR)
18231 {
18232 /* hard coded <SNR>, already translated */
18233 *arg += 3;
18234 return get_id_len(arg) + 3;
18235 }
18236 len = eval_fname_script(*arg);
18237 if (len > 0)
18238 {
18239 /* literal "<SID>", "s:" or "<SNR>" */
18240 *arg += len;
18241 }
18242
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018244 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018246 p = find_name_end(*arg, &expr_start, &expr_end,
18247 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248 if (expr_start != NULL)
18249 {
18250 char_u *temp_string;
18251
18252 if (!evaluate)
18253 {
18254 len += (int)(p - *arg);
18255 *arg = skipwhite(p);
18256 return len;
18257 }
18258
18259 /*
18260 * Include any <SID> etc in the expanded string:
18261 * Thus the -len here.
18262 */
18263 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18264 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018265 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266 *alias = temp_string;
18267 *arg = skipwhite(p);
18268 return (int)STRLEN(temp_string);
18269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270
18271 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018272 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273 EMSG2(_(e_invexpr2), *arg);
18274
18275 return len;
18276}
18277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018278/*
18279 * Find the end of a variable or function name, taking care of magic braces.
18280 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18281 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018282 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018283 * Return a pointer to just after the name. Equal to "arg" if there is no
18284 * valid name.
18285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018287find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288 char_u *arg;
18289 char_u **expr_start;
18290 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018291 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018293 int mb_nest = 0;
18294 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295 char_u *p;
18296
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018297 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018299 *expr_start = NULL;
18300 *expr_end = NULL;
18301 }
18302
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018303 /* Quick check for valid starting character. */
18304 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18305 return arg;
18306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018307 for (p = arg; *p != NUL
18308 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018309 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018310 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018311 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018312 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018313 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018314 if (*p == '\'')
18315 {
18316 /* skip over 'string' to avoid counting [ and ] inside it. */
18317 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18318 ;
18319 if (*p == NUL)
18320 break;
18321 }
18322 else if (*p == '"')
18323 {
18324 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18325 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18326 if (*p == '\\' && p[1] != NUL)
18327 ++p;
18328 if (*p == NUL)
18329 break;
18330 }
18331
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018332 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018334 if (*p == '[')
18335 ++br_nest;
18336 else if (*p == ']')
18337 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018340 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018342 if (*p == '{')
18343 {
18344 mb_nest++;
18345 if (expr_start != NULL && *expr_start == NULL)
18346 *expr_start = p;
18347 }
18348 else if (*p == '}')
18349 {
18350 mb_nest--;
18351 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18352 *expr_end = p;
18353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355 }
18356
18357 return p;
18358}
18359
18360/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018361 * Expands out the 'magic' {}'s in a variable/function name.
18362 * Note that this can call itself recursively, to deal with
18363 * constructs like foo{bar}{baz}{bam}
18364 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18365 * "in_start" ^
18366 * "expr_start" ^
18367 * "expr_end" ^
18368 * "in_end" ^
18369 *
18370 * Returns a new allocated string, which the caller must free.
18371 * Returns NULL for failure.
18372 */
18373 static char_u *
18374make_expanded_name(in_start, expr_start, expr_end, in_end)
18375 char_u *in_start;
18376 char_u *expr_start;
18377 char_u *expr_end;
18378 char_u *in_end;
18379{
18380 char_u c1;
18381 char_u *retval = NULL;
18382 char_u *temp_result;
18383 char_u *nextcmd = NULL;
18384
18385 if (expr_end == NULL || in_end == NULL)
18386 return NULL;
18387 *expr_start = NUL;
18388 *expr_end = NUL;
18389 c1 = *in_end;
18390 *in_end = NUL;
18391
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018392 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018393 if (temp_result != NULL && nextcmd == NULL)
18394 {
18395 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18396 + (in_end - expr_end) + 1));
18397 if (retval != NULL)
18398 {
18399 STRCPY(retval, in_start);
18400 STRCAT(retval, temp_result);
18401 STRCAT(retval, expr_end + 1);
18402 }
18403 }
18404 vim_free(temp_result);
18405
18406 *in_end = c1; /* put char back for error messages */
18407 *expr_start = '{';
18408 *expr_end = '}';
18409
18410 if (retval != NULL)
18411 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018412 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018413 if (expr_start != NULL)
18414 {
18415 /* Further expansion! */
18416 temp_result = make_expanded_name(retval, expr_start,
18417 expr_end, temp_result);
18418 vim_free(retval);
18419 retval = temp_result;
18420 }
18421 }
18422
18423 return retval;
18424}
18425
18426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018428 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429 */
18430 static int
18431eval_isnamec(c)
18432 int c;
18433{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018434 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18435}
18436
18437/*
18438 * Return TRUE if character "c" can be used as the first character in a
18439 * variable or function name (excluding '{' and '}').
18440 */
18441 static int
18442eval_isnamec1(c)
18443 int c;
18444{
18445 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446}
18447
18448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449 * Set number v: variable to "val".
18450 */
18451 void
18452set_vim_var_nr(idx, val)
18453 int idx;
18454 long val;
18455{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018456 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457}
18458
18459/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018460 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461 */
18462 long
18463get_vim_var_nr(idx)
18464 int idx;
18465{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018466 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018467}
18468
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018469/*
18470 * Get string v: variable value. Uses a static buffer, can only be used once.
18471 */
18472 char_u *
18473get_vim_var_str(idx)
18474 int idx;
18475{
18476 return get_tv_string(&vimvars[idx].vv_tv);
18477}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018478
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018480 * Get List v: variable value. Caller must take care of reference count when
18481 * needed.
18482 */
18483 list_T *
18484get_vim_var_list(idx)
18485 int idx;
18486{
18487 return vimvars[idx].vv_list;
18488}
18489
18490/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018491 * Set v:char to character "c".
18492 */
18493 void
18494set_vim_var_char(c)
18495 int c;
18496{
18497#ifdef FEAT_MBYTE
18498 char_u buf[MB_MAXBYTES];
18499#else
18500 char_u buf[2];
18501#endif
18502
18503#ifdef FEAT_MBYTE
18504 if (has_mbyte)
18505 buf[(*mb_char2bytes)(c, buf)] = NUL;
18506 else
18507#endif
18508 {
18509 buf[0] = c;
18510 buf[1] = NUL;
18511 }
18512 set_vim_var_string(VV_CHAR, buf, -1);
18513}
18514
18515/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018516 * Set v:count to "count" and v:count1 to "count1".
18517 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 */
18519 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018520set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521 long count;
18522 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018523 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018525 if (set_prevcount)
18526 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018527 vimvars[VV_COUNT].vv_nr = count;
18528 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529}
18530
18531/*
18532 * Set string v: variable to a copy of "val".
18533 */
18534 void
18535set_vim_var_string(idx, val, len)
18536 int idx;
18537 char_u *val;
18538 int len; /* length of "val" to use or -1 (whole string) */
18539{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018540 /* Need to do this (at least) once, since we can't initialize a union.
18541 * Will always be invoked when "v:progname" is set. */
18542 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18543
Bram Moolenaare9a41262005-01-15 22:18:47 +000018544 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018546 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018548 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018550 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551}
18552
18553/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018554 * Set List v: variable to "val".
18555 */
18556 void
18557set_vim_var_list(idx, val)
18558 int idx;
18559 list_T *val;
18560{
18561 list_unref(vimvars[idx].vv_list);
18562 vimvars[idx].vv_list = val;
18563 if (val != NULL)
18564 ++val->lv_refcount;
18565}
18566
18567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 * Set v:register if needed.
18569 */
18570 void
18571set_reg_var(c)
18572 int c;
18573{
18574 char_u regname;
18575
18576 if (c == 0 || c == ' ')
18577 regname = '"';
18578 else
18579 regname = c;
18580 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018581 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 set_vim_var_string(VV_REG, &regname, 1);
18583}
18584
18585/*
18586 * Get or set v:exception. If "oldval" == NULL, return the current value.
18587 * Otherwise, restore the value to "oldval" and return NULL.
18588 * Must always be called in pairs to save and restore v:exception! Does not
18589 * take care of memory allocations.
18590 */
18591 char_u *
18592v_exception(oldval)
18593 char_u *oldval;
18594{
18595 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018596 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597
Bram Moolenaare9a41262005-01-15 22:18:47 +000018598 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018599 return NULL;
18600}
18601
18602/*
18603 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18604 * Otherwise, restore the value to "oldval" and return NULL.
18605 * Must always be called in pairs to save and restore v:throwpoint! Does not
18606 * take care of memory allocations.
18607 */
18608 char_u *
18609v_throwpoint(oldval)
18610 char_u *oldval;
18611{
18612 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018613 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614
Bram Moolenaare9a41262005-01-15 22:18:47 +000018615 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 return NULL;
18617}
18618
18619#if defined(FEAT_AUTOCMD) || defined(PROTO)
18620/*
18621 * Set v:cmdarg.
18622 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18623 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18624 * Must always be called in pairs!
18625 */
18626 char_u *
18627set_cmdarg(eap, oldarg)
18628 exarg_T *eap;
18629 char_u *oldarg;
18630{
18631 char_u *oldval;
18632 char_u *newval;
18633 unsigned len;
18634
Bram Moolenaare9a41262005-01-15 22:18:47 +000018635 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018636 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018638 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018639 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018640 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641 }
18642
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018643 if (eap->force_bin == FORCE_BIN)
18644 len = 6;
18645 else if (eap->force_bin == FORCE_NOBIN)
18646 len = 8;
18647 else
18648 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018649
18650 if (eap->read_edit)
18651 len += 7;
18652
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018653 if (eap->force_ff != 0)
18654 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18655# ifdef FEAT_MBYTE
18656 if (eap->force_enc != 0)
18657 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018658 if (eap->bad_char != 0)
18659 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018660# endif
18661
18662 newval = alloc(len + 1);
18663 if (newval == NULL)
18664 return NULL;
18665
18666 if (eap->force_bin == FORCE_BIN)
18667 sprintf((char *)newval, " ++bin");
18668 else if (eap->force_bin == FORCE_NOBIN)
18669 sprintf((char *)newval, " ++nobin");
18670 else
18671 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018672
18673 if (eap->read_edit)
18674 STRCAT(newval, " ++edit");
18675
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018676 if (eap->force_ff != 0)
18677 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18678 eap->cmd + eap->force_ff);
18679# ifdef FEAT_MBYTE
18680 if (eap->force_enc != 0)
18681 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18682 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018683 if (eap->bad_char == BAD_KEEP)
18684 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18685 else if (eap->bad_char == BAD_DROP)
18686 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18687 else if (eap->bad_char != 0)
18688 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018689# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018690 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018691 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018692}
18693#endif
18694
18695/*
18696 * Get the value of internal variable "name".
18697 * Return OK or FAIL.
18698 */
18699 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018700get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018701 char_u *name;
18702 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018703 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018704 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705{
18706 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018707 typval_T *tv = NULL;
18708 typval_T atv;
18709 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018710 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711
18712 /* truncate the name, so that we can use strcmp() */
18713 cc = name[len];
18714 name[len] = NUL;
18715
18716 /*
18717 * Check for "b:changedtick".
18718 */
18719 if (STRCMP(name, "b:changedtick") == 0)
18720 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018721 atv.v_type = VAR_NUMBER;
18722 atv.vval.v_number = curbuf->b_changedtick;
18723 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724 }
18725
18726 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727 * Check for user-defined variables.
18728 */
18729 else
18730 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018731 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018733 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734 }
18735
Bram Moolenaare9a41262005-01-15 22:18:47 +000018736 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018738 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018739 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740 ret = FAIL;
18741 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018742 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018743 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744
18745 name[len] = cc;
18746
18747 return ret;
18748}
18749
18750/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018751 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18752 * Also handle function call with Funcref variable: func(expr)
18753 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18754 */
18755 static int
18756handle_subscript(arg, rettv, evaluate, verbose)
18757 char_u **arg;
18758 typval_T *rettv;
18759 int evaluate; /* do more than finding the end */
18760 int verbose; /* give error messages */
18761{
18762 int ret = OK;
18763 dict_T *selfdict = NULL;
18764 char_u *s;
18765 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018766 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018767
18768 while (ret == OK
18769 && (**arg == '['
18770 || (**arg == '.' && rettv->v_type == VAR_DICT)
18771 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18772 && !vim_iswhite(*(*arg - 1)))
18773 {
18774 if (**arg == '(')
18775 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018776 /* need to copy the funcref so that we can clear rettv */
18777 functv = *rettv;
18778 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018779
18780 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018781 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018782 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018783 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18784 &len, evaluate, selfdict);
18785
18786 /* Clear the funcref afterwards, so that deleting it while
18787 * evaluating the arguments is possible (see test55). */
18788 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018789
18790 /* Stop the expression evaluation when immediately aborting on
18791 * error, or when an interrupt occurred or an exception was thrown
18792 * but not caught. */
18793 if (aborting())
18794 {
18795 if (ret == OK)
18796 clear_tv(rettv);
18797 ret = FAIL;
18798 }
18799 dict_unref(selfdict);
18800 selfdict = NULL;
18801 }
18802 else /* **arg == '[' || **arg == '.' */
18803 {
18804 dict_unref(selfdict);
18805 if (rettv->v_type == VAR_DICT)
18806 {
18807 selfdict = rettv->vval.v_dict;
18808 if (selfdict != NULL)
18809 ++selfdict->dv_refcount;
18810 }
18811 else
18812 selfdict = NULL;
18813 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18814 {
18815 clear_tv(rettv);
18816 ret = FAIL;
18817 }
18818 }
18819 }
18820 dict_unref(selfdict);
18821 return ret;
18822}
18823
18824/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018825 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018826 * value).
18827 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018828 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018829alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018830{
Bram Moolenaar33570922005-01-25 22:26:29 +000018831 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018832}
18833
18834/*
18835 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 * The string "s" must have been allocated, it is consumed.
18837 * Return NULL for out of memory, the variable otherwise.
18838 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018839 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018840alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841 char_u *s;
18842{
Bram Moolenaar33570922005-01-25 22:26:29 +000018843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018845 rettv = alloc_tv();
18846 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018848 rettv->v_type = VAR_STRING;
18849 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850 }
18851 else
18852 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018853 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854}
18855
18856/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018857 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018859 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018860free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018861 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018862{
18863 if (varp != NULL)
18864 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018865 switch (varp->v_type)
18866 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018867 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018868 func_unref(varp->vval.v_string);
18869 /*FALLTHROUGH*/
18870 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018871 vim_free(varp->vval.v_string);
18872 break;
18873 case VAR_LIST:
18874 list_unref(varp->vval.v_list);
18875 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018876 case VAR_DICT:
18877 dict_unref(varp->vval.v_dict);
18878 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018879 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018880#ifdef FEAT_FLOAT
18881 case VAR_FLOAT:
18882#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018883 case VAR_UNKNOWN:
18884 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018885 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018886 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018887 break;
18888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889 vim_free(varp);
18890 }
18891}
18892
18893/*
18894 * Free the memory for a variable value and set the value to NULL or 0.
18895 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018896 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018897clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018898 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899{
18900 if (varp != NULL)
18901 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018902 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018904 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018905 func_unref(varp->vval.v_string);
18906 /*FALLTHROUGH*/
18907 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018908 vim_free(varp->vval.v_string);
18909 varp->vval.v_string = NULL;
18910 break;
18911 case VAR_LIST:
18912 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018913 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018914 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018915 case VAR_DICT:
18916 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018917 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018918 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018919 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018920 varp->vval.v_number = 0;
18921 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018922#ifdef FEAT_FLOAT
18923 case VAR_FLOAT:
18924 varp->vval.v_float = 0.0;
18925 break;
18926#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018927 case VAR_UNKNOWN:
18928 break;
18929 default:
18930 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018932 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933 }
18934}
18935
18936/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018937 * Set the value of a variable to NULL without freeing items.
18938 */
18939 static void
18940init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018941 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018942{
18943 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018944 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018945}
18946
18947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948 * Get the number value of a variable.
18949 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018950 * For incompatible types, return 0.
18951 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18952 * caller of incompatible types: it sets *denote to TRUE if "denote"
18953 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954 */
18955 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018956get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018957 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018959 int error = FALSE;
18960
18961 return get_tv_number_chk(varp, &error); /* return 0L on error */
18962}
18963
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018964 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018965get_tv_number_chk(varp, denote)
18966 typval_T *varp;
18967 int *denote;
18968{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018969 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018971 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018973 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018974 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018975#ifdef FEAT_FLOAT
18976 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018977 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018978 break;
18979#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018980 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018981 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018982 break;
18983 case VAR_STRING:
18984 if (varp->vval.v_string != NULL)
18985 vim_str2nr(varp->vval.v_string, NULL, NULL,
18986 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018987 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018988 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018989 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018990 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018991 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018992 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018993 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018994 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018995 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018996 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018997 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018998 if (denote == NULL) /* useful for values that must be unsigned */
18999 n = -1;
19000 else
19001 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019002 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003}
19004
19005/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019006 * Get the lnum from the first argument.
19007 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019008 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009 */
19010 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019011get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019012 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013{
Bram Moolenaar33570922005-01-25 22:26:29 +000019014 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 linenr_T lnum;
19016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019017 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018 if (lnum == 0) /* no valid number, try using line() */
19019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019020 rettv.v_type = VAR_NUMBER;
19021 f_line(argvars, &rettv);
19022 lnum = rettv.vval.v_number;
19023 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024 }
19025 return lnum;
19026}
19027
19028/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019029 * Get the lnum from the first argument.
19030 * Also accepts "$", then "buf" is used.
19031 * Returns 0 on error.
19032 */
19033 static linenr_T
19034get_tv_lnum_buf(argvars, buf)
19035 typval_T *argvars;
19036 buf_T *buf;
19037{
19038 if (argvars[0].v_type == VAR_STRING
19039 && argvars[0].vval.v_string != NULL
19040 && argvars[0].vval.v_string[0] == '$'
19041 && buf != NULL)
19042 return buf->b_ml.ml_line_count;
19043 return get_tv_number_chk(&argvars[0], NULL);
19044}
19045
19046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 * Get the string value of a variable.
19048 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019049 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19050 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 * If the String variable has never been set, return an empty string.
19052 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019053 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19054 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 */
19056 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019057get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019058 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019059{
19060 static char_u mybuf[NUMBUFLEN];
19061
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019062 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063}
19064
19065 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019066get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019067 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019068 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019070 char_u *res = get_tv_string_buf_chk(varp, buf);
19071
19072 return res != NULL ? res : (char_u *)"";
19073}
19074
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019075 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019076get_tv_string_chk(varp)
19077 typval_T *varp;
19078{
19079 static char_u mybuf[NUMBUFLEN];
19080
19081 return get_tv_string_buf_chk(varp, mybuf);
19082}
19083
19084 static char_u *
19085get_tv_string_buf_chk(varp, buf)
19086 typval_T *varp;
19087 char_u *buf;
19088{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019089 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019091 case VAR_NUMBER:
19092 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19093 return buf;
19094 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019095 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019096 break;
19097 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019098 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019099 break;
19100 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019101 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019102 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019103#ifdef FEAT_FLOAT
19104 case VAR_FLOAT:
19105 EMSG(_("E806: using Float as a String"));
19106 break;
19107#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019108 case VAR_STRING:
19109 if (varp->vval.v_string != NULL)
19110 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019111 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019112 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019113 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019114 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019116 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117}
19118
19119/*
19120 * Find variable "name" in the list of variables.
19121 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019122 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019123 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019124 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019126 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019127find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019129 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019132 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133
Bram Moolenaara7043832005-01-21 11:56:39 +000019134 ht = find_var_ht(name, &varname);
19135 if (htp != NULL)
19136 *htp = ht;
19137 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019139 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140}
19141
19142/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019143 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019144 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019146 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019147find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019148 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019149 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019150 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019151{
Bram Moolenaar33570922005-01-25 22:26:29 +000019152 hashitem_T *hi;
19153
19154 if (*varname == NUL)
19155 {
19156 /* Must be something like "s:", otherwise "ht" would be NULL. */
19157 switch (varname[-2])
19158 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019159 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019160 case 'g': return &globvars_var;
19161 case 'v': return &vimvars_var;
19162 case 'b': return &curbuf->b_bufvar;
19163 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019164#ifdef FEAT_WINDOWS
19165 case 't': return &curtab->tp_winvar;
19166#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019167 case 'l': return current_funccal == NULL
19168 ? NULL : &current_funccal->l_vars_var;
19169 case 'a': return current_funccal == NULL
19170 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019171 }
19172 return NULL;
19173 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019174
19175 hi = hash_find(ht, varname);
19176 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019177 {
19178 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019179 * worked find the variable again. Don't auto-load a script if it was
19180 * loaded already, otherwise it would be loaded every time when
19181 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019182 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019183 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019184 hi = hash_find(ht, varname);
19185 if (HASHITEM_EMPTY(hi))
19186 return NULL;
19187 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019188 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019189}
19190
19191/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019192 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019193 * Set "varname" to the start of name without ':'.
19194 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019195 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019196find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019197 char_u *name;
19198 char_u **varname;
19199{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019200 hashitem_T *hi;
19201
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202 if (name[1] != ':')
19203 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019204 /* The name must not start with a colon or #. */
19205 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 return NULL;
19207 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019208
19209 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019210 hi = hash_find(&compat_hashtab, name);
19211 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019212 return &compat_hashtab;
19213
Bram Moolenaar071d4272004-06-13 20:20:40 +000019214 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019215 return &globvarht; /* global variable */
19216 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217 }
19218 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019219 if (*name == 'g') /* global variable */
19220 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019221 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19222 */
19223 if (vim_strchr(name + 2, ':') != NULL
19224 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019225 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019227 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019229 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019230#ifdef FEAT_WINDOWS
19231 if (*name == 't') /* tab page variable */
19232 return &curtab->tp_vars.dv_hashtab;
19233#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019234 if (*name == 'v') /* v: variable */
19235 return &vimvarht;
19236 if (*name == 'a' && current_funccal != NULL) /* function argument */
19237 return &current_funccal->l_avars.dv_hashtab;
19238 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19239 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019240 if (*name == 's' /* script variable */
19241 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19242 return &SCRIPT_VARS(current_SID);
19243 return NULL;
19244}
19245
19246/*
19247 * Get the string value of a (global/local) variable.
19248 * Returns NULL when it doesn't exist.
19249 */
19250 char_u *
19251get_var_value(name)
19252 char_u *name;
19253{
Bram Moolenaar33570922005-01-25 22:26:29 +000019254 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255
Bram Moolenaara7043832005-01-21 11:56:39 +000019256 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019257 if (v == NULL)
19258 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019259 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019260}
19261
19262/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019263 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264 * sourcing this script and when executing functions defined in the script.
19265 */
19266 void
19267new_script_vars(id)
19268 scid_T id;
19269{
Bram Moolenaara7043832005-01-21 11:56:39 +000019270 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019271 hashtab_T *ht;
19272 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019273
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19275 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019276 /* Re-allocating ga_data means that an ht_array pointing to
19277 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019278 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019279 for (i = 1; i <= ga_scripts.ga_len; ++i)
19280 {
19281 ht = &SCRIPT_VARS(i);
19282 if (ht->ht_mask == HT_INIT_SIZE - 1)
19283 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019284 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019285 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019286 }
19287
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288 while (ga_scripts.ga_len < id)
19289 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019290 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019291 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019292 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019293 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294 }
19295 }
19296}
19297
19298/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019299 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19300 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301 */
19302 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019303init_var_dict(dict, dict_var)
19304 dict_T *dict;
19305 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306{
Bram Moolenaar33570922005-01-25 22:26:29 +000019307 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019308 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019309 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019310 dict_var->di_tv.vval.v_dict = dict;
19311 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019312 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019313 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19314 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315}
19316
19317/*
19318 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019319 * Frees all allocated variables and the value they contain.
19320 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321 */
19322 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019323vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019324 hashtab_T *ht;
19325{
19326 vars_clear_ext(ht, TRUE);
19327}
19328
19329/*
19330 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19331 */
19332 static void
19333vars_clear_ext(ht, free_val)
19334 hashtab_T *ht;
19335 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336{
Bram Moolenaara7043832005-01-21 11:56:39 +000019337 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019338 hashitem_T *hi;
19339 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019340
Bram Moolenaar33570922005-01-25 22:26:29 +000019341 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019342 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019343 for (hi = ht->ht_array; todo > 0; ++hi)
19344 {
19345 if (!HASHITEM_EMPTY(hi))
19346 {
19347 --todo;
19348
Bram Moolenaar33570922005-01-25 22:26:29 +000019349 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019350 * ht_array might change then. hash_clear() takes care of it
19351 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019352 v = HI2DI(hi);
19353 if (free_val)
19354 clear_tv(&v->di_tv);
19355 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19356 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019357 }
19358 }
19359 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019360 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019361}
19362
Bram Moolenaara7043832005-01-21 11:56:39 +000019363/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019364 * Delete a variable from hashtab "ht" at item "hi".
19365 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019368delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019369 hashtab_T *ht;
19370 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019371{
Bram Moolenaar33570922005-01-25 22:26:29 +000019372 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019373
19374 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019375 clear_tv(&di->di_tv);
19376 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377}
19378
19379/*
19380 * List the value of one internal variable.
19381 */
19382 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019383list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019384 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019386 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019388 char_u *tofree;
19389 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019390 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019391
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019392 current_copyID += COPYID_INC;
19393 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019394 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019395 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019396 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397}
19398
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019400list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401 char_u *prefix;
19402 char_u *name;
19403 int type;
19404 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019405 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406{
Bram Moolenaar31859182007-08-14 20:41:13 +000019407 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19408 msg_start();
19409 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410 if (name != NULL) /* "a:" vars don't have a name stored */
19411 msg_puts(name);
19412 msg_putchar(' ');
19413 msg_advance(22);
19414 if (type == VAR_NUMBER)
19415 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019416 else if (type == VAR_FUNC)
19417 msg_putchar('*');
19418 else if (type == VAR_LIST)
19419 {
19420 msg_putchar('[');
19421 if (*string == '[')
19422 ++string;
19423 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019424 else if (type == VAR_DICT)
19425 {
19426 msg_putchar('{');
19427 if (*string == '{')
19428 ++string;
19429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430 else
19431 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019432
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019434
19435 if (type == VAR_FUNC)
19436 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019437 if (*first)
19438 {
19439 msg_clr_eos();
19440 *first = FALSE;
19441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019442}
19443
19444/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019445 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446 * If the variable already exists, the value is updated.
19447 * Otherwise the variable is created.
19448 */
19449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019450set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019452 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019453 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454{
Bram Moolenaar33570922005-01-25 22:26:29 +000019455 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019456 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019457 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019458 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019460 ht = find_var_ht(name, &varname);
19461 if (ht == NULL || *varname == NUL)
19462 {
19463 EMSG2(_(e_illvar), name);
19464 return;
19465 }
19466 v = find_var_in_ht(ht, varname, TRUE);
19467
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019468 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019469 {
19470 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19471 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19472 ? name[2] : name[0]))
19473 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019474 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019475 return;
19476 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019477 /* Don't allow hiding a function. When "v" is not NULL we migth be
19478 * assigning another function to the same var, the type is checked
19479 * below. */
19480 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019481 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019482 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019483 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019484 return;
19485 }
19486 }
19487
Bram Moolenaar33570922005-01-25 22:26:29 +000019488 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019490 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019491 if (var_check_ro(v->di_flags, name)
19492 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019493 return;
19494 if (v->di_tv.v_type != tv->v_type
19495 && !((v->di_tv.v_type == VAR_STRING
19496 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019497 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019498 || tv->v_type == VAR_NUMBER))
19499#ifdef FEAT_FLOAT
19500 && !((v->di_tv.v_type == VAR_NUMBER
19501 || v->di_tv.v_type == VAR_FLOAT)
19502 && (tv->v_type == VAR_NUMBER
19503 || tv->v_type == VAR_FLOAT))
19504#endif
19505 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019506 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019507 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019508 return;
19509 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019510
19511 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019512 * Handle setting internal v: variables separately: we don't change
19513 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019514 */
19515 if (ht == &vimvarht)
19516 {
19517 if (v->di_tv.v_type == VAR_STRING)
19518 {
19519 vim_free(v->di_tv.vval.v_string);
19520 if (copy || tv->v_type != VAR_STRING)
19521 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19522 else
19523 {
19524 /* Take over the string to avoid an extra alloc/free. */
19525 v->di_tv.vval.v_string = tv->vval.v_string;
19526 tv->vval.v_string = NULL;
19527 }
19528 }
19529 else if (v->di_tv.v_type != VAR_NUMBER)
19530 EMSG2(_(e_intern2), "set_var()");
19531 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019532 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019533 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019534 if (STRCMP(varname, "searchforward") == 0)
19535 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19536 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019537 return;
19538 }
19539
19540 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541 }
19542 else /* add a new variable */
19543 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019544 /* Can't add "v:" variable. */
19545 if (ht == &vimvarht)
19546 {
19547 EMSG2(_(e_illvar), name);
19548 return;
19549 }
19550
Bram Moolenaar92124a32005-06-17 22:03:40 +000019551 /* Make sure the variable name is valid. */
19552 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019553 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19554 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019555 {
19556 EMSG2(_(e_illvar), varname);
19557 return;
19558 }
19559
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019560 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19561 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019562 if (v == NULL)
19563 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019564 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019565 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019567 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568 return;
19569 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019570 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019572
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019573 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019574 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019575 else
19576 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019577 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019578 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019579 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581}
19582
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019583/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019584 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019585 * Also give an error message.
19586 */
19587 static int
19588var_check_ro(flags, name)
19589 int flags;
19590 char_u *name;
19591{
19592 if (flags & DI_FLAGS_RO)
19593 {
19594 EMSG2(_(e_readonlyvar), name);
19595 return TRUE;
19596 }
19597 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19598 {
19599 EMSG2(_(e_readonlysbx), name);
19600 return TRUE;
19601 }
19602 return FALSE;
19603}
19604
19605/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019606 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19607 * Also give an error message.
19608 */
19609 static int
19610var_check_fixed(flags, name)
19611 int flags;
19612 char_u *name;
19613{
19614 if (flags & DI_FLAGS_FIX)
19615 {
19616 EMSG2(_("E795: Cannot delete variable %s"), name);
19617 return TRUE;
19618 }
19619 return FALSE;
19620}
19621
19622/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019623 * Return TRUE if typeval "tv" is set to be locked (immutable).
19624 * Also give an error message, using "name".
19625 */
19626 static int
19627tv_check_lock(lock, name)
19628 int lock;
19629 char_u *name;
19630{
19631 if (lock & VAR_LOCKED)
19632 {
19633 EMSG2(_("E741: Value is locked: %s"),
19634 name == NULL ? (char_u *)_("Unknown") : name);
19635 return TRUE;
19636 }
19637 if (lock & VAR_FIXED)
19638 {
19639 EMSG2(_("E742: Cannot change value of %s"),
19640 name == NULL ? (char_u *)_("Unknown") : name);
19641 return TRUE;
19642 }
19643 return FALSE;
19644}
19645
19646/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019647 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019648 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019649 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019650 * It is OK for "from" and "to" to point to the same item. This is used to
19651 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019652 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019653 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019654copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019655 typval_T *from;
19656 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019657{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019658 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019659 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019660 switch (from->v_type)
19661 {
19662 case VAR_NUMBER:
19663 to->vval.v_number = from->vval.v_number;
19664 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019665#ifdef FEAT_FLOAT
19666 case VAR_FLOAT:
19667 to->vval.v_float = from->vval.v_float;
19668 break;
19669#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019670 case VAR_STRING:
19671 case VAR_FUNC:
19672 if (from->vval.v_string == NULL)
19673 to->vval.v_string = NULL;
19674 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019675 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019676 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019677 if (from->v_type == VAR_FUNC)
19678 func_ref(to->vval.v_string);
19679 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019680 break;
19681 case VAR_LIST:
19682 if (from->vval.v_list == NULL)
19683 to->vval.v_list = NULL;
19684 else
19685 {
19686 to->vval.v_list = from->vval.v_list;
19687 ++to->vval.v_list->lv_refcount;
19688 }
19689 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019690 case VAR_DICT:
19691 if (from->vval.v_dict == NULL)
19692 to->vval.v_dict = NULL;
19693 else
19694 {
19695 to->vval.v_dict = from->vval.v_dict;
19696 ++to->vval.v_dict->dv_refcount;
19697 }
19698 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019699 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019700 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019701 break;
19702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703}
19704
19705/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019706 * Make a copy of an item.
19707 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019708 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19709 * reference to an already copied list/dict can be used.
19710 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019711 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019712 static int
19713item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019714 typval_T *from;
19715 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019716 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019717 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019718{
19719 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019720 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019721
Bram Moolenaar33570922005-01-25 22:26:29 +000019722 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019723 {
19724 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019725 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019726 }
19727 ++recurse;
19728
19729 switch (from->v_type)
19730 {
19731 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019732#ifdef FEAT_FLOAT
19733 case VAR_FLOAT:
19734#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019735 case VAR_STRING:
19736 case VAR_FUNC:
19737 copy_tv(from, to);
19738 break;
19739 case VAR_LIST:
19740 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019741 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019742 if (from->vval.v_list == NULL)
19743 to->vval.v_list = NULL;
19744 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19745 {
19746 /* use the copy made earlier */
19747 to->vval.v_list = from->vval.v_list->lv_copylist;
19748 ++to->vval.v_list->lv_refcount;
19749 }
19750 else
19751 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19752 if (to->vval.v_list == NULL)
19753 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019754 break;
19755 case VAR_DICT:
19756 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019757 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019758 if (from->vval.v_dict == NULL)
19759 to->vval.v_dict = NULL;
19760 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19761 {
19762 /* use the copy made earlier */
19763 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19764 ++to->vval.v_dict->dv_refcount;
19765 }
19766 else
19767 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19768 if (to->vval.v_dict == NULL)
19769 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019770 break;
19771 default:
19772 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019773 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019774 }
19775 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019776 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019777}
19778
19779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780 * ":echo expr1 ..." print each argument separated with a space, add a
19781 * newline at the end.
19782 * ":echon expr1 ..." print each argument plain.
19783 */
19784 void
19785ex_echo(eap)
19786 exarg_T *eap;
19787{
19788 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019789 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019790 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 char_u *p;
19792 int needclr = TRUE;
19793 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019794 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795
19796 if (eap->skip)
19797 ++emsg_skip;
19798 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19799 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019800 /* If eval1() causes an error message the text from the command may
19801 * still need to be cleared. E.g., "echo 22,44". */
19802 need_clr_eos = needclr;
19803
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019805 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806 {
19807 /*
19808 * Report the invalid expression unless the expression evaluation
19809 * has been cancelled due to an aborting error, an interrupt, or an
19810 * exception.
19811 */
19812 if (!aborting())
19813 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019814 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 break;
19816 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019817 need_clr_eos = FALSE;
19818
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819 if (!eap->skip)
19820 {
19821 if (atstart)
19822 {
19823 atstart = FALSE;
19824 /* Call msg_start() after eval1(), evaluating the expression
19825 * may cause a message to appear. */
19826 if (eap->cmdidx == CMD_echo)
19827 msg_start();
19828 }
19829 else if (eap->cmdidx == CMD_echo)
19830 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019831 current_copyID += COPYID_INC;
19832 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019833 if (p != NULL)
19834 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019836 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019838 if (*p != TAB && needclr)
19839 {
19840 /* remove any text still there from the command */
19841 msg_clr_eos();
19842 needclr = FALSE;
19843 }
19844 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845 }
19846 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019847 {
19848#ifdef FEAT_MBYTE
19849 if (has_mbyte)
19850 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019851 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019852
19853 (void)msg_outtrans_len_attr(p, i, echo_attr);
19854 p += i - 1;
19855 }
19856 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019858 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019861 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019863 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019864 arg = skipwhite(arg);
19865 }
19866 eap->nextcmd = check_nextcmd(arg);
19867
19868 if (eap->skip)
19869 --emsg_skip;
19870 else
19871 {
19872 /* remove text that may still be there from the command */
19873 if (needclr)
19874 msg_clr_eos();
19875 if (eap->cmdidx == CMD_echo)
19876 msg_end();
19877 }
19878}
19879
19880/*
19881 * ":echohl {name}".
19882 */
19883 void
19884ex_echohl(eap)
19885 exarg_T *eap;
19886{
19887 int id;
19888
19889 id = syn_name2id(eap->arg);
19890 if (id == 0)
19891 echo_attr = 0;
19892 else
19893 echo_attr = syn_id2attr(id);
19894}
19895
19896/*
19897 * ":execute expr1 ..." execute the result of an expression.
19898 * ":echomsg expr1 ..." Print a message
19899 * ":echoerr expr1 ..." Print an error
19900 * Each gets spaces around each argument and a newline at the end for
19901 * echo commands
19902 */
19903 void
19904ex_execute(eap)
19905 exarg_T *eap;
19906{
19907 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019908 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909 int ret = OK;
19910 char_u *p;
19911 garray_T ga;
19912 int len;
19913 int save_did_emsg;
19914
19915 ga_init2(&ga, 1, 80);
19916
19917 if (eap->skip)
19918 ++emsg_skip;
19919 while (*arg != NUL && *arg != '|' && *arg != '\n')
19920 {
19921 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019922 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923 {
19924 /*
19925 * Report the invalid expression unless the expression evaluation
19926 * has been cancelled due to an aborting error, an interrupt, or an
19927 * exception.
19928 */
19929 if (!aborting())
19930 EMSG2(_(e_invexpr2), p);
19931 ret = FAIL;
19932 break;
19933 }
19934
19935 if (!eap->skip)
19936 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019937 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 len = (int)STRLEN(p);
19939 if (ga_grow(&ga, len + 2) == FAIL)
19940 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019941 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942 ret = FAIL;
19943 break;
19944 }
19945 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948 ga.ga_len += len;
19949 }
19950
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019951 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952 arg = skipwhite(arg);
19953 }
19954
19955 if (ret != FAIL && ga.ga_data != NULL)
19956 {
19957 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019960 out_flush();
19961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962 else if (eap->cmdidx == CMD_echoerr)
19963 {
19964 /* We don't want to abort following commands, restore did_emsg. */
19965 save_did_emsg = did_emsg;
19966 EMSG((char_u *)ga.ga_data);
19967 if (!force_abort)
19968 did_emsg = save_did_emsg;
19969 }
19970 else if (eap->cmdidx == CMD_execute)
19971 do_cmdline((char_u *)ga.ga_data,
19972 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19973 }
19974
19975 ga_clear(&ga);
19976
19977 if (eap->skip)
19978 --emsg_skip;
19979
19980 eap->nextcmd = check_nextcmd(arg);
19981}
19982
19983/*
19984 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19985 * "arg" points to the "&" or '+' when called, to "option" when returning.
19986 * Returns NULL when no option name found. Otherwise pointer to the char
19987 * after the option name.
19988 */
19989 static char_u *
19990find_option_end(arg, opt_flags)
19991 char_u **arg;
19992 int *opt_flags;
19993{
19994 char_u *p = *arg;
19995
19996 ++p;
19997 if (*p == 'g' && p[1] == ':')
19998 {
19999 *opt_flags = OPT_GLOBAL;
20000 p += 2;
20001 }
20002 else if (*p == 'l' && p[1] == ':')
20003 {
20004 *opt_flags = OPT_LOCAL;
20005 p += 2;
20006 }
20007 else
20008 *opt_flags = 0;
20009
20010 if (!ASCII_ISALPHA(*p))
20011 return NULL;
20012 *arg = p;
20013
20014 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20015 p += 4; /* termcap option */
20016 else
20017 while (ASCII_ISALPHA(*p))
20018 ++p;
20019 return p;
20020}
20021
20022/*
20023 * ":function"
20024 */
20025 void
20026ex_function(eap)
20027 exarg_T *eap;
20028{
20029 char_u *theline;
20030 int j;
20031 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 char_u *name = NULL;
20034 char_u *p;
20035 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020036 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 garray_T newargs;
20038 garray_T newlines;
20039 int varargs = FALSE;
20040 int mustend = FALSE;
20041 int flags = 0;
20042 ufunc_T *fp;
20043 int indent;
20044 int nesting;
20045 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020046 dictitem_T *v;
20047 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020048 static int func_nr = 0; /* number for nameless function */
20049 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020050 hashtab_T *ht;
20051 int todo;
20052 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020053 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054
20055 /*
20056 * ":function" without argument: list functions.
20057 */
20058 if (ends_excmd(*eap->arg))
20059 {
20060 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020061 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020062 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020063 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020064 {
20065 if (!HASHITEM_EMPTY(hi))
20066 {
20067 --todo;
20068 fp = HI2UF(hi);
20069 if (!isdigit(*fp->uf_name))
20070 list_func_head(fp, FALSE);
20071 }
20072 }
20073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074 eap->nextcmd = check_nextcmd(eap->arg);
20075 return;
20076 }
20077
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020078 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020079 * ":function /pat": list functions matching pattern.
20080 */
20081 if (*eap->arg == '/')
20082 {
20083 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20084 if (!eap->skip)
20085 {
20086 regmatch_T regmatch;
20087
20088 c = *p;
20089 *p = NUL;
20090 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20091 *p = c;
20092 if (regmatch.regprog != NULL)
20093 {
20094 regmatch.rm_ic = p_ic;
20095
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020096 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020097 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20098 {
20099 if (!HASHITEM_EMPTY(hi))
20100 {
20101 --todo;
20102 fp = HI2UF(hi);
20103 if (!isdigit(*fp->uf_name)
20104 && vim_regexec(&regmatch, fp->uf_name, 0))
20105 list_func_head(fp, FALSE);
20106 }
20107 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020108 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020109 }
20110 }
20111 if (*p == '/')
20112 ++p;
20113 eap->nextcmd = check_nextcmd(p);
20114 return;
20115 }
20116
20117 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020118 * Get the function name. There are these situations:
20119 * func normal function name
20120 * "name" == func, "fudi.fd_dict" == NULL
20121 * dict.func new dictionary entry
20122 * "name" == NULL, "fudi.fd_dict" set,
20123 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20124 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020125 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020126 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20127 * dict.func existing dict entry that's not a Funcref
20128 * "name" == NULL, "fudi.fd_dict" set,
20129 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020132 name = trans_function_name(&p, eap->skip, 0, &fudi);
20133 paren = (vim_strchr(p, '(') != NULL);
20134 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 {
20136 /*
20137 * Return on an invalid expression in braces, unless the expression
20138 * evaluation has been cancelled due to an aborting error, an
20139 * interrupt, or an exception.
20140 */
20141 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020142 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020143 if (!eap->skip && fudi.fd_newkey != NULL)
20144 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020145 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148 else
20149 eap->skip = TRUE;
20150 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020151
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 /* An error in a function call during evaluation of an expression in magic
20153 * braces should not cause the function not to be defined. */
20154 saved_did_emsg = did_emsg;
20155 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156
20157 /*
20158 * ":function func" with only function name: list function.
20159 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020160 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161 {
20162 if (!ends_excmd(*skipwhite(p)))
20163 {
20164 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020165 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166 }
20167 eap->nextcmd = check_nextcmd(p);
20168 if (eap->nextcmd != NULL)
20169 *p = NUL;
20170 if (!eap->skip && !got_int)
20171 {
20172 fp = find_func(name);
20173 if (fp != NULL)
20174 {
20175 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020176 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020178 if (FUNCLINE(fp, j) == NULL)
20179 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180 msg_putchar('\n');
20181 msg_outnum((long)(j + 1));
20182 if (j < 9)
20183 msg_putchar(' ');
20184 if (j < 99)
20185 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020186 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 out_flush(); /* show a line at a time */
20188 ui_breakcheck();
20189 }
20190 if (!got_int)
20191 {
20192 msg_putchar('\n');
20193 msg_puts((char_u *)" endfunction");
20194 }
20195 }
20196 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020197 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020199 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 }
20201
20202 /*
20203 * ":function name(arg1, arg2)" Define function.
20204 */
20205 p = skipwhite(p);
20206 if (*p != '(')
20207 {
20208 if (!eap->skip)
20209 {
20210 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020211 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 }
20213 /* attempt to continue by skipping some text */
20214 if (vim_strchr(p, '(') != NULL)
20215 p = vim_strchr(p, '(');
20216 }
20217 p = skipwhite(p + 1);
20218
20219 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20220 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20221
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020222 if (!eap->skip)
20223 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020224 /* Check the name of the function. Unless it's a dictionary function
20225 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020226 if (name != NULL)
20227 arg = name;
20228 else
20229 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020230 if (arg != NULL && (fudi.fd_di == NULL
20231 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020232 {
20233 if (*arg == K_SPECIAL)
20234 j = 3;
20235 else
20236 j = 0;
20237 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20238 : eval_isnamec(arg[j])))
20239 ++j;
20240 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020241 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020242 }
20243 }
20244
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245 /*
20246 * Isolate the arguments: "arg1, arg2, ...)"
20247 */
20248 while (*p != ')')
20249 {
20250 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20251 {
20252 varargs = TRUE;
20253 p += 3;
20254 mustend = TRUE;
20255 }
20256 else
20257 {
20258 arg = p;
20259 while (ASCII_ISALNUM(*p) || *p == '_')
20260 ++p;
20261 if (arg == p || isdigit(*arg)
20262 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20263 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20264 {
20265 if (!eap->skip)
20266 EMSG2(_("E125: Illegal argument: %s"), arg);
20267 break;
20268 }
20269 if (ga_grow(&newargs, 1) == FAIL)
20270 goto erret;
20271 c = *p;
20272 *p = NUL;
20273 arg = vim_strsave(arg);
20274 if (arg == NULL)
20275 goto erret;
20276 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20277 *p = c;
20278 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279 if (*p == ',')
20280 ++p;
20281 else
20282 mustend = TRUE;
20283 }
20284 p = skipwhite(p);
20285 if (mustend && *p != ')')
20286 {
20287 if (!eap->skip)
20288 EMSG2(_(e_invarg2), eap->arg);
20289 break;
20290 }
20291 }
20292 ++p; /* skip the ')' */
20293
Bram Moolenaare9a41262005-01-15 22:18:47 +000020294 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 for (;;)
20296 {
20297 p = skipwhite(p);
20298 if (STRNCMP(p, "range", 5) == 0)
20299 {
20300 flags |= FC_RANGE;
20301 p += 5;
20302 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020303 else if (STRNCMP(p, "dict", 4) == 0)
20304 {
20305 flags |= FC_DICT;
20306 p += 4;
20307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 else if (STRNCMP(p, "abort", 5) == 0)
20309 {
20310 flags |= FC_ABORT;
20311 p += 5;
20312 }
20313 else
20314 break;
20315 }
20316
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020317 /* When there is a line break use what follows for the function body.
20318 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20319 if (*p == '\n')
20320 line_arg = p + 1;
20321 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322 EMSG(_(e_trailing));
20323
20324 /*
20325 * Read the body of the function, until ":endfunction" is found.
20326 */
20327 if (KeyTyped)
20328 {
20329 /* Check if the function already exists, don't let the user type the
20330 * whole function before telling him it doesn't work! For a script we
20331 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020332 if (!eap->skip && !eap->forceit)
20333 {
20334 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20335 EMSG(_(e_funcdict));
20336 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020337 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020340 if (!eap->skip && did_emsg)
20341 goto erret;
20342
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343 msg_putchar('\n'); /* don't overwrite the function name */
20344 cmdline_row = msg_row;
20345 }
20346
20347 indent = 2;
20348 nesting = 0;
20349 for (;;)
20350 {
20351 msg_scroll = TRUE;
20352 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020353 sourcing_lnum_off = sourcing_lnum;
20354
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020355 if (line_arg != NULL)
20356 {
20357 /* Use eap->arg, split up in parts by line breaks. */
20358 theline = line_arg;
20359 p = vim_strchr(theline, '\n');
20360 if (p == NULL)
20361 line_arg += STRLEN(line_arg);
20362 else
20363 {
20364 *p = NUL;
20365 line_arg = p + 1;
20366 }
20367 }
20368 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 theline = getcmdline(':', 0L, indent);
20370 else
20371 theline = eap->getline(':', eap->cookie, indent);
20372 if (KeyTyped)
20373 lines_left = Rows - 1;
20374 if (theline == NULL)
20375 {
20376 EMSG(_("E126: Missing :endfunction"));
20377 goto erret;
20378 }
20379
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020380 /* Detect line continuation: sourcing_lnum increased more than one. */
20381 if (sourcing_lnum > sourcing_lnum_off + 1)
20382 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20383 else
20384 sourcing_lnum_off = 0;
20385
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 if (skip_until != NULL)
20387 {
20388 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20389 * don't check for ":endfunc". */
20390 if (STRCMP(theline, skip_until) == 0)
20391 {
20392 vim_free(skip_until);
20393 skip_until = NULL;
20394 }
20395 }
20396 else
20397 {
20398 /* skip ':' and blanks*/
20399 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20400 ;
20401
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020402 /* Check for "endfunction". */
20403 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020405 if (line_arg == NULL)
20406 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 break;
20408 }
20409
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020410 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 * at "end". */
20412 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20413 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020414 else if (STRNCMP(p, "if", 2) == 0
20415 || STRNCMP(p, "wh", 2) == 0
20416 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417 || STRNCMP(p, "try", 3) == 0)
20418 indent += 2;
20419
20420 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020421 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020423 if (*p == '!')
20424 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425 p += eval_fname_script(p);
20426 if (ASCII_ISALPHA(*p))
20427 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020428 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429 if (*skipwhite(p) == '(')
20430 {
20431 ++nesting;
20432 indent += 2;
20433 }
20434 }
20435 }
20436
20437 /* Check for ":append" or ":insert". */
20438 p = skip_range(p, NULL);
20439 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20440 || (p[0] == 'i'
20441 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20442 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20443 skip_until = vim_strsave((char_u *)".");
20444
20445 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20446 arg = skipwhite(skiptowhite(p));
20447 if (arg[0] == '<' && arg[1] =='<'
20448 && ((p[0] == 'p' && p[1] == 'y'
20449 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20450 || (p[0] == 'p' && p[1] == 'e'
20451 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20452 || (p[0] == 't' && p[1] == 'c'
20453 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20454 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20455 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020456 || (p[0] == 'm' && p[1] == 'z'
20457 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458 ))
20459 {
20460 /* ":python <<" continues until a dot, like ":append" */
20461 p = skipwhite(arg + 2);
20462 if (*p == NUL)
20463 skip_until = vim_strsave((char_u *)".");
20464 else
20465 skip_until = vim_strsave(p);
20466 }
20467 }
20468
20469 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020470 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020471 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020472 if (line_arg == NULL)
20473 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020475 }
20476
20477 /* Copy the line to newly allocated memory. get_one_sourceline()
20478 * allocates 250 bytes per line, this saves 80% on average. The cost
20479 * is an extra alloc/free. */
20480 p = vim_strsave(theline);
20481 if (p != NULL)
20482 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020483 if (line_arg == NULL)
20484 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020485 theline = p;
20486 }
20487
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020488 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20489
20490 /* Add NULL lines for continuation lines, so that the line count is
20491 * equal to the index in the growarray. */
20492 while (sourcing_lnum_off-- > 0)
20493 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020494
20495 /* Check for end of eap->arg. */
20496 if (line_arg != NULL && *line_arg == NUL)
20497 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498 }
20499
20500 /* Don't define the function when skipping commands or when an error was
20501 * detected. */
20502 if (eap->skip || did_emsg)
20503 goto erret;
20504
20505 /*
20506 * If there are no errors, add the function
20507 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020508 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020509 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020510 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020511 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020512 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020513 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020514 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020515 goto erret;
20516 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020517
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020518 fp = find_func(name);
20519 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020521 if (!eap->forceit)
20522 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020523 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020524 goto erret;
20525 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020526 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020527 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020528 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020529 name);
20530 goto erret;
20531 }
20532 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020533 ga_clear_strings(&(fp->uf_args));
20534 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020535 vim_free(name);
20536 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538 }
20539 else
20540 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020541 char numbuf[20];
20542
20543 fp = NULL;
20544 if (fudi.fd_newkey == NULL && !eap->forceit)
20545 {
20546 EMSG(_(e_funcdict));
20547 goto erret;
20548 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020549 if (fudi.fd_di == NULL)
20550 {
20551 /* Can't add a function to a locked dictionary */
20552 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20553 goto erret;
20554 }
20555 /* Can't change an existing function if it is locked */
20556 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20557 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020558
20559 /* Give the function a sequential number. Can only be used with a
20560 * Funcref! */
20561 vim_free(name);
20562 sprintf(numbuf, "%d", ++func_nr);
20563 name = vim_strsave((char_u *)numbuf);
20564 if (name == NULL)
20565 goto erret;
20566 }
20567
20568 if (fp == NULL)
20569 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020570 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020571 {
20572 int slen, plen;
20573 char_u *scriptname;
20574
20575 /* Check that the autoload name matches the script name. */
20576 j = FAIL;
20577 if (sourcing_name != NULL)
20578 {
20579 scriptname = autoload_name(name);
20580 if (scriptname != NULL)
20581 {
20582 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020583 plen = (int)STRLEN(p);
20584 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020585 if (slen > plen && fnamecmp(p,
20586 sourcing_name + slen - plen) == 0)
20587 j = OK;
20588 vim_free(scriptname);
20589 }
20590 }
20591 if (j == FAIL)
20592 {
20593 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20594 goto erret;
20595 }
20596 }
20597
20598 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599 if (fp == NULL)
20600 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020601
20602 if (fudi.fd_dict != NULL)
20603 {
20604 if (fudi.fd_di == NULL)
20605 {
20606 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020607 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020608 if (fudi.fd_di == NULL)
20609 {
20610 vim_free(fp);
20611 goto erret;
20612 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020613 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20614 {
20615 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020616 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020617 goto erret;
20618 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020619 }
20620 else
20621 /* overwrite existing dict entry */
20622 clear_tv(&fudi.fd_di->di_tv);
20623 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020624 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020625 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020626 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020627
20628 /* behave like "dict" was used */
20629 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020630 }
20631
Bram Moolenaar071d4272004-06-13 20:20:40 +000020632 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020633 STRCPY(fp->uf_name, name);
20634 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020636 fp->uf_args = newargs;
20637 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020638#ifdef FEAT_PROFILE
20639 fp->uf_tml_count = NULL;
20640 fp->uf_tml_total = NULL;
20641 fp->uf_tml_self = NULL;
20642 fp->uf_profiling = FALSE;
20643 if (prof_def_func())
20644 func_do_profile(fp);
20645#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020646 fp->uf_varargs = varargs;
20647 fp->uf_flags = flags;
20648 fp->uf_calls = 0;
20649 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020650 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020651
20652erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653 ga_clear_strings(&newargs);
20654 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020655ret_free:
20656 vim_free(skip_until);
20657 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020660}
20661
20662/*
20663 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020664 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020665 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020666 * flags:
20667 * TFN_INT: internal function name OK
20668 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669 * Advances "pp" to just after the function name (if no error).
20670 */
20671 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020672trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673 char_u **pp;
20674 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020675 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020676 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020677{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020678 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020679 char_u *start;
20680 char_u *end;
20681 int lead;
20682 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020684 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020685
20686 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020687 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020689
20690 /* Check for hard coded <SNR>: already translated function ID (from a user
20691 * command). */
20692 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20693 && (*pp)[2] == (int)KE_SNR)
20694 {
20695 *pp += 3;
20696 len = get_id_len(pp) + 3;
20697 return vim_strnsave(start, len);
20698 }
20699
20700 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20701 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020703 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020705
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020706 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20707 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020708 if (end == start)
20709 {
20710 if (!skip)
20711 EMSG(_("E129: Function name required"));
20712 goto theend;
20713 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020714 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020715 {
20716 /*
20717 * Report an invalid expression in braces, unless the expression
20718 * evaluation has been cancelled due to an aborting error, an
20719 * interrupt, or an exception.
20720 */
20721 if (!aborting())
20722 {
20723 if (end != NULL)
20724 EMSG2(_(e_invarg2), start);
20725 }
20726 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020727 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020728 goto theend;
20729 }
20730
20731 if (lv.ll_tv != NULL)
20732 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020733 if (fdp != NULL)
20734 {
20735 fdp->fd_dict = lv.ll_dict;
20736 fdp->fd_newkey = lv.ll_newkey;
20737 lv.ll_newkey = NULL;
20738 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020739 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020740 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20741 {
20742 name = vim_strsave(lv.ll_tv->vval.v_string);
20743 *pp = end;
20744 }
20745 else
20746 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020747 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20748 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020749 EMSG(_(e_funcref));
20750 else
20751 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020752 name = NULL;
20753 }
20754 goto theend;
20755 }
20756
20757 if (lv.ll_name == NULL)
20758 {
20759 /* Error found, but continue after the function name. */
20760 *pp = end;
20761 goto theend;
20762 }
20763
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020764 /* Check if the name is a Funcref. If so, use the value. */
20765 if (lv.ll_exp_name != NULL)
20766 {
20767 len = (int)STRLEN(lv.ll_exp_name);
20768 name = deref_func_name(lv.ll_exp_name, &len);
20769 if (name == lv.ll_exp_name)
20770 name = NULL;
20771 }
20772 else
20773 {
20774 len = (int)(end - *pp);
20775 name = deref_func_name(*pp, &len);
20776 if (name == *pp)
20777 name = NULL;
20778 }
20779 if (name != NULL)
20780 {
20781 name = vim_strsave(name);
20782 *pp = end;
20783 goto theend;
20784 }
20785
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020786 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020787 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020788 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020789 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20790 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20791 {
20792 /* When there was "s:" already or the name expanded to get a
20793 * leading "s:" then remove it. */
20794 lv.ll_name += 2;
20795 len -= 2;
20796 lead = 2;
20797 }
20798 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020799 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020800 {
20801 if (lead == 2) /* skip over "s:" */
20802 lv.ll_name += 2;
20803 len = (int)(end - lv.ll_name);
20804 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020805
20806 /*
20807 * Copy the function name to allocated memory.
20808 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20809 * Accept <SNR>123_name() outside a script.
20810 */
20811 if (skip)
20812 lead = 0; /* do nothing */
20813 else if (lead > 0)
20814 {
20815 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020816 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20817 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020818 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020819 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020820 if (current_SID <= 0)
20821 {
20822 EMSG(_(e_usingsid));
20823 goto theend;
20824 }
20825 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20826 lead += (int)STRLEN(sid_buf);
20827 }
20828 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020829 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020830 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020831 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020832 goto theend;
20833 }
20834 name = alloc((unsigned)(len + lead + 1));
20835 if (name != NULL)
20836 {
20837 if (lead > 0)
20838 {
20839 name[0] = K_SPECIAL;
20840 name[1] = KS_EXTRA;
20841 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020842 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020843 STRCPY(name + 3, sid_buf);
20844 }
20845 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20846 name[len + lead] = NUL;
20847 }
20848 *pp = end;
20849
20850theend:
20851 clear_lval(&lv);
20852 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853}
20854
20855/*
20856 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20857 * Return 2 if "p" starts with "s:".
20858 * Return 0 otherwise.
20859 */
20860 static int
20861eval_fname_script(p)
20862 char_u *p;
20863{
20864 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20865 || STRNICMP(p + 1, "SNR>", 4) == 0))
20866 return 5;
20867 if (p[0] == 's' && p[1] == ':')
20868 return 2;
20869 return 0;
20870}
20871
20872/*
20873 * Return TRUE if "p" starts with "<SID>" or "s:".
20874 * Only works if eval_fname_script() returned non-zero for "p"!
20875 */
20876 static int
20877eval_fname_sid(p)
20878 char_u *p;
20879{
20880 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20881}
20882
20883/*
20884 * List the head of the function: "name(arg1, arg2)".
20885 */
20886 static void
20887list_func_head(fp, indent)
20888 ufunc_T *fp;
20889 int indent;
20890{
20891 int j;
20892
20893 msg_start();
20894 if (indent)
20895 MSG_PUTS(" ");
20896 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020897 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898 {
20899 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020900 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901 }
20902 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020903 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020905 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 {
20907 if (j)
20908 MSG_PUTS(", ");
20909 msg_puts(FUNCARG(fp, j));
20910 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020911 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912 {
20913 if (j)
20914 MSG_PUTS(", ");
20915 MSG_PUTS("...");
20916 }
20917 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020918 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020919 if (p_verbose > 0)
20920 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921}
20922
20923/*
20924 * Find a function by name, return pointer to it in ufuncs.
20925 * Return NULL for unknown function.
20926 */
20927 static ufunc_T *
20928find_func(name)
20929 char_u *name;
20930{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020931 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020933 hi = hash_find(&func_hashtab, name);
20934 if (!HASHITEM_EMPTY(hi))
20935 return HI2UF(hi);
20936 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937}
20938
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020939#if defined(EXITFREE) || defined(PROTO)
20940 void
20941free_all_functions()
20942{
20943 hashitem_T *hi;
20944
20945 /* Need to start all over every time, because func_free() may change the
20946 * hash table. */
20947 while (func_hashtab.ht_used > 0)
20948 for (hi = func_hashtab.ht_array; ; ++hi)
20949 if (!HASHITEM_EMPTY(hi))
20950 {
20951 func_free(HI2UF(hi));
20952 break;
20953 }
20954}
20955#endif
20956
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020957/*
20958 * Return TRUE if a function "name" exists.
20959 */
20960 static int
20961function_exists(name)
20962 char_u *name;
20963{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020964 char_u *nm = name;
20965 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020966 int n = FALSE;
20967
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020968 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020969 nm = skipwhite(nm);
20970
20971 /* Only accept "funcname", "funcname ", "funcname (..." and
20972 * "funcname(...", not "funcname!...". */
20973 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020974 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020975 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020976 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020977 else
20978 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020979 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020980 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020981 return n;
20982}
20983
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020984/*
20985 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020986 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020987 */
20988 static int
20989builtin_function(name)
20990 char_u *name;
20991{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020992 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20993 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020994}
20995
Bram Moolenaar05159a02005-02-26 23:04:13 +000020996#if defined(FEAT_PROFILE) || defined(PROTO)
20997/*
20998 * Start profiling function "fp".
20999 */
21000 static void
21001func_do_profile(fp)
21002 ufunc_T *fp;
21003{
21004 fp->uf_tm_count = 0;
21005 profile_zero(&fp->uf_tm_self);
21006 profile_zero(&fp->uf_tm_total);
21007 if (fp->uf_tml_count == NULL)
21008 fp->uf_tml_count = (int *)alloc_clear((unsigned)
21009 (sizeof(int) * fp->uf_lines.ga_len));
21010 if (fp->uf_tml_total == NULL)
21011 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
21012 (sizeof(proftime_T) * fp->uf_lines.ga_len));
21013 if (fp->uf_tml_self == NULL)
21014 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
21015 (sizeof(proftime_T) * fp->uf_lines.ga_len));
21016 fp->uf_tml_idx = -1;
21017 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21018 || fp->uf_tml_self == NULL)
21019 return; /* out of memory */
21020
21021 fp->uf_profiling = TRUE;
21022}
21023
21024/*
21025 * Dump the profiling results for all functions in file "fd".
21026 */
21027 void
21028func_dump_profile(fd)
21029 FILE *fd;
21030{
21031 hashitem_T *hi;
21032 int todo;
21033 ufunc_T *fp;
21034 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021035 ufunc_T **sorttab;
21036 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021037
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021038 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021039 if (todo == 0)
21040 return; /* nothing to dump */
21041
Bram Moolenaar73830342005-02-28 22:48:19 +000021042 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21043
Bram Moolenaar05159a02005-02-26 23:04:13 +000021044 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21045 {
21046 if (!HASHITEM_EMPTY(hi))
21047 {
21048 --todo;
21049 fp = HI2UF(hi);
21050 if (fp->uf_profiling)
21051 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021052 if (sorttab != NULL)
21053 sorttab[st_len++] = fp;
21054
Bram Moolenaar05159a02005-02-26 23:04:13 +000021055 if (fp->uf_name[0] == K_SPECIAL)
21056 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21057 else
21058 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21059 if (fp->uf_tm_count == 1)
21060 fprintf(fd, "Called 1 time\n");
21061 else
21062 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21063 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21064 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21065 fprintf(fd, "\n");
21066 fprintf(fd, "count total (s) self (s)\n");
21067
21068 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21069 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021070 if (FUNCLINE(fp, i) == NULL)
21071 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021072 prof_func_line(fd, fp->uf_tml_count[i],
21073 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021074 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21075 }
21076 fprintf(fd, "\n");
21077 }
21078 }
21079 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021080
21081 if (sorttab != NULL && st_len > 0)
21082 {
21083 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21084 prof_total_cmp);
21085 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21086 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21087 prof_self_cmp);
21088 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21089 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021090
21091 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021092}
Bram Moolenaar73830342005-02-28 22:48:19 +000021093
21094 static void
21095prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21096 FILE *fd;
21097 ufunc_T **sorttab;
21098 int st_len;
21099 char *title;
21100 int prefer_self; /* when equal print only self time */
21101{
21102 int i;
21103 ufunc_T *fp;
21104
21105 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21106 fprintf(fd, "count total (s) self (s) function\n");
21107 for (i = 0; i < 20 && i < st_len; ++i)
21108 {
21109 fp = sorttab[i];
21110 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21111 prefer_self);
21112 if (fp->uf_name[0] == K_SPECIAL)
21113 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21114 else
21115 fprintf(fd, " %s()\n", fp->uf_name);
21116 }
21117 fprintf(fd, "\n");
21118}
21119
21120/*
21121 * Print the count and times for one function or function line.
21122 */
21123 static void
21124prof_func_line(fd, count, total, self, prefer_self)
21125 FILE *fd;
21126 int count;
21127 proftime_T *total;
21128 proftime_T *self;
21129 int prefer_self; /* when equal print only self time */
21130{
21131 if (count > 0)
21132 {
21133 fprintf(fd, "%5d ", count);
21134 if (prefer_self && profile_equal(total, self))
21135 fprintf(fd, " ");
21136 else
21137 fprintf(fd, "%s ", profile_msg(total));
21138 if (!prefer_self && profile_equal(total, self))
21139 fprintf(fd, " ");
21140 else
21141 fprintf(fd, "%s ", profile_msg(self));
21142 }
21143 else
21144 fprintf(fd, " ");
21145}
21146
21147/*
21148 * Compare function for total time sorting.
21149 */
21150 static int
21151#ifdef __BORLANDC__
21152_RTLENTRYF
21153#endif
21154prof_total_cmp(s1, s2)
21155 const void *s1;
21156 const void *s2;
21157{
21158 ufunc_T *p1, *p2;
21159
21160 p1 = *(ufunc_T **)s1;
21161 p2 = *(ufunc_T **)s2;
21162 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21163}
21164
21165/*
21166 * Compare function for self time sorting.
21167 */
21168 static int
21169#ifdef __BORLANDC__
21170_RTLENTRYF
21171#endif
21172prof_self_cmp(s1, s2)
21173 const void *s1;
21174 const void *s2;
21175{
21176 ufunc_T *p1, *p2;
21177
21178 p1 = *(ufunc_T **)s1;
21179 p2 = *(ufunc_T **)s2;
21180 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21181}
21182
Bram Moolenaar05159a02005-02-26 23:04:13 +000021183#endif
21184
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021185/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021186 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021187 * Return TRUE if a package was loaded.
21188 */
21189 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021190script_autoload(name, reload)
21191 char_u *name;
21192 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021193{
21194 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021195 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021196 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021197 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021198
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021199 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021200 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021201 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021202 return FALSE;
21203
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021204 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021205
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021206 /* Find the name in the list of previously loaded package names. Skip
21207 * "autoload/", it's always the same. */
21208 for (i = 0; i < ga_loaded.ga_len; ++i)
21209 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21210 break;
21211 if (!reload && i < ga_loaded.ga_len)
21212 ret = FALSE; /* was loaded already */
21213 else
21214 {
21215 /* Remember the name if it wasn't loaded already. */
21216 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21217 {
21218 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21219 tofree = NULL;
21220 }
21221
21222 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021223 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021224 ret = TRUE;
21225 }
21226
21227 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021228 return ret;
21229}
21230
21231/*
21232 * Return the autoload script name for a function or variable name.
21233 * Returns NULL when out of memory.
21234 */
21235 static char_u *
21236autoload_name(name)
21237 char_u *name;
21238{
21239 char_u *p;
21240 char_u *scriptname;
21241
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021242 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021243 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21244 if (scriptname == NULL)
21245 return FALSE;
21246 STRCPY(scriptname, "autoload/");
21247 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021248 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021249 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021250 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021251 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021252 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021253}
21254
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21256
21257/*
21258 * Function given to ExpandGeneric() to obtain the list of user defined
21259 * function names.
21260 */
21261 char_u *
21262get_user_func_name(xp, idx)
21263 expand_T *xp;
21264 int idx;
21265{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021266 static long_u done;
21267 static hashitem_T *hi;
21268 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269
21270 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021272 done = 0;
21273 hi = func_hashtab.ht_array;
21274 }
21275 if (done < func_hashtab.ht_used)
21276 {
21277 if (done++ > 0)
21278 ++hi;
21279 while (HASHITEM_EMPTY(hi))
21280 ++hi;
21281 fp = HI2UF(hi);
21282
21283 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21284 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021285
21286 cat_func_name(IObuff, fp);
21287 if (xp->xp_context != EXPAND_USER_FUNC)
21288 {
21289 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021290 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291 STRCAT(IObuff, ")");
21292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293 return IObuff;
21294 }
21295 return NULL;
21296}
21297
21298#endif /* FEAT_CMDL_COMPL */
21299
21300/*
21301 * Copy the function name of "fp" to buffer "buf".
21302 * "buf" must be able to hold the function name plus three bytes.
21303 * Takes care of script-local function names.
21304 */
21305 static void
21306cat_func_name(buf, fp)
21307 char_u *buf;
21308 ufunc_T *fp;
21309{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021310 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311 {
21312 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021313 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 }
21315 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021316 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317}
21318
21319/*
21320 * ":delfunction {name}"
21321 */
21322 void
21323ex_delfunction(eap)
21324 exarg_T *eap;
21325{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021326 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 char_u *p;
21328 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021329 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021330
21331 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021332 name = trans_function_name(&p, eap->skip, 0, &fudi);
21333 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021335 {
21336 if (fudi.fd_dict != NULL && !eap->skip)
21337 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021340 if (!ends_excmd(*skipwhite(p)))
21341 {
21342 vim_free(name);
21343 EMSG(_(e_trailing));
21344 return;
21345 }
21346 eap->nextcmd = check_nextcmd(p);
21347 if (eap->nextcmd != NULL)
21348 *p = NUL;
21349
21350 if (!eap->skip)
21351 fp = find_func(name);
21352 vim_free(name);
21353
21354 if (!eap->skip)
21355 {
21356 if (fp == NULL)
21357 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021358 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 return;
21360 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021361 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362 {
21363 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21364 return;
21365 }
21366
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021367 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021369 /* Delete the dict item that refers to the function, it will
21370 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021371 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021373 else
21374 func_free(fp);
21375 }
21376}
21377
21378/*
21379 * Free a function and remove it from the list of functions.
21380 */
21381 static void
21382func_free(fp)
21383 ufunc_T *fp;
21384{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021385 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021386
21387 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021388 ga_clear_strings(&(fp->uf_args));
21389 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021390#ifdef FEAT_PROFILE
21391 vim_free(fp->uf_tml_count);
21392 vim_free(fp->uf_tml_total);
21393 vim_free(fp->uf_tml_self);
21394#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021395
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021396 /* remove the function from the function hashtable */
21397 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21398 if (HASHITEM_EMPTY(hi))
21399 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021400 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021401 hash_remove(&func_hashtab, hi);
21402
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021403 vim_free(fp);
21404}
21405
21406/*
21407 * Unreference a Function: decrement the reference count and free it when it
21408 * becomes zero. Only for numbered functions.
21409 */
21410 static void
21411func_unref(name)
21412 char_u *name;
21413{
21414 ufunc_T *fp;
21415
21416 if (name != NULL && isdigit(*name))
21417 {
21418 fp = find_func(name);
21419 if (fp == NULL)
21420 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021421 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021422 {
21423 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021424 * when "uf_calls" becomes zero. */
21425 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021426 func_free(fp);
21427 }
21428 }
21429}
21430
21431/*
21432 * Count a reference to a Function.
21433 */
21434 static void
21435func_ref(name)
21436 char_u *name;
21437{
21438 ufunc_T *fp;
21439
21440 if (name != NULL && isdigit(*name))
21441 {
21442 fp = find_func(name);
21443 if (fp == NULL)
21444 EMSG2(_(e_intern2), "func_ref()");
21445 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021446 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447 }
21448}
21449
21450/*
21451 * Call a user function.
21452 */
21453 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021454call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 ufunc_T *fp; /* pointer to function */
21456 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021457 typval_T *argvars; /* arguments */
21458 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021459 linenr_T firstline; /* first line of range */
21460 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021461 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021462{
Bram Moolenaar33570922005-01-25 22:26:29 +000021463 char_u *save_sourcing_name;
21464 linenr_T save_sourcing_lnum;
21465 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021466 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021467 int save_did_emsg;
21468 static int depth = 0;
21469 dictitem_T *v;
21470 int fixvar_idx = 0; /* index in fixvar[] */
21471 int i;
21472 int ai;
21473 char_u numbuf[NUMBUFLEN];
21474 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021475#ifdef FEAT_PROFILE
21476 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021477 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479
21480 /* If depth of calling is getting too high, don't execute the function */
21481 if (depth >= p_mfd)
21482 {
21483 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021484 rettv->v_type = VAR_NUMBER;
21485 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 return;
21487 }
21488 ++depth;
21489
21490 line_breakcheck(); /* check for CTRL-C hit */
21491
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021492 fc = (funccall_T *)alloc(sizeof(funccall_T));
21493 fc->caller = current_funccal;
21494 current_funccal = fc;
21495 fc->func = fp;
21496 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021497 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021498 fc->linenr = 0;
21499 fc->returned = FALSE;
21500 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021502 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21503 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504
Bram Moolenaar33570922005-01-25 22:26:29 +000021505 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021506 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021507 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21508 * each argument variable and saves a lot of time.
21509 */
21510 /*
21511 * Init l: variables.
21512 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021513 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021514 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021515 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021516 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21517 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021518 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021519 name = v->di_key;
21520 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021521 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021522 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021523 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021524 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021525 v->di_tv.vval.v_dict = selfdict;
21526 ++selfdict->dv_refcount;
21527 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021528
Bram Moolenaar33570922005-01-25 22:26:29 +000021529 /*
21530 * Init a: variables.
21531 * Set a:0 to "argcount".
21532 * Set a:000 to a list with room for the "..." arguments.
21533 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021534 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21535 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021536 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021537 /* Use "name" to avoid a warning from some compiler that checks the
21538 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021539 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021540 name = v->di_key;
21541 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021542 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021543 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021544 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021545 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021546 v->di_tv.vval.v_list = &fc->l_varlist;
21547 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21548 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21549 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021550
21551 /*
21552 * Set a:firstline to "firstline" and a:lastline to "lastline".
21553 * Set a:name to named arguments.
21554 * Set a:N to the "..." arguments.
21555 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021556 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021557 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021558 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021559 (varnumber_T)lastline);
21560 for (i = 0; i < argcount; ++i)
21561 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021562 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021563 if (ai < 0)
21564 /* named argument a:name */
21565 name = FUNCARG(fp, i);
21566 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021567 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021568 /* "..." argument a:1, a:2, etc. */
21569 sprintf((char *)numbuf, "%d", ai + 1);
21570 name = numbuf;
21571 }
21572 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21573 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021574 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021575 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21576 }
21577 else
21578 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021579 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21580 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021581 if (v == NULL)
21582 break;
21583 v->di_flags = DI_FLAGS_RO;
21584 }
21585 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021586 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021587
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021588 /* Note: the values are copied directly to avoid alloc/free.
21589 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021590 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021591 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021592
21593 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21594 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021595 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21596 fc->l_listitems[ai].li_tv = argvars[i];
21597 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021598 }
21599 }
21600
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601 /* Don't redraw while executing the function. */
21602 ++RedrawingDisabled;
21603 save_sourcing_name = sourcing_name;
21604 save_sourcing_lnum = sourcing_lnum;
21605 sourcing_lnum = 1;
21606 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021607 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 if (sourcing_name != NULL)
21609 {
21610 if (save_sourcing_name != NULL
21611 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21612 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21613 else
21614 STRCPY(sourcing_name, "function ");
21615 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21616
21617 if (p_verbose >= 12)
21618 {
21619 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021620 verbose_enter_scroll();
21621
Bram Moolenaar555b2802005-05-19 21:08:39 +000021622 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623 if (p_verbose >= 14)
21624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021626 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021627 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021628 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629
21630 msg_puts((char_u *)"(");
21631 for (i = 0; i < argcount; ++i)
21632 {
21633 if (i > 0)
21634 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021635 if (argvars[i].v_type == VAR_NUMBER)
21636 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637 else
21638 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021639 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21640 if (s != NULL)
21641 {
21642 trunc_string(s, buf, MSG_BUF_CLEN);
21643 msg_puts(buf);
21644 vim_free(tofree);
21645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646 }
21647 }
21648 msg_puts((char_u *)")");
21649 }
21650 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021651
21652 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653 --no_wait_return;
21654 }
21655 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021656#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021657 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021658 {
21659 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21660 func_do_profile(fp);
21661 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021662 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021663 {
21664 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021665 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021666 profile_zero(&fp->uf_tm_children);
21667 }
21668 script_prof_save(&wait_start);
21669 }
21670#endif
21671
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021673 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 save_did_emsg = did_emsg;
21675 did_emsg = FALSE;
21676
21677 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021678 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21680
21681 --RedrawingDisabled;
21682
21683 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021684 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021686 clear_tv(rettv);
21687 rettv->v_type = VAR_NUMBER;
21688 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021689 }
21690
Bram Moolenaar05159a02005-02-26 23:04:13 +000021691#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021692 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021693 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021694 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021695 profile_end(&call_start);
21696 profile_sub_wait(&wait_start, &call_start);
21697 profile_add(&fp->uf_tm_total, &call_start);
21698 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021699 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021700 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021701 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21702 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021703 }
21704 }
21705#endif
21706
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707 /* when being verbose, mention the return value */
21708 if (p_verbose >= 12)
21709 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021711 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021712
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021714 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021715 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021716 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021717 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021718 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021719 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021720 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021721 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021722 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021723 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021724
Bram Moolenaar555b2802005-05-19 21:08:39 +000021725 /* The value may be very long. Skip the middle part, so that we
21726 * have some idea how it starts and ends. smsg() would always
21727 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021728 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021729 if (s != NULL)
21730 {
21731 trunc_string(s, buf, MSG_BUF_CLEN);
21732 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21733 vim_free(tofree);
21734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735 }
21736 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021737
21738 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739 --no_wait_return;
21740 }
21741
21742 vim_free(sourcing_name);
21743 sourcing_name = save_sourcing_name;
21744 sourcing_lnum = save_sourcing_lnum;
21745 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021746#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021747 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021748 script_prof_restore(&wait_start);
21749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750
21751 if (p_verbose >= 12 && sourcing_name != NULL)
21752 {
21753 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021754 verbose_enter_scroll();
21755
Bram Moolenaar555b2802005-05-19 21:08:39 +000021756 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021758
21759 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760 --no_wait_return;
21761 }
21762
21763 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021764 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021766
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021767 /* If the a:000 list and the l: and a: dicts are not referenced we can
21768 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021769 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21770 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21771 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21772 {
21773 free_funccal(fc, FALSE);
21774 }
21775 else
21776 {
21777 hashitem_T *hi;
21778 listitem_T *li;
21779 int todo;
21780
21781 /* "fc" is still in use. This can happen when returning "a:000" or
21782 * assigning "l:" to a global variable.
21783 * Link "fc" in the list for garbage collection later. */
21784 fc->caller = previous_funccal;
21785 previous_funccal = fc;
21786
21787 /* Make a copy of the a: variables, since we didn't do that above. */
21788 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21789 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21790 {
21791 if (!HASHITEM_EMPTY(hi))
21792 {
21793 --todo;
21794 v = HI2DI(hi);
21795 copy_tv(&v->di_tv, &v->di_tv);
21796 }
21797 }
21798
21799 /* Make a copy of the a:000 items, since we didn't do that above. */
21800 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21801 copy_tv(&li->li_tv, &li->li_tv);
21802 }
21803}
21804
21805/*
21806 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021807 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021808 */
21809 static int
21810can_free_funccal(fc, copyID)
21811 funccall_T *fc;
21812 int copyID;
21813{
21814 return (fc->l_varlist.lv_copyID != copyID
21815 && fc->l_vars.dv_copyID != copyID
21816 && fc->l_avars.dv_copyID != copyID);
21817}
21818
21819/*
21820 * Free "fc" and what it contains.
21821 */
21822 static void
21823free_funccal(fc, free_val)
21824 funccall_T *fc;
21825 int free_val; /* a: vars were allocated */
21826{
21827 listitem_T *li;
21828
21829 /* The a: variables typevals may not have been allocated, only free the
21830 * allocated variables. */
21831 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21832
21833 /* free all l: variables */
21834 vars_clear(&fc->l_vars.dv_hashtab);
21835
21836 /* Free the a:000 variables if they were allocated. */
21837 if (free_val)
21838 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21839 clear_tv(&li->li_tv);
21840
21841 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842}
21843
21844/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021845 * Add a number variable "name" to dict "dp" with value "nr".
21846 */
21847 static void
21848add_nr_var(dp, v, name, nr)
21849 dict_T *dp;
21850 dictitem_T *v;
21851 char *name;
21852 varnumber_T nr;
21853{
21854 STRCPY(v->di_key, name);
21855 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21856 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21857 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021858 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021859 v->di_tv.vval.v_number = nr;
21860}
21861
21862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 * ":return [expr]"
21864 */
21865 void
21866ex_return(eap)
21867 exarg_T *eap;
21868{
21869 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021870 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871 int returning = FALSE;
21872
21873 if (current_funccal == NULL)
21874 {
21875 EMSG(_("E133: :return not inside a function"));
21876 return;
21877 }
21878
21879 if (eap->skip)
21880 ++emsg_skip;
21881
21882 eap->nextcmd = NULL;
21883 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021884 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 {
21886 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021887 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021889 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021890 }
21891 /* It's safer to return also on error. */
21892 else if (!eap->skip)
21893 {
21894 /*
21895 * Return unless the expression evaluation has been cancelled due to an
21896 * aborting error, an interrupt, or an exception.
21897 */
21898 if (!aborting())
21899 returning = do_return(eap, FALSE, TRUE, NULL);
21900 }
21901
21902 /* When skipping or the return gets pending, advance to the next command
21903 * in this line (!returning). Otherwise, ignore the rest of the line.
21904 * Following lines will be ignored by get_func_line(). */
21905 if (returning)
21906 eap->nextcmd = NULL;
21907 else if (eap->nextcmd == NULL) /* no argument */
21908 eap->nextcmd = check_nextcmd(arg);
21909
21910 if (eap->skip)
21911 --emsg_skip;
21912}
21913
21914/*
21915 * Return from a function. Possibly makes the return pending. Also called
21916 * for a pending return at the ":endtry" or after returning from an extra
21917 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021918 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021919 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 * FALSE when the return gets pending.
21921 */
21922 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021923do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924 exarg_T *eap;
21925 int reanimate;
21926 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021927 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928{
21929 int idx;
21930 struct condstack *cstack = eap->cstack;
21931
21932 if (reanimate)
21933 /* Undo the return. */
21934 current_funccal->returned = FALSE;
21935
21936 /*
21937 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21938 * not in its finally clause (which then is to be executed next) is found.
21939 * In this case, make the ":return" pending for execution at the ":endtry".
21940 * Otherwise, return normally.
21941 */
21942 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21943 if (idx >= 0)
21944 {
21945 cstack->cs_pending[idx] = CSTP_RETURN;
21946
21947 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021948 /* A pending return again gets pending. "rettv" points to an
21949 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021951 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021952 else
21953 {
21954 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021955 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021956 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021957 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021959 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021960 {
21961 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021962 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021963 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021964 else
21965 EMSG(_(e_outofmem));
21966 }
21967 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021968 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021969
21970 if (reanimate)
21971 {
21972 /* The pending return value could be overwritten by a ":return"
21973 * without argument in a finally clause; reset the default
21974 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021975 current_funccal->rettv->v_type = VAR_NUMBER;
21976 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977 }
21978 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021979 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980 }
21981 else
21982 {
21983 current_funccal->returned = TRUE;
21984
21985 /* If the return is carried out now, store the return value. For
21986 * a return immediately after reanimation, the value is already
21987 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021988 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021990 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021991 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021993 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 }
21995 }
21996
21997 return idx < 0;
21998}
21999
22000/*
22001 * Free the variable with a pending return value.
22002 */
22003 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022004discard_pending_return(rettv)
22005 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006{
Bram Moolenaar33570922005-01-25 22:26:29 +000022007 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008}
22009
22010/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022011 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012 * is an allocated string. Used by report_pending() for verbose messages.
22013 */
22014 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022015get_return_cmd(rettv)
22016 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022017{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022018 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022019 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022020 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022022 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022023 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022024 if (s == NULL)
22025 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022026
22027 STRCPY(IObuff, ":return ");
22028 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22029 if (STRLEN(s) + 8 >= IOSIZE)
22030 STRCPY(IObuff + IOSIZE - 4, "...");
22031 vim_free(tofree);
22032 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033}
22034
22035/*
22036 * Get next function line.
22037 * Called by do_cmdline() to get the next line.
22038 * Returns allocated string, or NULL for end of function.
22039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040 char_u *
22041get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022042 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022044 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045{
Bram Moolenaar33570922005-01-25 22:26:29 +000022046 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022047 ufunc_T *fp = fcp->func;
22048 char_u *retval;
22049 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050
22051 /* If breakpoints have been added/deleted need to check for it. */
22052 if (fcp->dbg_tick != debug_tick)
22053 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022054 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055 sourcing_lnum);
22056 fcp->dbg_tick = debug_tick;
22057 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022058#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022059 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022060 func_line_end(cookie);
22061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022062
Bram Moolenaar05159a02005-02-26 23:04:13 +000022063 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022064 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22065 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066 retval = NULL;
22067 else
22068 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022069 /* Skip NULL lines (continuation lines). */
22070 while (fcp->linenr < gap->ga_len
22071 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22072 ++fcp->linenr;
22073 if (fcp->linenr >= gap->ga_len)
22074 retval = NULL;
22075 else
22076 {
22077 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22078 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022079#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022080 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022081 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022082#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084 }
22085
22086 /* Did we encounter a breakpoint? */
22087 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22088 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022089 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022091 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 sourcing_lnum);
22093 fcp->dbg_tick = debug_tick;
22094 }
22095
22096 return retval;
22097}
22098
Bram Moolenaar05159a02005-02-26 23:04:13 +000022099#if defined(FEAT_PROFILE) || defined(PROTO)
22100/*
22101 * Called when starting to read a function line.
22102 * "sourcing_lnum" must be correct!
22103 * When skipping lines it may not actually be executed, but we won't find out
22104 * until later and we need to store the time now.
22105 */
22106 void
22107func_line_start(cookie)
22108 void *cookie;
22109{
22110 funccall_T *fcp = (funccall_T *)cookie;
22111 ufunc_T *fp = fcp->func;
22112
22113 if (fp->uf_profiling && sourcing_lnum >= 1
22114 && sourcing_lnum <= fp->uf_lines.ga_len)
22115 {
22116 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022117 /* Skip continuation lines. */
22118 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22119 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022120 fp->uf_tml_execed = FALSE;
22121 profile_start(&fp->uf_tml_start);
22122 profile_zero(&fp->uf_tml_children);
22123 profile_get_wait(&fp->uf_tml_wait);
22124 }
22125}
22126
22127/*
22128 * Called when actually executing a function line.
22129 */
22130 void
22131func_line_exec(cookie)
22132 void *cookie;
22133{
22134 funccall_T *fcp = (funccall_T *)cookie;
22135 ufunc_T *fp = fcp->func;
22136
22137 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22138 fp->uf_tml_execed = TRUE;
22139}
22140
22141/*
22142 * Called when done with a function line.
22143 */
22144 void
22145func_line_end(cookie)
22146 void *cookie;
22147{
22148 funccall_T *fcp = (funccall_T *)cookie;
22149 ufunc_T *fp = fcp->func;
22150
22151 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22152 {
22153 if (fp->uf_tml_execed)
22154 {
22155 ++fp->uf_tml_count[fp->uf_tml_idx];
22156 profile_end(&fp->uf_tml_start);
22157 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022158 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022159 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22160 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022161 }
22162 fp->uf_tml_idx = -1;
22163 }
22164}
22165#endif
22166
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167/*
22168 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022169 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170 */
22171 int
22172func_has_ended(cookie)
22173 void *cookie;
22174{
Bram Moolenaar33570922005-01-25 22:26:29 +000022175 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022176
22177 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22178 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022179 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180 || fcp->returned);
22181}
22182
22183/*
22184 * return TRUE if cookie indicates a function which "abort"s on errors.
22185 */
22186 int
22187func_has_abort(cookie)
22188 void *cookie;
22189{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022190 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191}
22192
22193#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22194typedef enum
22195{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022196 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22197 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22198 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199} var_flavour_T;
22200
22201static var_flavour_T var_flavour __ARGS((char_u *varname));
22202
22203 static var_flavour_T
22204var_flavour(varname)
22205 char_u *varname;
22206{
22207 char_u *p = varname;
22208
22209 if (ASCII_ISUPPER(*p))
22210 {
22211 while (*(++p))
22212 if (ASCII_ISLOWER(*p))
22213 return VAR_FLAVOUR_SESSION;
22214 return VAR_FLAVOUR_VIMINFO;
22215 }
22216 else
22217 return VAR_FLAVOUR_DEFAULT;
22218}
22219#endif
22220
22221#if defined(FEAT_VIMINFO) || defined(PROTO)
22222/*
22223 * Restore global vars that start with a capital from the viminfo file
22224 */
22225 int
22226read_viminfo_varlist(virp, writing)
22227 vir_T *virp;
22228 int writing;
22229{
22230 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022231 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022232 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233
22234 if (!writing && (find_viminfo_parameter('!') != NULL))
22235 {
22236 tab = vim_strchr(virp->vir_line + 1, '\t');
22237 if (tab != NULL)
22238 {
22239 *tab++ = '\0'; /* isolate the variable name */
22240 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022241 type = VAR_STRING;
22242#ifdef FEAT_FLOAT
22243 else if (*tab == 'F')
22244 type = VAR_FLOAT;
22245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246
22247 tab = vim_strchr(tab, '\t');
22248 if (tab != NULL)
22249 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022250 tv.v_type = type;
22251 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022252 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022254#ifdef FEAT_FLOAT
22255 else if (type == VAR_FLOAT)
22256 (void)string2float(tab + 1, &tv.vval.v_float);
22257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022258 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022259 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022260 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022261 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022262 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263 }
22264 }
22265 }
22266
22267 return viminfo_readline(virp);
22268}
22269
22270/*
22271 * Write global vars that start with a capital to the viminfo file
22272 */
22273 void
22274write_viminfo_varlist(fp)
22275 FILE *fp;
22276{
Bram Moolenaar33570922005-01-25 22:26:29 +000022277 hashitem_T *hi;
22278 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022279 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022280 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022281 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022282 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022283 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284
22285 if (find_viminfo_parameter('!') == NULL)
22286 return;
22287
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022288 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022289
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022290 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022291 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022293 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022295 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022296 this_var = HI2DI(hi);
22297 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022298 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022299 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022300 {
22301 case VAR_STRING: s = "STR"; break;
22302 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022303#ifdef FEAT_FLOAT
22304 case VAR_FLOAT: s = "FLO"; break;
22305#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022306 default: continue;
22307 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022308 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022309 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022310 if (p != NULL)
22311 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022312 vim_free(tofree);
22313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314 }
22315 }
22316}
22317#endif
22318
22319#if defined(FEAT_SESSION) || defined(PROTO)
22320 int
22321store_session_globals(fd)
22322 FILE *fd;
22323{
Bram Moolenaar33570922005-01-25 22:26:29 +000022324 hashitem_T *hi;
22325 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022326 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327 char_u *p, *t;
22328
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022329 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022330 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022331 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022332 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022333 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022334 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022335 this_var = HI2DI(hi);
22336 if ((this_var->di_tv.v_type == VAR_NUMBER
22337 || this_var->di_tv.v_type == VAR_STRING)
22338 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022339 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022340 /* Escape special characters with a backslash. Turn a LF and
22341 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022342 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022343 (char_u *)"\\\"\n\r");
22344 if (p == NULL) /* out of memory */
22345 break;
22346 for (t = p; *t != NUL; ++t)
22347 if (*t == '\n')
22348 *t = 'n';
22349 else if (*t == '\r')
22350 *t = 'r';
22351 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022352 this_var->di_key,
22353 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22354 : ' ',
22355 p,
22356 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22357 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022358 || put_eol(fd) == FAIL)
22359 {
22360 vim_free(p);
22361 return FAIL;
22362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363 vim_free(p);
22364 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022365#ifdef FEAT_FLOAT
22366 else if (this_var->di_tv.v_type == VAR_FLOAT
22367 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22368 {
22369 float_T f = this_var->di_tv.vval.v_float;
22370 int sign = ' ';
22371
22372 if (f < 0)
22373 {
22374 f = -f;
22375 sign = '-';
22376 }
22377 if ((fprintf(fd, "let %s = %c&%f",
22378 this_var->di_key, sign, f) < 0)
22379 || put_eol(fd) == FAIL)
22380 return FAIL;
22381 }
22382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383 }
22384 }
22385 return OK;
22386}
22387#endif
22388
Bram Moolenaar661b1822005-07-28 22:36:45 +000022389/*
22390 * Display script name where an item was last set.
22391 * Should only be invoked when 'verbose' is non-zero.
22392 */
22393 void
22394last_set_msg(scriptID)
22395 scid_T scriptID;
22396{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022397 char_u *p;
22398
Bram Moolenaar661b1822005-07-28 22:36:45 +000022399 if (scriptID != 0)
22400 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022401 p = home_replace_save(NULL, get_scriptname(scriptID));
22402 if (p != NULL)
22403 {
22404 verbose_enter();
22405 MSG_PUTS(_("\n\tLast set from "));
22406 MSG_PUTS(p);
22407 vim_free(p);
22408 verbose_leave();
22409 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022410 }
22411}
22412
Bram Moolenaard812df62008-11-09 12:46:09 +000022413/*
22414 * List v:oldfiles in a nice way.
22415 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022416 void
22417ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022418 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022419{
22420 list_T *l = vimvars[VV_OLDFILES].vv_list;
22421 listitem_T *li;
22422 int nr = 0;
22423
22424 if (l == NULL)
22425 msg((char_u *)_("No old files"));
22426 else
22427 {
22428 msg_start();
22429 msg_scroll = TRUE;
22430 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22431 {
22432 msg_outnum((long)++nr);
22433 MSG_PUTS(": ");
22434 msg_outtrans(get_tv_string(&li->li_tv));
22435 msg_putchar('\n');
22436 out_flush(); /* output one line at a time */
22437 ui_breakcheck();
22438 }
22439 /* Assume "got_int" was set to truncate the listing. */
22440 got_int = FALSE;
22441
22442#ifdef FEAT_BROWSE_CMD
22443 if (cmdmod.browse)
22444 {
22445 quit_more = FALSE;
22446 nr = prompt_for_number(FALSE);
22447 msg_starthere();
22448 if (nr > 0)
22449 {
22450 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22451 (long)nr);
22452
22453 if (p != NULL)
22454 {
22455 p = expand_env_save(p);
22456 eap->arg = p;
22457 eap->cmdidx = CMD_edit;
22458 cmdmod.browse = FALSE;
22459 do_exedit(eap, NULL);
22460 vim_free(p);
22461 }
22462 }
22463 }
22464#endif
22465 }
22466}
22467
Bram Moolenaar071d4272004-06-13 20:20:40 +000022468#endif /* FEAT_EVAL */
22469
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022471#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472
22473#ifdef WIN3264
22474/*
22475 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22476 */
22477static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22478static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22479static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22480
22481/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022482 * Get the short path (8.3) for the filename in "fnamep".
22483 * Only works for a valid file name.
22484 * When the path gets longer "fnamep" is changed and the allocated buffer
22485 * is put in "bufp".
22486 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22487 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488 */
22489 static int
22490get_short_pathname(fnamep, bufp, fnamelen)
22491 char_u **fnamep;
22492 char_u **bufp;
22493 int *fnamelen;
22494{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022495 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496 char_u *newbuf;
22497
22498 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499 l = GetShortPathName(*fnamep, *fnamep, len);
22500 if (l > len - 1)
22501 {
22502 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022503 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504 newbuf = vim_strnsave(*fnamep, l);
22505 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022506 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507
22508 vim_free(*bufp);
22509 *fnamep = *bufp = newbuf;
22510
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022511 /* Really should always succeed, as the buffer is big enough. */
22512 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022513 }
22514
22515 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022516 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517}
22518
22519/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022520 * Get the short path (8.3) for the filename in "fname". The converted
22521 * path is returned in "bufp".
22522 *
22523 * Some of the directories specified in "fname" may not exist. This function
22524 * will shorten the existing directories at the beginning of the path and then
22525 * append the remaining non-existing path.
22526 *
22527 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022528 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022529 * bufp - Pointer to an allocated buffer for the filename.
22530 * fnamelen - Length of the filename pointed to by fname
22531 *
22532 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022533 */
22534 static int
22535shortpath_for_invalid_fname(fname, bufp, fnamelen)
22536 char_u **fname;
22537 char_u **bufp;
22538 int *fnamelen;
22539{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022540 char_u *short_fname, *save_fname, *pbuf_unused;
22541 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022543 int old_len, len;
22544 int new_len, sfx_len;
22545 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022546
22547 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022548 old_len = *fnamelen;
22549 save_fname = vim_strnsave(*fname, old_len);
22550 pbuf_unused = NULL;
22551 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022552
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022553 endp = save_fname + old_len - 1; /* Find the end of the copy */
22554 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022555
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022556 /*
22557 * Try shortening the supplied path till it succeeds by removing one
22558 * directory at a time from the tail of the path.
22559 */
22560 len = 0;
22561 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022563 /* go back one path-separator */
22564 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22565 --endp;
22566 if (endp <= save_fname)
22567 break; /* processed the complete path */
22568
22569 /*
22570 * Replace the path separator with a NUL and try to shorten the
22571 * resulting path.
22572 */
22573 ch = *endp;
22574 *endp = 0;
22575 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022576 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022577 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22578 {
22579 retval = FAIL;
22580 goto theend;
22581 }
22582 *endp = ch; /* preserve the string */
22583
22584 if (len > 0)
22585 break; /* successfully shortened the path */
22586
22587 /* failed to shorten the path. Skip the path separator */
22588 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589 }
22590
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022591 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022593 /*
22594 * Succeeded in shortening the path. Now concatenate the shortened
22595 * path with the remaining path at the tail.
22596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022598 /* Compute the length of the new path. */
22599 sfx_len = (int)(save_endp - endp) + 1;
22600 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022602 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022604 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022606 /* There is not enough space in the currently allocated string,
22607 * copy it to a buffer big enough. */
22608 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022610 {
22611 retval = FAIL;
22612 goto theend;
22613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614 }
22615 else
22616 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022617 /* Transfer short_fname to the main buffer (it's big enough),
22618 * unless get_short_pathname() did its work in-place. */
22619 *fname = *bufp = save_fname;
22620 if (short_fname != save_fname)
22621 vim_strncpy(save_fname, short_fname, len);
22622 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022623 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022624
22625 /* concat the not-shortened part of the path */
22626 vim_strncpy(*fname + len, endp, sfx_len);
22627 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022629
22630theend:
22631 vim_free(pbuf_unused);
22632 vim_free(save_fname);
22633
22634 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022635}
22636
22637/*
22638 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022639 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640 */
22641 static int
22642shortpath_for_partial(fnamep, bufp, fnamelen)
22643 char_u **fnamep;
22644 char_u **bufp;
22645 int *fnamelen;
22646{
22647 int sepcount, len, tflen;
22648 char_u *p;
22649 char_u *pbuf, *tfname;
22650 int hasTilde;
22651
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022652 /* Count up the path separators from the RHS.. so we know which part
22653 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022655 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656 if (vim_ispathsep(*p))
22657 ++sepcount;
22658
22659 /* Need full path first (use expand_env() to remove a "~/") */
22660 hasTilde = (**fnamep == '~');
22661 if (hasTilde)
22662 pbuf = tfname = expand_env_save(*fnamep);
22663 else
22664 pbuf = tfname = FullName_save(*fnamep, FALSE);
22665
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022666 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022668 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22669 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670
22671 if (len == 0)
22672 {
22673 /* Don't have a valid filename, so shorten the rest of the
22674 * path if we can. This CAN give us invalid 8.3 filenames, but
22675 * there's not a lot of point in guessing what it might be.
22676 */
22677 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022678 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22679 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 }
22681
22682 /* Count the paths backward to find the beginning of the desired string. */
22683 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022684 {
22685#ifdef FEAT_MBYTE
22686 if (has_mbyte)
22687 p -= mb_head_off(tfname, p);
22688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689 if (vim_ispathsep(*p))
22690 {
22691 if (sepcount == 0 || (hasTilde && sepcount == 1))
22692 break;
22693 else
22694 sepcount --;
22695 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697 if (hasTilde)
22698 {
22699 --p;
22700 if (p >= tfname)
22701 *p = '~';
22702 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022703 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 }
22705 else
22706 ++p;
22707
22708 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22709 vim_free(*bufp);
22710 *fnamelen = (int)STRLEN(p);
22711 *bufp = pbuf;
22712 *fnamep = p;
22713
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022714 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715}
22716#endif /* WIN3264 */
22717
22718/*
22719 * Adjust a filename, according to a string of modifiers.
22720 * *fnamep must be NUL terminated when called. When returning, the length is
22721 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022722 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723 * When there is an error, *fnamep is set to NULL.
22724 */
22725 int
22726modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22727 char_u *src; /* string with modifiers */
22728 int *usedlen; /* characters after src that are used */
22729 char_u **fnamep; /* file name so far */
22730 char_u **bufp; /* buffer for allocated file name or NULL */
22731 int *fnamelen; /* length of fnamep */
22732{
22733 int valid = 0;
22734 char_u *tail;
22735 char_u *s, *p, *pbuf;
22736 char_u dirname[MAXPATHL];
22737 int c;
22738 int has_fullname = 0;
22739#ifdef WIN3264
22740 int has_shortname = 0;
22741#endif
22742
22743repeat:
22744 /* ":p" - full path/file_name */
22745 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22746 {
22747 has_fullname = 1;
22748
22749 valid |= VALID_PATH;
22750 *usedlen += 2;
22751
22752 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22753 if ((*fnamep)[0] == '~'
22754#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22755 && ((*fnamep)[1] == '/'
22756# ifdef BACKSLASH_IN_FILENAME
22757 || (*fnamep)[1] == '\\'
22758# endif
22759 || (*fnamep)[1] == NUL)
22760
22761#endif
22762 )
22763 {
22764 *fnamep = expand_env_save(*fnamep);
22765 vim_free(*bufp); /* free any allocated file name */
22766 *bufp = *fnamep;
22767 if (*fnamep == NULL)
22768 return -1;
22769 }
22770
22771 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022772 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022773 {
22774 if (vim_ispathsep(*p)
22775 && p[1] == '.'
22776 && (p[2] == NUL
22777 || vim_ispathsep(p[2])
22778 || (p[2] == '.'
22779 && (p[3] == NUL || vim_ispathsep(p[3])))))
22780 break;
22781 }
22782
22783 /* FullName_save() is slow, don't use it when not needed. */
22784 if (*p != NUL || !vim_isAbsName(*fnamep))
22785 {
22786 *fnamep = FullName_save(*fnamep, *p != NUL);
22787 vim_free(*bufp); /* free any allocated file name */
22788 *bufp = *fnamep;
22789 if (*fnamep == NULL)
22790 return -1;
22791 }
22792
22793 /* Append a path separator to a directory. */
22794 if (mch_isdir(*fnamep))
22795 {
22796 /* Make room for one or two extra characters. */
22797 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22798 vim_free(*bufp); /* free any allocated file name */
22799 *bufp = *fnamep;
22800 if (*fnamep == NULL)
22801 return -1;
22802 add_pathsep(*fnamep);
22803 }
22804 }
22805
22806 /* ":." - path relative to the current directory */
22807 /* ":~" - path relative to the home directory */
22808 /* ":8" - shortname path - postponed till after */
22809 while (src[*usedlen] == ':'
22810 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22811 {
22812 *usedlen += 2;
22813 if (c == '8')
22814 {
22815#ifdef WIN3264
22816 has_shortname = 1; /* Postpone this. */
22817#endif
22818 continue;
22819 }
22820 pbuf = NULL;
22821 /* Need full path first (use expand_env() to remove a "~/") */
22822 if (!has_fullname)
22823 {
22824 if (c == '.' && **fnamep == '~')
22825 p = pbuf = expand_env_save(*fnamep);
22826 else
22827 p = pbuf = FullName_save(*fnamep, FALSE);
22828 }
22829 else
22830 p = *fnamep;
22831
22832 has_fullname = 0;
22833
22834 if (p != NULL)
22835 {
22836 if (c == '.')
22837 {
22838 mch_dirname(dirname, MAXPATHL);
22839 s = shorten_fname(p, dirname);
22840 if (s != NULL)
22841 {
22842 *fnamep = s;
22843 if (pbuf != NULL)
22844 {
22845 vim_free(*bufp); /* free any allocated file name */
22846 *bufp = pbuf;
22847 pbuf = NULL;
22848 }
22849 }
22850 }
22851 else
22852 {
22853 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22854 /* Only replace it when it starts with '~' */
22855 if (*dirname == '~')
22856 {
22857 s = vim_strsave(dirname);
22858 if (s != NULL)
22859 {
22860 *fnamep = s;
22861 vim_free(*bufp);
22862 *bufp = s;
22863 }
22864 }
22865 }
22866 vim_free(pbuf);
22867 }
22868 }
22869
22870 tail = gettail(*fnamep);
22871 *fnamelen = (int)STRLEN(*fnamep);
22872
22873 /* ":h" - head, remove "/file_name", can be repeated */
22874 /* Don't remove the first "/" or "c:\" */
22875 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22876 {
22877 valid |= VALID_HEAD;
22878 *usedlen += 2;
22879 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022880 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022881 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882 *fnamelen = (int)(tail - *fnamep);
22883#ifdef VMS
22884 if (*fnamelen > 0)
22885 *fnamelen += 1; /* the path separator is part of the path */
22886#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022887 if (*fnamelen == 0)
22888 {
22889 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22890 p = vim_strsave((char_u *)".");
22891 if (p == NULL)
22892 return -1;
22893 vim_free(*bufp);
22894 *bufp = *fnamep = tail = p;
22895 *fnamelen = 1;
22896 }
22897 else
22898 {
22899 while (tail > s && !after_pathsep(s, tail))
22900 mb_ptr_back(*fnamep, tail);
22901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 }
22903
22904 /* ":8" - shortname */
22905 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22906 {
22907 *usedlen += 2;
22908#ifdef WIN3264
22909 has_shortname = 1;
22910#endif
22911 }
22912
22913#ifdef WIN3264
22914 /* Check shortname after we have done 'heads' and before we do 'tails'
22915 */
22916 if (has_shortname)
22917 {
22918 pbuf = NULL;
22919 /* Copy the string if it is shortened by :h */
22920 if (*fnamelen < (int)STRLEN(*fnamep))
22921 {
22922 p = vim_strnsave(*fnamep, *fnamelen);
22923 if (p == 0)
22924 return -1;
22925 vim_free(*bufp);
22926 *bufp = *fnamep = p;
22927 }
22928
22929 /* Split into two implementations - makes it easier. First is where
22930 * there isn't a full name already, second is where there is.
22931 */
22932 if (!has_fullname && !vim_isAbsName(*fnamep))
22933 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022934 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 return -1;
22936 }
22937 else
22938 {
22939 int l;
22940
22941 /* Simple case, already have the full-name
22942 * Nearly always shorter, so try first time. */
22943 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022944 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022945 return -1;
22946
22947 if (l == 0)
22948 {
22949 /* Couldn't find the filename.. search the paths.
22950 */
22951 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022952 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953 return -1;
22954 }
22955 *fnamelen = l;
22956 }
22957 }
22958#endif /* WIN3264 */
22959
22960 /* ":t" - tail, just the basename */
22961 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22962 {
22963 *usedlen += 2;
22964 *fnamelen -= (int)(tail - *fnamep);
22965 *fnamep = tail;
22966 }
22967
22968 /* ":e" - extension, can be repeated */
22969 /* ":r" - root, without extension, can be repeated */
22970 while (src[*usedlen] == ':'
22971 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22972 {
22973 /* find a '.' in the tail:
22974 * - for second :e: before the current fname
22975 * - otherwise: The last '.'
22976 */
22977 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22978 s = *fnamep - 2;
22979 else
22980 s = *fnamep + *fnamelen - 1;
22981 for ( ; s > tail; --s)
22982 if (s[0] == '.')
22983 break;
22984 if (src[*usedlen + 1] == 'e') /* :e */
22985 {
22986 if (s > tail)
22987 {
22988 *fnamelen += (int)(*fnamep - (s + 1));
22989 *fnamep = s + 1;
22990#ifdef VMS
22991 /* cut version from the extension */
22992 s = *fnamep + *fnamelen - 1;
22993 for ( ; s > *fnamep; --s)
22994 if (s[0] == ';')
22995 break;
22996 if (s > *fnamep)
22997 *fnamelen = s - *fnamep;
22998#endif
22999 }
23000 else if (*fnamep <= tail)
23001 *fnamelen = 0;
23002 }
23003 else /* :r */
23004 {
23005 if (s > tail) /* remove one extension */
23006 *fnamelen = (int)(s - *fnamep);
23007 }
23008 *usedlen += 2;
23009 }
23010
23011 /* ":s?pat?foo?" - substitute */
23012 /* ":gs?pat?foo?" - global substitute */
23013 if (src[*usedlen] == ':'
23014 && (src[*usedlen + 1] == 's'
23015 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23016 {
23017 char_u *str;
23018 char_u *pat;
23019 char_u *sub;
23020 int sep;
23021 char_u *flags;
23022 int didit = FALSE;
23023
23024 flags = (char_u *)"";
23025 s = src + *usedlen + 2;
23026 if (src[*usedlen + 1] == 'g')
23027 {
23028 flags = (char_u *)"g";
23029 ++s;
23030 }
23031
23032 sep = *s++;
23033 if (sep)
23034 {
23035 /* find end of pattern */
23036 p = vim_strchr(s, sep);
23037 if (p != NULL)
23038 {
23039 pat = vim_strnsave(s, (int)(p - s));
23040 if (pat != NULL)
23041 {
23042 s = p + 1;
23043 /* find end of substitution */
23044 p = vim_strchr(s, sep);
23045 if (p != NULL)
23046 {
23047 sub = vim_strnsave(s, (int)(p - s));
23048 str = vim_strnsave(*fnamep, *fnamelen);
23049 if (sub != NULL && str != NULL)
23050 {
23051 *usedlen = (int)(p + 1 - src);
23052 s = do_string_sub(str, pat, sub, flags);
23053 if (s != NULL)
23054 {
23055 *fnamep = s;
23056 *fnamelen = (int)STRLEN(s);
23057 vim_free(*bufp);
23058 *bufp = s;
23059 didit = TRUE;
23060 }
23061 }
23062 vim_free(sub);
23063 vim_free(str);
23064 }
23065 vim_free(pat);
23066 }
23067 }
23068 /* after using ":s", repeat all the modifiers */
23069 if (didit)
23070 goto repeat;
23071 }
23072 }
23073
23074 return valid;
23075}
23076
23077/*
23078 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23079 * "flags" can be "g" to do a global substitute.
23080 * Returns an allocated string, NULL for error.
23081 */
23082 char_u *
23083do_string_sub(str, pat, sub, flags)
23084 char_u *str;
23085 char_u *pat;
23086 char_u *sub;
23087 char_u *flags;
23088{
23089 int sublen;
23090 regmatch_T regmatch;
23091 int i;
23092 int do_all;
23093 char_u *tail;
23094 garray_T ga;
23095 char_u *ret;
23096 char_u *save_cpo;
23097
23098 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23099 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023100 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023101
23102 ga_init2(&ga, 1, 200);
23103
23104 do_all = (flags[0] == 'g');
23105
23106 regmatch.rm_ic = p_ic;
23107 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23108 if (regmatch.regprog != NULL)
23109 {
23110 tail = str;
23111 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23112 {
23113 /*
23114 * Get some space for a temporary buffer to do the substitution
23115 * into. It will contain:
23116 * - The text up to where the match is.
23117 * - The substituted text.
23118 * - The text after the match.
23119 */
23120 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23121 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23122 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23123 {
23124 ga_clear(&ga);
23125 break;
23126 }
23127
23128 /* copy the text up to where the match is */
23129 i = (int)(regmatch.startp[0] - tail);
23130 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23131 /* add the substituted text */
23132 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23133 + ga.ga_len + i, TRUE, TRUE, FALSE);
23134 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135 /* avoid getting stuck on a match with an empty string */
23136 if (tail == regmatch.endp[0])
23137 {
23138 if (*tail == NUL)
23139 break;
23140 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23141 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142 }
23143 else
23144 {
23145 tail = regmatch.endp[0];
23146 if (*tail == NUL)
23147 break;
23148 }
23149 if (!do_all)
23150 break;
23151 }
23152
23153 if (ga.ga_data != NULL)
23154 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23155
23156 vim_free(regmatch.regprog);
23157 }
23158
23159 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23160 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023161 if (p_cpo == empty_option)
23162 p_cpo = save_cpo;
23163 else
23164 /* Darn, evaluating {sub} expression changed the value. */
23165 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166
23167 return ret;
23168}
23169
23170#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */