blob: 1803e0e55dc523b0adc34a5fc41a90bba8040814 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarc236c162008-07-13 17:41:49 +000013#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019#if defined(FEAT_EVAL) || defined(PROTO)
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef AMIGA
22# include <time.h> /* for strftime() */
23#endif
24
25#ifdef MACOS
26# include <time.h> /* for time_t */
27#endif
28
Bram Moolenaar8c8de832008-06-24 22:58:06 +000029#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
30# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000035#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
36 be freed. */
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038/*
Bram Moolenaar33570922005-01-25 22:26:29 +000039 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
40 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000041 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
42 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
43 * HI2DI() converts a hashitem pointer to a dictitem pointer.
44 */
Bram Moolenaar33570922005-01-25 22:26:29 +000045static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000047#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000048#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000049
50/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000051 * Structure returned by get_lval() and used by set_var_lval().
52 * For a plain name:
53 * "name" points to the variable name.
54 * "exp_name" is NULL.
55 * "tv" is NULL
56 * For a magic braces name:
57 * "name" points to the expanded variable name.
58 * "exp_name" is non-NULL, to be freed later.
59 * "tv" is NULL
60 * For an index in a list:
61 * "name" points to the (expanded) variable name.
62 * "exp_name" NULL or non-NULL, to be freed later.
63 * "tv" points to the (first) list item value
64 * "li" points to the (first) list item
65 * "range", "n1", "n2" and "empty2" indicate what items are used.
66 * For an existing Dict item:
67 * "name" points to the (expanded) variable name.
68 * "exp_name" NULL or non-NULL, to be freed later.
69 * "tv" points to the dict item value
70 * "newkey" is NULL
71 * For a non-existing Dict item:
72 * "name" points to the (expanded) variable name.
73 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000074 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000075 * "newkey" is the key for the new item.
76 */
77typedef struct lval_S
78{
79 char_u *ll_name; /* start of variable name (can be NULL) */
80 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000082 isn't NULL it's the Dict to which to add
83 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000084 listitem_T *ll_li; /* The list item or NULL. */
85 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000086 int ll_range; /* TRUE when a [i:j] range was used */
87 long ll_n1; /* First index for list */
88 long ll_n2; /* Second index for list range */
89 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000090 dict_T *ll_dict; /* The Dictionary or NULL */
91 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000092 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000093} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000094
Bram Moolenaar8c711452005-01-14 21:53:12 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000114
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000115/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000116 * All user-defined global variables are stored in dictionary "globvardict".
117 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119static dict_T globvardict;
120static dictitem_T globvars_var;
121#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124 * Old Vim variables such as "v:version" are also available without the "v:".
125 * Also in functions. We need a special hashtable for them.
126 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000127static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000128
129/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000130 * When recursively copying lists and dicts we need to remember which ones we
131 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000132 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133 */
134static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000135#define COPYID_INC 2
136#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137
138/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000139 * Array to hold the hashtab with variables local to each sourced script.
140 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000142typedef struct
143{
144 dictitem_T sv_var;
145 dict_T sv_dict;
146} scriptvar_T;
147
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200148static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
149#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
150#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152static int echo_attr = 0; /* attributes used for ":echo" */
153
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000154/* Values for trans_function_name() argument: */
155#define TFN_INT 1 /* internal function name OK */
156#define TFN_QUIET 2 /* no error messages */
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158/*
159 * Structure to hold info for a user function.
160 */
161typedef struct ufunc ufunc_T;
162
163struct ufunc
164{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000165 int uf_varargs; /* variable nr of arguments */
166 int uf_flags;
167 int uf_calls; /* nr of active calls */
168 garray_T uf_args; /* arguments */
169 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000170#ifdef FEAT_PROFILE
171 int uf_profiling; /* TRUE when func is being profiled */
172 /* profiling the function as a whole */
173 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000174 proftime_T uf_tm_total; /* time spent in function + children */
175 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000176 proftime_T uf_tm_children; /* time spent in children this call */
177 /* profiling the function per line */
178 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000179 proftime_T *uf_tml_total; /* time spent in a line + children */
180 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000181 proftime_T uf_tml_start; /* start time for current line */
182 proftime_T uf_tml_children; /* time spent in children for this line */
183 proftime_T uf_tml_wait; /* start wait time for current line */
184 int uf_tml_idx; /* index of line being timed; -1 if none */
185 int uf_tml_execed; /* line being timed was executed */
186#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000187 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000189 int uf_refcount; /* for numbered function: reference count */
190 char_u uf_name[1]; /* name of function (actually longer); can
191 start with <SNR>123_ (<SNR> is K_SPECIAL
192 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193};
194
195/* function flags */
196#define FC_ABORT 1 /* abort function on error */
197#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000198#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199
200/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000203static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000205/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000206static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
207
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208/* list heads for garbage collection */
209static dict_T *first_dict = NULL; /* list of all dicts */
210static list_T *first_list = NULL; /* list of all lists */
211
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000212/* From user function to hashitem and back. */
213static ufunc_T dumuf;
214#define UF2HIKEY(fp) ((fp)->uf_name)
215#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
216#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
217
218#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
219#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220
Bram Moolenaar33570922005-01-25 22:26:29 +0000221#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
222#define VAR_SHORT_LEN 20 /* short variable name length */
223#define FIXVAR_CNT 12 /* number of fixed variables */
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000226typedef struct funccall_S funccall_T;
227
228struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229{
230 ufunc_T *func; /* function being called */
231 int linenr; /* next line to be executed */
232 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000233 struct /* fixed variables for arguments */
234 {
235 dictitem_T var; /* variable (without room for name) */
236 char_u room[VAR_SHORT_LEN]; /* room for the name */
237 } fixvar[FIXVAR_CNT];
238 dict_T l_vars; /* l: local function variables */
239 dictitem_T l_vars_var; /* variable for l: scope */
240 dict_T l_avars; /* a: argument variables */
241 dictitem_T l_avars_var; /* variable for a: scope */
242 list_T l_varlist; /* list for a:000 */
243 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
244 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 linenr_T breakpoint; /* next line with breakpoint or zero */
246 int dbg_tick; /* debug_tick when breakpoint was set */
247 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000248#ifdef FEAT_PROFILE
249 proftime_T prof_child; /* time spent in a child */
250#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000251 funccall_T *caller; /* calling function or NULL */
252};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253
254/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000255 * Info used by a ":for" loop.
256 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000257typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258{
259 int fi_semicolon; /* TRUE if ending in '; var]' */
260 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261 listwatch_T fi_lw; /* keep an eye on the item used. */
262 list_T *fi_list; /* list being used */
263} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000266 * Struct used by trans_function_name()
267 */
268typedef struct
269{
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000271 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000272 dictitem_T *fd_di; /* Dictionary item used */
273} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000274
Bram Moolenaara7043832005-01-21 11:56:39 +0000275
276/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 * Array to hold the value of v: variables.
278 * The value is in a dictitem, so that it can also be used in the v: scope.
279 * The reason to use this table anyway is for very quick access to the
280 * variables with the VV_ defines.
281 */
282#include "version.h"
283
284/* values for vv_flags: */
285#define VV_COMPAT 1 /* compatible, also used without "v:" */
286#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000287#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000288
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000289#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000290
291static struct vimvar
292{
293 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294 dictitem_T vv_di; /* value and name for key */
295 char vv_filler[16]; /* space for LONGEST name below!!! */
296 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
297} vimvars[VV_LEN] =
298{
299 /*
300 * The order here must match the VV_ defines in vim.h!
301 * Initializing a union does not work, leave tv.vval empty to get zero's.
302 */
303 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
304 {VV_NAME("count1", VAR_NUMBER), VV_RO},
305 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
306 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
307 {VV_NAME("warningmsg", VAR_STRING), 0},
308 {VV_NAME("statusmsg", VAR_STRING), 0},
309 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
311 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
313 {VV_NAME("termresponse", VAR_STRING), VV_RO},
314 {VV_NAME("fname", VAR_STRING), VV_RO},
315 {VV_NAME("lang", VAR_STRING), VV_RO},
316 {VV_NAME("lc_time", VAR_STRING), VV_RO},
317 {VV_NAME("ctype", VAR_STRING), VV_RO},
318 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
319 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
320 {VV_NAME("fname_in", VAR_STRING), VV_RO},
321 {VV_NAME("fname_out", VAR_STRING), VV_RO},
322 {VV_NAME("fname_new", VAR_STRING), VV_RO},
323 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
324 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
325 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
326 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
327 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
328 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
329 {VV_NAME("progname", VAR_STRING), VV_RO},
330 {VV_NAME("servername", VAR_STRING), VV_RO},
331 {VV_NAME("dying", VAR_NUMBER), VV_RO},
332 {VV_NAME("exception", VAR_STRING), VV_RO},
333 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
334 {VV_NAME("register", VAR_STRING), VV_RO},
335 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
336 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000337 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
338 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000339 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000340 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
341 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000342 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
343 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
344 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
345 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
346 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000347 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000348 {VV_NAME("swapname", VAR_STRING), VV_RO},
349 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000350 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000351 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000352 {VV_NAME("mouse_win", VAR_NUMBER), 0},
353 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
354 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000355 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000356 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000357 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000358};
359
360/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000361#define vv_type vv_di.di_tv.v_type
362#define vv_nr vv_di.di_tv.vval.v_number
363#define vv_float vv_di.di_tv.vval.v_float
364#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000365#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000367
368/*
369 * The v: variables are stored in dictionary "vimvardict".
370 * "vimvars_var" is the variable that is used for the "l:" scope.
371 */
372static dict_T vimvardict;
373static dictitem_T vimvars_var;
374#define vimvarht vimvardict.dv_hashtab
375
Bram Moolenaara40058a2005-07-11 22:42:07 +0000376static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
377static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
378#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
379static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
380#endif
381static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
382static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
383static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000384static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
385static void list_glob_vars __ARGS((int *first));
386static void list_buf_vars __ARGS((int *first));
387static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000388#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000389static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_vim_vars __ARGS((int *first));
392static void list_script_vars __ARGS((int *first));
393static void list_func_vars __ARGS((int *first));
394static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000395static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
396static int check_changedtick __ARGS((char_u *arg));
397static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
398static void clear_lval __ARGS((lval_T *lp));
399static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
400static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
401static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
402static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
403static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
404static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
405static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
406static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
407static void item_lock __ARGS((typval_T *tv, int deep, int lock));
408static int tv_islocked __ARGS((typval_T *tv));
409
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
411static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000416static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
417static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000418
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000419static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000424static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static listitem_T *listitem_alloc __ARGS((void));
426static void listitem_free __ARGS((listitem_T *item));
427static void listitem_remove __ARGS((list_T *l, listitem_T *item));
428static long list_len __ARGS((list_T *l));
429static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
430static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
431static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000433static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000436static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
438static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
439static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000440static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static char_u *list2string __ARGS((typval_T *tv, int copyID));
443static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000444static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000445static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
446static void set_ref_in_list __ARGS((list_T *l, int copyID));
447static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000449static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000450static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
451static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000452static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static long dict_len __ARGS((dict_T *d));
454static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000455static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000457static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
458static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
461static int string2float __ARGS((char_u *text, float_T *value));
462#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
464static int find_internal_func __ARGS((char_u *name));
465static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
466static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
467static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000468static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000469static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#ifdef FEAT_FLOAT
472static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200473static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000475static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000480#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200481static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200483static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#ifdef FEAT_FLOAT
497static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
498#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000499static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000500static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000502static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000504#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000505static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000506static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
508#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#ifdef FEAT_FLOAT
512static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200513static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
518static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200529#ifdef FEAT_FLOAT
530static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
531#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000532static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000534static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000540#ifdef FEAT_FLOAT
541static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200543static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000544#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000545static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000546static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000554static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000556static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000562static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000570static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000571static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000572static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000573static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000576static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000584static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000598static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000604static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000616#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200617static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000618static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
619#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000624static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000625static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000626static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000628static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000632#ifdef vim_mkdir
633static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
634#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100636#ifdef FEAT_MZSCHEME
637static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
638#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000641static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000642#ifdef FEAT_FLOAT
643static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000646static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000647static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000649static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000650static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000662#ifdef FEAT_FLOAT
663static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
664#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000665static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000666static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000668static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000670static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000675static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000676static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000677static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000678static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000680static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000682static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000684#ifdef FEAT_FLOAT
685static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200686static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000687#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000689static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000690static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
691static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000693#ifdef FEAT_FLOAT
694static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
696#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000697static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698#ifdef HAVE_STRFTIME
699static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
700#endif
701static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
703static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000712static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000713static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000714static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000715static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000716static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000717static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000718static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000719static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000720static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200721#ifdef FEAT_FLOAT
722static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
724#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000725static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
726static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
727static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000728#ifdef FEAT_FLOAT
729static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
730#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000731static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
733static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000741static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000743static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000744static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000746static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000747static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static int get_env_len __ARGS((char_u **arg));
749static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000750static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000751static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
752#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
753#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
754 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000755static 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 +0000756static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000757static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000758static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
759static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000760static typval_T *alloc_tv __ARGS((void));
761static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static void init_tv __ARGS((typval_T *varp));
763static long get_tv_number __ARGS((typval_T *varp));
764static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000765static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000766static char_u *get_tv_string __ARGS((typval_T *varp));
767static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000768static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000770static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000771static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
772static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
773static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000774static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
775static 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 +0000776static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
777static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000778static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000779static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000780static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
782static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
783static int eval_fname_script __ARGS((char_u *p));
784static int eval_fname_sid __ARGS((char_u *p));
785static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786static ufunc_T *find_func __ARGS((char_u *name));
787static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000788static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000789#ifdef FEAT_PROFILE
790static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000791static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
792static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
793static int
794# ifdef __BORLANDC__
795 _RTLENTRYF
796# endif
797 prof_total_cmp __ARGS((const void *s1, const void *s2));
798static int
799# ifdef __BORLANDC__
800 _RTLENTRYF
801# endif
802 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000803#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000804static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000805static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000806static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000807static void func_free __ARGS((ufunc_T *fp));
808static void func_unref __ARGS((char_u *name));
809static void func_ref __ARGS((char_u *name));
810static 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 +0000811static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
812static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000813static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000814static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
815static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000816static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000817static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000818static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000819
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000820/* Character used as separated in autoload function/variable names. */
821#define AUTOLOAD_CHAR '#'
822
Bram Moolenaar33570922005-01-25 22:26:29 +0000823/*
824 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000825 */
826 void
827eval_init()
828{
Bram Moolenaar33570922005-01-25 22:26:29 +0000829 int i;
830 struct vimvar *p;
831
832 init_var_dict(&globvardict, &globvars_var);
833 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000834 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000835 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000836
837 for (i = 0; i < VV_LEN; ++i)
838 {
839 p = &vimvars[i];
840 STRCPY(p->vv_di.di_key, p->vv_name);
841 if (p->vv_flags & VV_RO)
842 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
843 else if (p->vv_flags & VV_RO_SBX)
844 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
845 else
846 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000847
848 /* add to v: scope dict, unless the value is not always available */
849 if (p->vv_type != VAR_UNKNOWN)
850 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000851 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000852 /* add to compat scope dict */
853 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000854 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000855 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaara7043832005-01-21 11:56:39 +0000856}
857
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000858#if defined(EXITFREE) || defined(PROTO)
859 void
860eval_clear()
861{
862 int i;
863 struct vimvar *p;
864
865 for (i = 0; i < VV_LEN; ++i)
866 {
867 p = &vimvars[i];
868 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000869 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000870 vim_free(p->vv_str);
871 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000872 }
873 else if (p->vv_di.di_tv.v_type == VAR_LIST)
874 {
875 list_unref(p->vv_list);
876 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000877 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000878 }
879 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000880 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000881 hash_clear(&compat_hashtab);
882
Bram Moolenaard9fba312005-06-26 22:34:35 +0000883 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000884
885 /* global variables */
886 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000887
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000888 /* autoloaded script names */
889 ga_clear_strings(&ga_loaded);
890
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200891 /* script-local variables */
892 for (i = 1; i <= ga_scripts.ga_len; ++i)
893 {
894 vars_clear(&SCRIPT_VARS(i));
895 vim_free(SCRIPT_SV(i));
896 }
897 ga_clear(&ga_scripts);
898
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000899 /* unreferenced lists and dicts */
900 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000901
902 /* functions */
903 free_all_functions();
904 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000905}
906#endif
907
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 * Return the name of the executed function.
910 */
911 char_u *
912func_name(cookie)
913 void *cookie;
914{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000915 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916}
917
918/*
919 * Return the address holding the next breakpoint line for a funccall cookie.
920 */
921 linenr_T *
922func_breakpoint(cookie)
923 void *cookie;
924{
Bram Moolenaar33570922005-01-25 22:26:29 +0000925 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926}
927
928/*
929 * Return the address holding the debug tick for a funccall cookie.
930 */
931 int *
932func_dbg_tick(cookie)
933 void *cookie;
934{
Bram Moolenaar33570922005-01-25 22:26:29 +0000935 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936}
937
938/*
939 * Return the nesting level for a funccall cookie.
940 */
941 int
942func_level(cookie)
943 void *cookie;
944{
Bram Moolenaar33570922005-01-25 22:26:29 +0000945 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946}
947
948/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000949funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000951/* pointer to list of previously used funccal, still around because some
952 * item in it is still being used. */
953funccall_T *previous_funccal = NULL;
954
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955/*
956 * Return TRUE when a function was ended by a ":return" command.
957 */
958 int
959current_func_returned()
960{
961 return current_funccal->returned;
962}
963
964
965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 * Set an internal variable to a string value. Creates the variable if it does
967 * not already exist.
968 */
969 void
970set_internal_string_var(name, value)
971 char_u *name;
972 char_u *value;
973{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000974 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
977 val = vim_strsave(value);
978 if (val != NULL)
979 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000980 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000981 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000983 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000984 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 }
986 }
987}
988
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000989static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000990static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000991static char_u *redir_endp = NULL;
992static char_u *redir_varname = NULL;
993
994/*
995 * Start recording command output to a variable
996 * Returns OK if successfully completed the setup. FAIL otherwise.
997 */
998 int
999var_redir_start(name, append)
1000 char_u *name;
1001 int append; /* append to an existing variable */
1002{
1003 int save_emsg;
1004 int err;
1005 typval_T tv;
1006
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001007 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001008 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001009 {
1010 EMSG(_(e_invarg));
1011 return FAIL;
1012 }
1013
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001014 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001015 redir_varname = vim_strsave(name);
1016 if (redir_varname == NULL)
1017 return FAIL;
1018
1019 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1020 if (redir_lval == NULL)
1021 {
1022 var_redir_stop();
1023 return FAIL;
1024 }
1025
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001026 /* The output is stored in growarray "redir_ga" until redirection ends. */
1027 ga_init2(&redir_ga, (int)sizeof(char), 500);
1028
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001030 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1031 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001032 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1033 {
1034 if (redir_endp != NULL && *redir_endp != NUL)
1035 /* Trailing characters are present after the variable name */
1036 EMSG(_(e_trailing));
1037 else
1038 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001039 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040 var_redir_stop();
1041 return FAIL;
1042 }
1043
1044 /* check if we can write to the variable: set it to or append an empty
1045 * string */
1046 save_emsg = did_emsg;
1047 did_emsg = FALSE;
1048 tv.v_type = VAR_STRING;
1049 tv.vval.v_string = (char_u *)"";
1050 if (append)
1051 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1052 else
1053 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1054 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001055 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 if (err)
1057 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001058 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 var_redir_stop();
1060 return FAIL;
1061 }
1062 if (redir_lval->ll_newkey != NULL)
1063 {
1064 /* Dictionary item was created, don't do it again. */
1065 vim_free(redir_lval->ll_newkey);
1066 redir_lval->ll_newkey = NULL;
1067 }
1068
1069 return OK;
1070}
1071
1072/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001073 * Append "value[value_len]" to the variable set by var_redir_start().
1074 * The actual appending is postponed until redirection ends, because the value
1075 * appended may in fact be the string we write to, changing it may cause freed
1076 * memory to be used:
1077 * :redir => foo
1078 * :let foo
1079 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080 */
1081 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001082var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001084 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001086 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087
1088 if (redir_lval == NULL)
1089 return;
1090
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001091 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001092 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001094 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001096 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001097 {
1098 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001099 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001100 }
1101 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103}
1104
1105/*
1106 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001107 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 */
1109 void
1110var_redir_stop()
1111{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112 typval_T tv;
1113
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 if (redir_lval != NULL)
1115 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001116 /* If there was no error: assign the text to the variable. */
1117 if (redir_endp != NULL)
1118 {
1119 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1120 tv.v_type = VAR_STRING;
1121 tv.vval.v_string = redir_ga.ga_data;
1122 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1123 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001125 /* free the collected output */
1126 vim_free(redir_ga.ga_data);
1127 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001128
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 clear_lval(redir_lval);
1130 vim_free(redir_lval);
1131 redir_lval = NULL;
1132 }
1133 vim_free(redir_varname);
1134 redir_varname = NULL;
1135}
1136
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137# if defined(FEAT_MBYTE) || defined(PROTO)
1138 int
1139eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1140 char_u *enc_from;
1141 char_u *enc_to;
1142 char_u *fname_from;
1143 char_u *fname_to;
1144{
1145 int err = FALSE;
1146
1147 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1148 set_vim_var_string(VV_CC_TO, enc_to, -1);
1149 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1150 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1151 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1152 err = TRUE;
1153 set_vim_var_string(VV_CC_FROM, NULL, -1);
1154 set_vim_var_string(VV_CC_TO, NULL, -1);
1155 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1156 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1157
1158 if (err)
1159 return FAIL;
1160 return OK;
1161}
1162# endif
1163
1164# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1165 int
1166eval_printexpr(fname, args)
1167 char_u *fname;
1168 char_u *args;
1169{
1170 int err = FALSE;
1171
1172 set_vim_var_string(VV_FNAME_IN, fname, -1);
1173 set_vim_var_string(VV_CMDARG, args, -1);
1174 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1175 err = TRUE;
1176 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1177 set_vim_var_string(VV_CMDARG, NULL, -1);
1178
1179 if (err)
1180 {
1181 mch_remove(fname);
1182 return FAIL;
1183 }
1184 return OK;
1185}
1186# endif
1187
1188# if defined(FEAT_DIFF) || defined(PROTO)
1189 void
1190eval_diff(origfile, newfile, outfile)
1191 char_u *origfile;
1192 char_u *newfile;
1193 char_u *outfile;
1194{
1195 int err = FALSE;
1196
1197 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1198 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1199 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1200 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1201 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1202 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1203 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1204}
1205
1206 void
1207eval_patch(origfile, difffile, outfile)
1208 char_u *origfile;
1209 char_u *difffile;
1210 char_u *outfile;
1211{
1212 int err;
1213
1214 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1215 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1216 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1217 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1218 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1219 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1220 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1221}
1222# endif
1223
1224/*
1225 * Top level evaluation function, returning a boolean.
1226 * Sets "error" to TRUE if there was an error.
1227 * Return TRUE or FALSE.
1228 */
1229 int
1230eval_to_bool(arg, error, nextcmd, skip)
1231 char_u *arg;
1232 int *error;
1233 char_u **nextcmd;
1234 int skip; /* only parse, don't execute */
1235{
Bram Moolenaar33570922005-01-25 22:26:29 +00001236 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 int retval = FALSE;
1238
1239 if (skip)
1240 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001241 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 else
1244 {
1245 *error = FALSE;
1246 if (!skip)
1247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001248 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001249 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 }
1251 }
1252 if (skip)
1253 --emsg_skip;
1254
1255 return retval;
1256}
1257
1258/*
1259 * Top level evaluation function, returning a string. If "skip" is TRUE,
1260 * only parsing to "nextcmd" is done, without reporting errors. Return
1261 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1262 */
1263 char_u *
1264eval_to_string_skip(arg, nextcmd, skip)
1265 char_u *arg;
1266 char_u **nextcmd;
1267 int skip; /* only parse, don't execute */
1268{
Bram Moolenaar33570922005-01-25 22:26:29 +00001269 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 char_u *retval;
1271
1272 if (skip)
1273 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001274 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 retval = NULL;
1276 else
1277 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001278 retval = vim_strsave(get_tv_string(&tv));
1279 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 }
1281 if (skip)
1282 --emsg_skip;
1283
1284 return retval;
1285}
1286
1287/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001288 * Skip over an expression at "*pp".
1289 * Return FAIL for an error, OK otherwise.
1290 */
1291 int
1292skip_expr(pp)
1293 char_u **pp;
1294{
Bram Moolenaar33570922005-01-25 22:26:29 +00001295 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001296
1297 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001298 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001299}
1300
1301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001303 * When "convert" is TRUE convert a List into a sequence of lines and convert
1304 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 * Return pointer to allocated memory, or NULL for failure.
1306 */
1307 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001308eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 char_u *arg;
1310 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001311 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312{
Bram Moolenaar33570922005-01-25 22:26:29 +00001313 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001315 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001316#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001317 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001320 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 retval = NULL;
1322 else
1323 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001324 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001325 {
1326 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001327 if (tv.vval.v_list != NULL)
1328 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001329 ga_append(&ga, NUL);
1330 retval = (char_u *)ga.ga_data;
1331 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001332#ifdef FEAT_FLOAT
1333 else if (convert && tv.v_type == VAR_FLOAT)
1334 {
1335 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1336 retval = vim_strsave(numbuf);
1337 }
1338#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001339 else
1340 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001341 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 }
1343
1344 return retval;
1345}
1346
1347/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001348 * Call eval_to_string() without using current local variables and using
1349 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 */
1351 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001352eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 char_u *arg;
1354 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001355 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356{
1357 char_u *retval;
1358 void *save_funccalp;
1359
1360 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001361 if (use_sandbox)
1362 ++sandbox;
1363 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001364 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001365 if (use_sandbox)
1366 --sandbox;
1367 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 restore_funccal(save_funccalp);
1369 return retval;
1370}
1371
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372/*
1373 * Top level evaluation function, returning a number.
1374 * Evaluates "expr" silently.
1375 * Returns -1 for an error.
1376 */
1377 int
1378eval_to_number(expr)
1379 char_u *expr;
1380{
Bram Moolenaar33570922005-01-25 22:26:29 +00001381 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001383 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384
1385 ++emsg_off;
1386
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001387 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 retval = -1;
1389 else
1390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001391 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001392 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
1394 --emsg_off;
1395
1396 return retval;
1397}
1398
Bram Moolenaara40058a2005-07-11 22:42:07 +00001399/*
1400 * Prepare v: variable "idx" to be used.
1401 * Save the current typeval in "save_tv".
1402 * When not used yet add the variable to the v: hashtable.
1403 */
1404 static void
1405prepare_vimvar(idx, save_tv)
1406 int idx;
1407 typval_T *save_tv;
1408{
1409 *save_tv = vimvars[idx].vv_tv;
1410 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1411 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1412}
1413
1414/*
1415 * Restore v: variable "idx" to typeval "save_tv".
1416 * When no longer defined, remove the variable from the v: hashtable.
1417 */
1418 static void
1419restore_vimvar(idx, save_tv)
1420 int idx;
1421 typval_T *save_tv;
1422{
1423 hashitem_T *hi;
1424
Bram Moolenaara40058a2005-07-11 22:42:07 +00001425 vimvars[idx].vv_tv = *save_tv;
1426 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1427 {
1428 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1429 if (HASHITEM_EMPTY(hi))
1430 EMSG2(_(e_intern2), "restore_vimvar()");
1431 else
1432 hash_remove(&vimvarht, hi);
1433 }
1434}
1435
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001436#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001437/*
1438 * Evaluate an expression to a list with suggestions.
1439 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001440 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001441 */
1442 list_T *
1443eval_spell_expr(badword, expr)
1444 char_u *badword;
1445 char_u *expr;
1446{
1447 typval_T save_val;
1448 typval_T rettv;
1449 list_T *list = NULL;
1450 char_u *p = skipwhite(expr);
1451
1452 /* Set "v:val" to the bad word. */
1453 prepare_vimvar(VV_VAL, &save_val);
1454 vimvars[VV_VAL].vv_type = VAR_STRING;
1455 vimvars[VV_VAL].vv_str = badword;
1456 if (p_verbose == 0)
1457 ++emsg_off;
1458
1459 if (eval1(&p, &rettv, TRUE) == OK)
1460 {
1461 if (rettv.v_type != VAR_LIST)
1462 clear_tv(&rettv);
1463 else
1464 list = rettv.vval.v_list;
1465 }
1466
1467 if (p_verbose == 0)
1468 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001469 restore_vimvar(VV_VAL, &save_val);
1470
1471 return list;
1472}
1473
1474/*
1475 * "list" is supposed to contain two items: a word and a number. Return the
1476 * word in "pp" and the number as the return value.
1477 * Return -1 if anything isn't right.
1478 * Used to get the good word and score from the eval_spell_expr() result.
1479 */
1480 int
1481get_spellword(list, pp)
1482 list_T *list;
1483 char_u **pp;
1484{
1485 listitem_T *li;
1486
1487 li = list->lv_first;
1488 if (li == NULL)
1489 return -1;
1490 *pp = get_tv_string(&li->li_tv);
1491
1492 li = li->li_next;
1493 if (li == NULL)
1494 return -1;
1495 return get_tv_number(&li->li_tv);
1496}
1497#endif
1498
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001499/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001500 * Top level evaluation function.
1501 * Returns an allocated typval_T with the result.
1502 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001503 */
1504 typval_T *
1505eval_expr(arg, nextcmd)
1506 char_u *arg;
1507 char_u **nextcmd;
1508{
1509 typval_T *tv;
1510
1511 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001512 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001513 {
1514 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001515 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001516 }
1517
1518 return tv;
1519}
1520
1521
Bram Moolenaar4f688582007-07-24 12:34:30 +00001522#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1523 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001525 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001526 * Uses argv[argc] for the function arguments. Only Number and String
1527 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001528 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001530 static int
1531call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 char_u *func;
1533 int argc;
1534 char_u **argv;
1535 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
Bram Moolenaar33570922005-01-25 22:26:29 +00001538 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 long n;
1540 int len;
1541 int i;
1542 int doesrange;
1543 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001544 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001546 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001548 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549
1550 for (i = 0; i < argc; i++)
1551 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001552 /* Pass a NULL or empty argument as an empty string */
1553 if (argv[i] == NULL || *argv[i] == NUL)
1554 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001555 argvars[i].v_type = VAR_STRING;
1556 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001557 continue;
1558 }
1559
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 /* Recognize a number argument, the others must be strings. */
1561 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1562 if (len != 0 && len == (int)STRLEN(argv[i]))
1563 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001564 argvars[i].v_type = VAR_NUMBER;
1565 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 }
1567 else
1568 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001569 argvars[i].v_type = VAR_STRING;
1570 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 }
1572 }
1573
1574 if (safe)
1575 {
1576 save_funccalp = save_funccal();
1577 ++sandbox;
1578 }
1579
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1581 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001583 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 if (safe)
1585 {
1586 --sandbox;
1587 restore_funccal(save_funccalp);
1588 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001589 vim_free(argvars);
1590
1591 if (ret == FAIL)
1592 clear_tv(rettv);
1593
1594 return ret;
1595}
1596
Bram Moolenaar4f688582007-07-24 12:34:30 +00001597# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001598/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001599 * Call vimL function "func" and return the result as a string.
1600 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001601 * Uses argv[argc] for the function arguments.
1602 */
1603 void *
1604call_func_retstr(func, argc, argv, safe)
1605 char_u *func;
1606 int argc;
1607 char_u **argv;
1608 int safe; /* use the sandbox */
1609{
1610 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001611 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001612
1613 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1614 return NULL;
1615
1616 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 return retval;
1619}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001620# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621
Bram Moolenaar4f688582007-07-24 12:34:30 +00001622# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001624 * Call vimL function "func" and return the result as a number.
1625 * Returns -1 when calling the function fails.
1626 * Uses argv[argc] for the function arguments.
1627 */
1628 long
1629call_func_retnr(func, argc, argv, safe)
1630 char_u *func;
1631 int argc;
1632 char_u **argv;
1633 int safe; /* use the sandbox */
1634{
1635 typval_T rettv;
1636 long retval;
1637
1638 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1639 return -1;
1640
1641 retval = get_tv_number_chk(&rettv, NULL);
1642 clear_tv(&rettv);
1643 return retval;
1644}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001645# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001646
1647/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001648 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001650 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001651 */
1652 void *
1653call_func_retlist(func, argc, argv, safe)
1654 char_u *func;
1655 int argc;
1656 char_u **argv;
1657 int safe; /* use the sandbox */
1658{
1659 typval_T rettv;
1660
1661 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1662 return NULL;
1663
1664 if (rettv.v_type != VAR_LIST)
1665 {
1666 clear_tv(&rettv);
1667 return NULL;
1668 }
1669
1670 return rettv.vval.v_list;
1671}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672#endif
1673
Bram Moolenaar4f688582007-07-24 12:34:30 +00001674
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675/*
1676 * Save the current function call pointer, and set it to NULL.
1677 * Used when executing autocommands and for ":source".
1678 */
1679 void *
1680save_funccal()
1681{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001682 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 current_funccal = NULL;
1685 return (void *)fc;
1686}
1687
1688 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001689restore_funccal(vfc)
1690 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001692 funccall_T *fc = (funccall_T *)vfc;
1693
1694 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695}
1696
Bram Moolenaar05159a02005-02-26 23:04:13 +00001697#if defined(FEAT_PROFILE) || defined(PROTO)
1698/*
1699 * Prepare profiling for entering a child or something else that is not
1700 * counted for the script/function itself.
1701 * Should always be called in pair with prof_child_exit().
1702 */
1703 void
1704prof_child_enter(tm)
1705 proftime_T *tm; /* place to store waittime */
1706{
1707 funccall_T *fc = current_funccal;
1708
1709 if (fc != NULL && fc->func->uf_profiling)
1710 profile_start(&fc->prof_child);
1711 script_prof_save(tm);
1712}
1713
1714/*
1715 * Take care of time spent in a child.
1716 * Should always be called after prof_child_enter().
1717 */
1718 void
1719prof_child_exit(tm)
1720 proftime_T *tm; /* where waittime was stored */
1721{
1722 funccall_T *fc = current_funccal;
1723
1724 if (fc != NULL && fc->func->uf_profiling)
1725 {
1726 profile_end(&fc->prof_child);
1727 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1728 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1729 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1730 }
1731 script_prof_restore(tm);
1732}
1733#endif
1734
1735
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736#ifdef FEAT_FOLDING
1737/*
1738 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1739 * it in "*cp". Doesn't give error messages.
1740 */
1741 int
1742eval_foldexpr(arg, cp)
1743 char_u *arg;
1744 int *cp;
1745{
Bram Moolenaar33570922005-01-25 22:26:29 +00001746 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 int retval;
1748 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001749 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1750 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
1752 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001753 if (use_sandbox)
1754 ++sandbox;
1755 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001757 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 retval = 0;
1759 else
1760 {
1761 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001762 if (tv.v_type == VAR_NUMBER)
1763 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001764 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 retval = 0;
1766 else
1767 {
1768 /* If the result is a string, check if there is a non-digit before
1769 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001770 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 if (!VIM_ISDIGIT(*s) && *s != '-')
1772 *cp = *s++;
1773 retval = atol((char *)s);
1774 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001775 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 }
1777 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001778 if (use_sandbox)
1779 --sandbox;
1780 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782 return retval;
1783}
1784#endif
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 * ":let" list all variable values
1788 * ":let var1 var2" list variable values
1789 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001790 * ":let var += expr" assignment command.
1791 * ":let var -= expr" assignment command.
1792 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 */
1795 void
1796ex_let(eap)
1797 exarg_T *eap;
1798{
1799 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001801 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 int var_count = 0;
1804 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001805 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001806 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001807 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
Bram Moolenaardb552d602006-03-23 22:59:57 +00001809 argend = skip_var_list(arg, &var_count, &semicolon);
1810 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001811 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001812 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1813 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001814 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001815 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001817 /*
1818 * ":let" without "=": list variables
1819 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820 if (*arg == '[')
1821 EMSG(_(e_invarg));
1822 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001823 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001824 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001826 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001828 list_glob_vars(&first);
1829 list_buf_vars(&first);
1830 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001831#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001832 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001833#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001834 list_script_vars(&first);
1835 list_func_vars(&first);
1836 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 eap->nextcmd = check_nextcmd(arg);
1839 }
1840 else
1841 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001842 op[0] = '=';
1843 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001844 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001845 {
1846 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1847 op[0] = expr[-1]; /* +=, -= or .= */
1848 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001849 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (eap->skip)
1852 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001853 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 if (eap->skip)
1855 {
1856 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 --emsg_skip;
1859 }
1860 else if (i != FAIL)
1861 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001862 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001863 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 }
1866 }
1867}
1868
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001869/*
1870 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1871 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001872 * When "nextchars" is not NULL it points to a string with characters that
1873 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1874 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001875 * Returns OK or FAIL;
1876 */
1877 static int
1878ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1879 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001880 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 int copy; /* copy values from "tv", don't move */
1882 int semicolon; /* from skip_var_list() */
1883 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001884 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885{
1886 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001887 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001888 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001889 listitem_T *item;
1890 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001891
1892 if (*arg != '[')
1893 {
1894 /*
1895 * ":let var = expr" or ":for var in list"
1896 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001897 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001898 return FAIL;
1899 return OK;
1900 }
1901
1902 /*
1903 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1904 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001905 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 {
1907 EMSG(_(e_listreq));
1908 return FAIL;
1909 }
1910
1911 i = list_len(l);
1912 if (semicolon == 0 && var_count < i)
1913 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001914 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 return FAIL;
1916 }
1917 if (var_count - semicolon > i)
1918 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001919 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920 return FAIL;
1921 }
1922
1923 item = l->lv_first;
1924 while (*arg != ']')
1925 {
1926 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001927 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 item = item->li_next;
1929 if (arg == NULL)
1930 return FAIL;
1931
1932 arg = skipwhite(arg);
1933 if (*arg == ';')
1934 {
1935 /* Put the rest of the list (may be empty) in the var after ';'.
1936 * Create a new list for this. */
1937 l = list_alloc();
1938 if (l == NULL)
1939 return FAIL;
1940 while (item != NULL)
1941 {
1942 list_append_tv(l, &item->li_tv);
1943 item = item->li_next;
1944 }
1945
1946 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001947 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 ltv.vval.v_list = l;
1949 l->lv_refcount = 1;
1950
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001951 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1952 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953 clear_tv(&ltv);
1954 if (arg == NULL)
1955 return FAIL;
1956 break;
1957 }
1958 else if (*arg != ',' && *arg != ']')
1959 {
1960 EMSG2(_(e_intern2), "ex_let_vars()");
1961 return FAIL;
1962 }
1963 }
1964
1965 return OK;
1966}
1967
1968/*
1969 * Skip over assignable variable "var" or list of variables "[var, var]".
1970 * Used for ":let varvar = expr" and ":for varvar in expr".
1971 * For "[var, var]" increment "*var_count" for each variable.
1972 * for "[var, var; var]" set "semicolon".
1973 * Return NULL for an error.
1974 */
1975 static char_u *
1976skip_var_list(arg, var_count, semicolon)
1977 char_u *arg;
1978 int *var_count;
1979 int *semicolon;
1980{
1981 char_u *p, *s;
1982
1983 if (*arg == '[')
1984 {
1985 /* "[var, var]": find the matching ']'. */
1986 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001987 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 {
1989 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1990 s = skip_var_one(p);
1991 if (s == p)
1992 {
1993 EMSG2(_(e_invarg2), p);
1994 return NULL;
1995 }
1996 ++*var_count;
1997
1998 p = skipwhite(s);
1999 if (*p == ']')
2000 break;
2001 else if (*p == ';')
2002 {
2003 if (*semicolon == 1)
2004 {
2005 EMSG(_("Double ; in list of variables"));
2006 return NULL;
2007 }
2008 *semicolon = 1;
2009 }
2010 else if (*p != ',')
2011 {
2012 EMSG2(_(e_invarg2), p);
2013 return NULL;
2014 }
2015 }
2016 return p + 1;
2017 }
2018 else
2019 return skip_var_one(arg);
2020}
2021
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002022/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002023 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002024 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002025 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026 static char_u *
2027skip_var_one(arg)
2028 char_u *arg;
2029{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002030 if (*arg == '@' && arg[1] != NUL)
2031 return arg + 2;
2032 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2033 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002034}
2035
Bram Moolenaara7043832005-01-21 11:56:39 +00002036/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002037 * List variables for hashtab "ht" with prefix "prefix".
2038 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002039 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002040 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002041list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002042 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002043 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002044 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002045 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002046{
Bram Moolenaar33570922005-01-25 22:26:29 +00002047 hashitem_T *hi;
2048 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002049 int todo;
2050
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002051 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002052 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2053 {
2054 if (!HASHITEM_EMPTY(hi))
2055 {
2056 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002057 di = HI2DI(hi);
2058 if (empty || di->di_tv.v_type != VAR_STRING
2059 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002060 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002061 }
2062 }
2063}
2064
2065/*
2066 * List global variables.
2067 */
2068 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002069list_glob_vars(first)
2070 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002071{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002072 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002073}
2074
2075/*
2076 * List buffer variables.
2077 */
2078 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002079list_buf_vars(first)
2080 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002081{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002082 char_u numbuf[NUMBUFLEN];
2083
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002084 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2085 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002086
2087 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002088 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2089 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002090}
2091
2092/*
2093 * List window variables.
2094 */
2095 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002096list_win_vars(first)
2097 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002098{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002099 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2100 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002101}
2102
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002103#ifdef FEAT_WINDOWS
2104/*
2105 * List tab page variables.
2106 */
2107 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002108list_tab_vars(first)
2109 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002110{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002111 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2112 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002113}
2114#endif
2115
Bram Moolenaara7043832005-01-21 11:56:39 +00002116/*
2117 * List Vim variables.
2118 */
2119 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120list_vim_vars(first)
2121 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002122{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002124}
2125
2126/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002127 * List script-local variables, if there is a script.
2128 */
2129 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002130list_script_vars(first)
2131 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002132{
2133 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2135 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002136}
2137
2138/*
2139 * List function variables, if there is a function.
2140 */
2141 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002142list_func_vars(first)
2143 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002144{
2145 if (current_funccal != NULL)
2146 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002148}
2149
2150/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002151 * List variables in "arg".
2152 */
2153 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155 exarg_T *eap;
2156 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002158{
2159 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002160 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002162 char_u *name_start;
2163 char_u *arg_subsc;
2164 char_u *tofree;
2165 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002166
2167 while (!ends_excmd(*arg) && !got_int)
2168 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002169 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002170 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002171 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002172 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2173 {
2174 emsg_severe = TRUE;
2175 EMSG(_(e_trailing));
2176 break;
2177 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002178 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002179 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002180 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002181 /* get_name_len() takes care of expanding curly braces */
2182 name_start = name = arg;
2183 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2184 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002186 /* This is mainly to keep test 49 working: when expanding
2187 * curly braces fails overrule the exception error message. */
2188 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002190 emsg_severe = TRUE;
2191 EMSG2(_(e_invarg2), arg);
2192 break;
2193 }
2194 error = TRUE;
2195 }
2196 else
2197 {
2198 if (tofree != NULL)
2199 name = tofree;
2200 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 else
2203 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 /* handle d.key, l[idx], f(expr) */
2205 arg_subsc = arg;
2206 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002207 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002209 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002211 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002213 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002214 case 'g': list_glob_vars(first); break;
2215 case 'b': list_buf_vars(first); break;
2216 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002217#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002219#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 case 'v': list_vim_vars(first); break;
2221 case 's': list_script_vars(first); break;
2222 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 default:
2224 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002225 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002226 }
2227 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 {
2229 char_u numbuf[NUMBUFLEN];
2230 char_u *tf;
2231 int c;
2232 char_u *s;
2233
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002234 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 c = *arg;
2236 *arg = NUL;
2237 list_one_var_a((char_u *)"",
2238 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002239 tv.v_type,
2240 s == NULL ? (char_u *)"" : s,
2241 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 *arg = c;
2243 vim_free(tf);
2244 }
2245 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002246 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 }
2248 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249
2250 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252
2253 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 }
2255
2256 return arg;
2257}
2258
2259/*
2260 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2261 * Returns a pointer to the char just after the var name.
2262 * Returns NULL if there is an error.
2263 */
2264 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002265ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002267 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002269 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002270 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271{
2272 int c1;
2273 char_u *name;
2274 char_u *p;
2275 char_u *arg_end = NULL;
2276 int len;
2277 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002278 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279
2280 /*
2281 * ":let $VAR = expr": Set environment variable.
2282 */
2283 if (*arg == '$')
2284 {
2285 /* Find the end of the name. */
2286 ++arg;
2287 name = arg;
2288 len = get_env_len(&arg);
2289 if (len == 0)
2290 EMSG2(_(e_invarg2), name - 1);
2291 else
2292 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002293 if (op != NULL && (*op == '+' || *op == '-'))
2294 EMSG2(_(e_letwrong), op);
2295 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002296 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 EMSG(_(e_letunexp));
2298 else
2299 {
2300 c1 = name[len];
2301 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002302 p = get_tv_string_chk(tv);
2303 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002304 {
2305 int mustfree = FALSE;
2306 char_u *s = vim_getenv(name, &mustfree);
2307
2308 if (s != NULL)
2309 {
2310 p = tofree = concat_str(s, p);
2311 if (mustfree)
2312 vim_free(s);
2313 }
2314 }
2315 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002316 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002317 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002318 if (STRICMP(name, "HOME") == 0)
2319 init_homedir();
2320 else if (didset_vim && STRICMP(name, "VIM") == 0)
2321 didset_vim = FALSE;
2322 else if (didset_vimruntime
2323 && STRICMP(name, "VIMRUNTIME") == 0)
2324 didset_vimruntime = FALSE;
2325 arg_end = arg;
2326 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 }
2330 }
2331 }
2332
2333 /*
2334 * ":let &option = expr": Set option value.
2335 * ":let &l:option = expr": Set local option value.
2336 * ":let &g:option = expr": Set global option value.
2337 */
2338 else if (*arg == '&')
2339 {
2340 /* Find the end of the name. */
2341 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002342 if (p == NULL || (endchars != NULL
2343 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 EMSG(_(e_letunexp));
2345 else
2346 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002347 long n;
2348 int opt_type;
2349 long numval;
2350 char_u *stringval = NULL;
2351 char_u *s;
2352
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353 c1 = *p;
2354 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002355
2356 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002357 s = get_tv_string_chk(tv); /* != NULL if number or string */
2358 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002359 {
2360 opt_type = get_option_value(arg, &numval,
2361 &stringval, opt_flags);
2362 if ((opt_type == 1 && *op == '.')
2363 || (opt_type == 0 && *op != '.'))
2364 EMSG2(_(e_letwrong), op);
2365 else
2366 {
2367 if (opt_type == 1) /* number */
2368 {
2369 if (*op == '+')
2370 n = numval + n;
2371 else
2372 n = numval - n;
2373 }
2374 else if (opt_type == 0 && stringval != NULL) /* string */
2375 {
2376 s = concat_str(stringval, s);
2377 vim_free(stringval);
2378 stringval = s;
2379 }
2380 }
2381 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002382 if (s != NULL)
2383 {
2384 set_option_value(arg, n, s, opt_flags);
2385 arg_end = p;
2386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
2390 }
2391
2392 /*
2393 * ":let @r = expr": Set register contents.
2394 */
2395 else if (*arg == '@')
2396 {
2397 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002398 if (op != NULL && (*op == '+' || *op == '-'))
2399 EMSG2(_(e_letwrong), op);
2400 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002401 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 EMSG(_(e_letunexp));
2403 else
2404 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002405 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002406 char_u *s;
2407
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002408 p = get_tv_string_chk(tv);
2409 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002410 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002411 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 if (s != NULL)
2413 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002414 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 vim_free(s);
2416 }
2417 }
2418 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002419 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002421 arg_end = arg + 1;
2422 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002423 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 }
2425 }
2426
2427 /*
2428 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002431 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002433 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002435 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2439 EMSG(_(e_letunexp));
2440 else
2441 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 arg_end = p;
2444 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002445 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 }
2448
2449 else
2450 EMSG2(_(e_invarg2), arg);
2451
2452 return arg_end;
2453}
2454
2455/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2457 */
2458 static int
2459check_changedtick(arg)
2460 char_u *arg;
2461{
2462 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2463 {
2464 EMSG2(_(e_readonlyvar), arg);
2465 return TRUE;
2466 }
2467 return FALSE;
2468}
2469
2470/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 * Get an lval: variable, Dict item or List item that can be assigned a value
2472 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2473 * "name.key", "name.key[expr]" etc.
2474 * Indexing only works if "name" is an existing List or Dictionary.
2475 * "name" points to the start of the name.
2476 * If "rettv" is not NULL it points to the value to be assigned.
2477 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2478 * wrong; must end in space or cmd separator.
2479 *
2480 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002481 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 */
2484 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002485get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002486 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002487 typval_T *rettv;
2488 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 int unlet;
2490 int skip;
2491 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002492 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002493{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 char_u *expr_start, *expr_end;
2496 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002497 dictitem_T *v;
2498 typval_T var1;
2499 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002501 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002503 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002504 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002507 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508
2509 if (skip)
2510 {
2511 /* When skipping just find the end of the name. */
2512 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002513 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 }
2515
2516 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002517 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (expr_start != NULL)
2519 {
2520 /* Don't expand the name when we already know there is an error. */
2521 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2522 && *p != '[' && *p != '.')
2523 {
2524 EMSG(_(e_trailing));
2525 return NULL;
2526 }
2527
2528 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2529 if (lp->ll_exp_name == NULL)
2530 {
2531 /* Report an invalid expression in braces, unless the
2532 * expression evaluation has been cancelled due to an
2533 * aborting error, an interrupt, or an exception. */
2534 if (!aborting() && !quiet)
2535 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002536 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 EMSG2(_(e_invarg2), name);
2538 return NULL;
2539 }
2540 }
2541 lp->ll_name = lp->ll_exp_name;
2542 }
2543 else
2544 lp->ll_name = name;
2545
2546 /* Without [idx] or .key we are done. */
2547 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2548 return p;
2549
2550 cc = *p;
2551 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002552 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 if (v == NULL && !quiet)
2554 EMSG2(_(e_undefvar), lp->ll_name);
2555 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556 if (v == NULL)
2557 return NULL;
2558
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 /*
2560 * Loop until no more [idx] or .key is following.
2561 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002562 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002564 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2566 && !(lp->ll_tv->v_type == VAR_DICT
2567 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002568 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 if (!quiet)
2570 EMSG(_("E689: Can only index a List or Dictionary"));
2571 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 if (!quiet)
2576 EMSG(_("E708: [:] must come last"));
2577 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002578 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002579
Bram Moolenaar8c711452005-01-14 21:53:12 +00002580 len = -1;
2581 if (*p == '.')
2582 {
2583 key = p + 1;
2584 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2585 ;
2586 if (len == 0)
2587 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 if (!quiet)
2589 EMSG(_(e_emptykey));
2590 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 }
2592 p = key + len;
2593 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002594 else
2595 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002596 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002597 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002598 if (*p == ':')
2599 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002600 else
2601 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 empty1 = FALSE;
2603 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002605 if (get_tv_string_chk(&var1) == NULL)
2606 {
2607 /* not a number or string */
2608 clear_tv(&var1);
2609 return NULL;
2610 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 }
2612
2613 /* Optionally get the second index [ :expr]. */
2614 if (*p == ':')
2615 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002619 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002620 if (!empty1)
2621 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002623 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (rettv != NULL && (rettv->v_type != VAR_LIST
2625 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 if (!quiet)
2628 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 if (!empty1)
2630 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 }
2633 p = skipwhite(p + 1);
2634 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002636 else
2637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2640 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 if (!empty1)
2642 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002645 if (get_tv_string_chk(&var2) == NULL)
2646 {
2647 /* not a number or string */
2648 if (!empty1)
2649 clear_tv(&var1);
2650 clear_tv(&var2);
2651 return NULL;
2652 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658
Bram Moolenaar8c711452005-01-14 21:53:12 +00002659 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (!quiet)
2662 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 if (!empty1)
2664 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 }
2669
2670 /* Skip to past ']'. */
2671 ++p;
2672 }
2673
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 {
2676 if (len == -1)
2677 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002679 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 if (*key == NUL)
2681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!quiet)
2683 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 }
2687 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002688 lp->ll_list = NULL;
2689 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002690 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002693 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002697 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 if (len == -1)
2699 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 }
2702 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (len == -1)
2707 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 p = NULL;
2710 break;
2711 }
2712 if (len == -1)
2713 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 }
2716 else
2717 {
2718 /*
2719 * Get the number and item for the only or first index of the List.
2720 */
2721 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 else
2724 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002725 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 clear_tv(&var1);
2727 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002728 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 lp->ll_list = lp->ll_tv->vval.v_list;
2730 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2731 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002733 if (lp->ll_n1 < 0)
2734 {
2735 lp->ll_n1 = 0;
2736 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2737 }
2738 }
2739 if (lp->ll_li == NULL)
2740 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 }
2745
2746 /*
2747 * May need to find the item or absolute index for the second
2748 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 * When no index given: "lp->ll_empty2" is TRUE.
2750 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002754 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 }
2763
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2765 if (lp->ll_n1 < 0)
2766 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2767 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002769 }
2770
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002772 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002773 }
2774
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 return p;
2776}
2777
2778/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002779 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 */
2781 static void
2782clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002783 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784{
2785 vim_free(lp->ll_exp_name);
2786 vim_free(lp->ll_newkey);
2787}
2788
2789/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002790 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002792 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 */
2794 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002795set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002796 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002798 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002800 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801{
2802 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002803 listitem_T *ri;
2804 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805
2806 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002807 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002809 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 cc = *endp;
2811 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002812 if (op != NULL && *op != '=')
2813 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002814 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002815
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002816 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002817 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002818 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002819 {
2820 if (tv_op(&tv, rettv, op) == OK)
2821 set_var(lp->ll_name, &tv, FALSE);
2822 clear_tv(&tv);
2823 }
2824 }
2825 else
2826 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002828 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002830 else if (tv_check_lock(lp->ll_newkey == NULL
2831 ? lp->ll_tv->v_lock
2832 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2833 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 else if (lp->ll_range)
2835 {
2836 /*
2837 * Assign the List values to the list items.
2838 */
2839 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002840 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002841 if (op != NULL && *op != '=')
2842 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2843 else
2844 {
2845 clear_tv(&lp->ll_li->li_tv);
2846 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2847 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 ri = ri->li_next;
2849 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2850 break;
2851 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002852 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002854 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002855 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 ri = NULL;
2857 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002858 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002859 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 lp->ll_li = lp->ll_li->li_next;
2861 ++lp->ll_n1;
2862 }
2863 if (ri != NULL)
2864 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002865 else if (lp->ll_empty2
2866 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 : lp->ll_n1 != lp->ll_n2)
2868 EMSG(_("E711: List value has not enough items"));
2869 }
2870 else
2871 {
2872 /*
2873 * Assign to a List or Dictionary item.
2874 */
2875 if (lp->ll_newkey != NULL)
2876 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002877 if (op != NULL && *op != '=')
2878 {
2879 EMSG2(_(e_letwrong), op);
2880 return;
2881 }
2882
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002884 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 if (di == NULL)
2886 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002887 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2888 {
2889 vim_free(di);
2890 return;
2891 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002893 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002894 else if (op != NULL && *op != '=')
2895 {
2896 tv_op(lp->ll_tv, rettv, op);
2897 return;
2898 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002899 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002901
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 /*
2903 * Assign the value to the variable or list item.
2904 */
2905 if (copy)
2906 copy_tv(rettv, lp->ll_tv);
2907 else
2908 {
2909 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002910 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 }
2913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002914}
2915
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002916/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002917 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2918 * Returns OK or FAIL.
2919 */
2920 static int
2921tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002922 typval_T *tv1;
2923 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 char_u *op;
2925{
2926 long n;
2927 char_u numbuf[NUMBUFLEN];
2928 char_u *s;
2929
2930 /* Can't do anything with a Funcref or a Dict on the right. */
2931 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2932 {
2933 switch (tv1->v_type)
2934 {
2935 case VAR_DICT:
2936 case VAR_FUNC:
2937 break;
2938
2939 case VAR_LIST:
2940 if (*op != '+' || tv2->v_type != VAR_LIST)
2941 break;
2942 /* List += List */
2943 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2944 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2945 return OK;
2946
2947 case VAR_NUMBER:
2948 case VAR_STRING:
2949 if (tv2->v_type == VAR_LIST)
2950 break;
2951 if (*op == '+' || *op == '-')
2952 {
2953 /* nr += nr or nr -= nr*/
2954 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002955#ifdef FEAT_FLOAT
2956 if (tv2->v_type == VAR_FLOAT)
2957 {
2958 float_T f = n;
2959
2960 if (*op == '+')
2961 f += tv2->vval.v_float;
2962 else
2963 f -= tv2->vval.v_float;
2964 clear_tv(tv1);
2965 tv1->v_type = VAR_FLOAT;
2966 tv1->vval.v_float = f;
2967 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002968 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002969#endif
2970 {
2971 if (*op == '+')
2972 n += get_tv_number(tv2);
2973 else
2974 n -= get_tv_number(tv2);
2975 clear_tv(tv1);
2976 tv1->v_type = VAR_NUMBER;
2977 tv1->vval.v_number = n;
2978 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002979 }
2980 else
2981 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002982 if (tv2->v_type == VAR_FLOAT)
2983 break;
2984
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002985 /* str .= str */
2986 s = get_tv_string(tv1);
2987 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2988 clear_tv(tv1);
2989 tv1->v_type = VAR_STRING;
2990 tv1->vval.v_string = s;
2991 }
2992 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002993
2994#ifdef FEAT_FLOAT
2995 case VAR_FLOAT:
2996 {
2997 float_T f;
2998
2999 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3000 && tv2->v_type != VAR_NUMBER
3001 && tv2->v_type != VAR_STRING))
3002 break;
3003 if (tv2->v_type == VAR_FLOAT)
3004 f = tv2->vval.v_float;
3005 else
3006 f = get_tv_number(tv2);
3007 if (*op == '+')
3008 tv1->vval.v_float += f;
3009 else
3010 tv1->vval.v_float -= f;
3011 }
3012 return OK;
3013#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003014 }
3015 }
3016
3017 EMSG2(_(e_letwrong), op);
3018 return FAIL;
3019}
3020
3021/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003022 * Add a watcher to a list.
3023 */
3024 static void
3025list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003026 list_T *l;
3027 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003028{
3029 lw->lw_next = l->lv_watch;
3030 l->lv_watch = lw;
3031}
3032
3033/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003034 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003035 * No warning when it isn't found...
3036 */
3037 static void
3038list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003039 list_T *l;
3040 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003041{
Bram Moolenaar33570922005-01-25 22:26:29 +00003042 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003043
3044 lwp = &l->lv_watch;
3045 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3046 {
3047 if (lw == lwrem)
3048 {
3049 *lwp = lw->lw_next;
3050 break;
3051 }
3052 lwp = &lw->lw_next;
3053 }
3054}
3055
3056/*
3057 * Just before removing an item from a list: advance watchers to the next
3058 * item.
3059 */
3060 static void
3061list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003062 list_T *l;
3063 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003064{
Bram Moolenaar33570922005-01-25 22:26:29 +00003065 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003066
3067 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3068 if (lw->lw_item == item)
3069 lw->lw_item = item->li_next;
3070}
3071
3072/*
3073 * Evaluate the expression used in a ":for var in expr" command.
3074 * "arg" points to "var".
3075 * Set "*errp" to TRUE for an error, FALSE otherwise;
3076 * Return a pointer that holds the info. Null when there is an error.
3077 */
3078 void *
3079eval_for_line(arg, errp, nextcmdp, skip)
3080 char_u *arg;
3081 int *errp;
3082 char_u **nextcmdp;
3083 int skip;
3084{
Bram Moolenaar33570922005-01-25 22:26:29 +00003085 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003086 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003087 typval_T tv;
3088 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003089
3090 *errp = TRUE; /* default: there is an error */
3091
Bram Moolenaar33570922005-01-25 22:26:29 +00003092 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003093 if (fi == NULL)
3094 return NULL;
3095
3096 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3097 if (expr == NULL)
3098 return fi;
3099
3100 expr = skipwhite(expr);
3101 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3102 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003103 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003104 return fi;
3105 }
3106
3107 if (skip)
3108 ++emsg_skip;
3109 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3110 {
3111 *errp = FALSE;
3112 if (!skip)
3113 {
3114 l = tv.vval.v_list;
3115 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003116 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003118 clear_tv(&tv);
3119 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003120 else
3121 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003122 /* No need to increment the refcount, it's already set for the
3123 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124 fi->fi_list = l;
3125 list_add_watch(l, &fi->fi_lw);
3126 fi->fi_lw.lw_item = l->lv_first;
3127 }
3128 }
3129 }
3130 if (skip)
3131 --emsg_skip;
3132
3133 return fi;
3134}
3135
3136/*
3137 * Use the first item in a ":for" list. Advance to the next.
3138 * Assign the values to the variable (list). "arg" points to the first one.
3139 * Return TRUE when a valid item was found, FALSE when at end of list or
3140 * something wrong.
3141 */
3142 int
3143next_for_item(fi_void, arg)
3144 void *fi_void;
3145 char_u *arg;
3146{
Bram Moolenaar33570922005-01-25 22:26:29 +00003147 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003149 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150
3151 item = fi->fi_lw.lw_item;
3152 if (item == NULL)
3153 result = FALSE;
3154 else
3155 {
3156 fi->fi_lw.lw_item = item->li_next;
3157 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3158 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3159 }
3160 return result;
3161}
3162
3163/*
3164 * Free the structure used to store info used by ":for".
3165 */
3166 void
3167free_for_info(fi_void)
3168 void *fi_void;
3169{
Bram Moolenaar33570922005-01-25 22:26:29 +00003170 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003172 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003173 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003175 list_unref(fi->fi_list);
3176 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177 vim_free(fi);
3178}
3179
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3181
3182 void
3183set_context_for_expression(xp, arg, cmdidx)
3184 expand_T *xp;
3185 char_u *arg;
3186 cmdidx_T cmdidx;
3187{
3188 int got_eq = FALSE;
3189 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003190 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192 if (cmdidx == CMD_let)
3193 {
3194 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003195 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003196 {
3197 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003198 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199 {
3200 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003201 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202 if (vim_iswhite(*p))
3203 break;
3204 }
3205 return;
3206 }
3207 }
3208 else
3209 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3210 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 while ((xp->xp_pattern = vim_strpbrk(arg,
3212 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3213 {
3214 c = *xp->xp_pattern;
3215 if (c == '&')
3216 {
3217 c = xp->xp_pattern[1];
3218 if (c == '&')
3219 {
3220 ++xp->xp_pattern;
3221 xp->xp_context = cmdidx != CMD_let || got_eq
3222 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3223 }
3224 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003227 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3228 xp->xp_pattern += 2;
3229
3230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 }
3232 else if (c == '$')
3233 {
3234 /* environment variable */
3235 xp->xp_context = EXPAND_ENV_VARS;
3236 }
3237 else if (c == '=')
3238 {
3239 got_eq = TRUE;
3240 xp->xp_context = EXPAND_EXPRESSION;
3241 }
3242 else if (c == '<'
3243 && xp->xp_context == EXPAND_FUNCTIONS
3244 && vim_strchr(xp->xp_pattern, '(') == NULL)
3245 {
3246 /* Function name can start with "<SNR>" */
3247 break;
3248 }
3249 else if (cmdidx != CMD_let || got_eq)
3250 {
3251 if (c == '"') /* string */
3252 {
3253 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3254 if (c == '\\' && xp->xp_pattern[1] != NUL)
3255 ++xp->xp_pattern;
3256 xp->xp_context = EXPAND_NOTHING;
3257 }
3258 else if (c == '\'') /* literal string */
3259 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003260 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3262 /* skip */ ;
3263 xp->xp_context = EXPAND_NOTHING;
3264 }
3265 else if (c == '|')
3266 {
3267 if (xp->xp_pattern[1] == '|')
3268 {
3269 ++xp->xp_pattern;
3270 xp->xp_context = EXPAND_EXPRESSION;
3271 }
3272 else
3273 xp->xp_context = EXPAND_COMMANDS;
3274 }
3275 else
3276 xp->xp_context = EXPAND_EXPRESSION;
3277 }
3278 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 /* Doesn't look like something valid, expand as an expression
3280 * anyway. */
3281 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 arg = xp->xp_pattern;
3283 if (*arg != NUL)
3284 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3285 /* skip */ ;
3286 }
3287 xp->xp_pattern = arg;
3288}
3289
3290#endif /* FEAT_CMDL_COMPL */
3291
3292/*
3293 * ":1,25call func(arg1, arg2)" function call.
3294 */
3295 void
3296ex_call(eap)
3297 exarg_T *eap;
3298{
3299 char_u *arg = eap->arg;
3300 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003302 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003304 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 linenr_T lnum;
3306 int doesrange;
3307 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003308 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003310 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003311 if (fudi.fd_newkey != NULL)
3312 {
3313 /* Still need to give an error message for missing key. */
3314 EMSG2(_(e_dictkey), fudi.fd_newkey);
3315 vim_free(fudi.fd_newkey);
3316 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003317 if (tofree == NULL)
3318 return;
3319
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003320 /* Increase refcount on dictionary, it could get deleted when evaluating
3321 * the arguments. */
3322 if (fudi.fd_dict != NULL)
3323 ++fudi.fd_dict->dv_refcount;
3324
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003325 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003326 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003327 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
Bram Moolenaar532c7802005-01-27 14:44:31 +00003329 /* Skip white space to allow ":call func ()". Not good, but required for
3330 * backward compatibility. */
3331 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003332 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
3334 if (*startarg != '(')
3335 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003336 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 goto end;
3338 }
3339
3340 /*
3341 * When skipping, evaluate the function once, to find the end of the
3342 * arguments.
3343 * When the function takes a range, this is discovered after the first
3344 * call, and the loop is broken.
3345 */
3346 if (eap->skip)
3347 {
3348 ++emsg_skip;
3349 lnum = eap->line2; /* do it once, also with an invalid range */
3350 }
3351 else
3352 lnum = eap->line1;
3353 for ( ; lnum <= eap->line2; ++lnum)
3354 {
3355 if (!eap->skip && eap->addr_count > 0)
3356 {
3357 curwin->w_cursor.lnum = lnum;
3358 curwin->w_cursor.col = 0;
3359 }
3360 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003361 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003362 eap->line1, eap->line2, &doesrange,
3363 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
3365 failed = TRUE;
3366 break;
3367 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003368
3369 /* Handle a function returning a Funcref, Dictionary or List. */
3370 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3371 {
3372 failed = TRUE;
3373 break;
3374 }
3375
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003376 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 if (doesrange || eap->skip)
3378 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003379
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003381 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003382 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003383 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 if (aborting())
3385 break;
3386 }
3387 if (eap->skip)
3388 --emsg_skip;
3389
3390 if (!failed)
3391 {
3392 /* Check for trailing illegal characters and a following command. */
3393 if (!ends_excmd(*arg))
3394 {
3395 emsg_severe = TRUE;
3396 EMSG(_(e_trailing));
3397 }
3398 else
3399 eap->nextcmd = check_nextcmd(arg);
3400 }
3401
3402end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003403 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003404 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405}
3406
3407/*
3408 * ":unlet[!] var1 ... " command.
3409 */
3410 void
3411ex_unlet(eap)
3412 exarg_T *eap;
3413{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003414 ex_unletlock(eap, eap->arg, 0);
3415}
3416
3417/*
3418 * ":lockvar" and ":unlockvar" commands
3419 */
3420 void
3421ex_lockvar(eap)
3422 exarg_T *eap;
3423{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003425 int deep = 2;
3426
3427 if (eap->forceit)
3428 deep = -1;
3429 else if (vim_isdigit(*arg))
3430 {
3431 deep = getdigits(&arg);
3432 arg = skipwhite(arg);
3433 }
3434
3435 ex_unletlock(eap, arg, deep);
3436}
3437
3438/*
3439 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3440 */
3441 static void
3442ex_unletlock(eap, argstart, deep)
3443 exarg_T *eap;
3444 char_u *argstart;
3445 int deep;
3446{
3447 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003450 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
3452 do
3453 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003454 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003455 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3456 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003457 if (lv.ll_name == NULL)
3458 error = TRUE; /* error but continue parsing */
3459 if (name_end == NULL || (!vim_iswhite(*name_end)
3460 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003462 if (name_end != NULL)
3463 {
3464 emsg_severe = TRUE;
3465 EMSG(_(e_trailing));
3466 }
3467 if (!(eap->skip || error))
3468 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 break;
3470 }
3471
3472 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003473 {
3474 if (eap->cmdidx == CMD_unlet)
3475 {
3476 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3477 error = TRUE;
3478 }
3479 else
3480 {
3481 if (do_lock_var(&lv, name_end, deep,
3482 eap->cmdidx == CMD_lockvar) == FAIL)
3483 error = TRUE;
3484 }
3485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003487 if (!eap->skip)
3488 clear_lval(&lv);
3489
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 arg = skipwhite(name_end);
3491 } while (!ends_excmd(*arg));
3492
3493 eap->nextcmd = check_nextcmd(arg);
3494}
3495
Bram Moolenaar8c711452005-01-14 21:53:12 +00003496 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003497do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003498 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003499 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003500 int forceit;
3501{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003502 int ret = OK;
3503 int cc;
3504
3505 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003506 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003507 cc = *name_end;
3508 *name_end = NUL;
3509
3510 /* Normal name or expanded name. */
3511 if (check_changedtick(lp->ll_name))
3512 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003513 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003514 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003515 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003516 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003517 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3518 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003519 else if (lp->ll_range)
3520 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003521 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003522
3523 /* Delete a range of List items. */
3524 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3525 {
3526 li = lp->ll_li->li_next;
3527 listitem_remove(lp->ll_list, lp->ll_li);
3528 lp->ll_li = li;
3529 ++lp->ll_n1;
3530 }
3531 }
3532 else
3533 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003534 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003535 /* unlet a List item. */
3536 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003537 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003538 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003539 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003540 }
3541
3542 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003543}
3544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545/*
3546 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003547 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 */
3549 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003550do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003552 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553{
Bram Moolenaar33570922005-01-25 22:26:29 +00003554 hashtab_T *ht;
3555 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003556 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003557 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
Bram Moolenaar33570922005-01-25 22:26:29 +00003559 ht = find_var_ht(name, &varname);
3560 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003562 hi = hash_find(ht, varname);
3563 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003564 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003565 di = HI2DI(hi);
3566 if (var_check_fixed(di->di_flags, name)
3567 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003568 return FAIL;
3569 delete_var(ht, hi);
3570 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003573 if (forceit)
3574 return OK;
3575 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 return FAIL;
3577}
3578
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003579/*
3580 * Lock or unlock variable indicated by "lp".
3581 * "deep" is the levels to go (-1 for unlimited);
3582 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3583 */
3584 static int
3585do_lock_var(lp, name_end, deep, lock)
3586 lval_T *lp;
3587 char_u *name_end;
3588 int deep;
3589 int lock;
3590{
3591 int ret = OK;
3592 int cc;
3593 dictitem_T *di;
3594
3595 if (deep == 0) /* nothing to do */
3596 return OK;
3597
3598 if (lp->ll_tv == NULL)
3599 {
3600 cc = *name_end;
3601 *name_end = NUL;
3602
3603 /* Normal name or expanded name. */
3604 if (check_changedtick(lp->ll_name))
3605 ret = FAIL;
3606 else
3607 {
3608 di = find_var(lp->ll_name, NULL);
3609 if (di == NULL)
3610 ret = FAIL;
3611 else
3612 {
3613 if (lock)
3614 di->di_flags |= DI_FLAGS_LOCK;
3615 else
3616 di->di_flags &= ~DI_FLAGS_LOCK;
3617 item_lock(&di->di_tv, deep, lock);
3618 }
3619 }
3620 *name_end = cc;
3621 }
3622 else if (lp->ll_range)
3623 {
3624 listitem_T *li = lp->ll_li;
3625
3626 /* (un)lock a range of List items. */
3627 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3628 {
3629 item_lock(&li->li_tv, deep, lock);
3630 li = li->li_next;
3631 ++lp->ll_n1;
3632 }
3633 }
3634 else if (lp->ll_list != NULL)
3635 /* (un)lock a List item. */
3636 item_lock(&lp->ll_li->li_tv, deep, lock);
3637 else
3638 /* un(lock) a Dictionary item. */
3639 item_lock(&lp->ll_di->di_tv, deep, lock);
3640
3641 return ret;
3642}
3643
3644/*
3645 * Lock or unlock an item. "deep" is nr of levels to go.
3646 */
3647 static void
3648item_lock(tv, deep, lock)
3649 typval_T *tv;
3650 int deep;
3651 int lock;
3652{
3653 static int recurse = 0;
3654 list_T *l;
3655 listitem_T *li;
3656 dict_T *d;
3657 hashitem_T *hi;
3658 int todo;
3659
3660 if (recurse >= DICT_MAXNEST)
3661 {
3662 EMSG(_("E743: variable nested too deep for (un)lock"));
3663 return;
3664 }
3665 if (deep == 0)
3666 return;
3667 ++recurse;
3668
3669 /* lock/unlock the item itself */
3670 if (lock)
3671 tv->v_lock |= VAR_LOCKED;
3672 else
3673 tv->v_lock &= ~VAR_LOCKED;
3674
3675 switch (tv->v_type)
3676 {
3677 case VAR_LIST:
3678 if ((l = tv->vval.v_list) != NULL)
3679 {
3680 if (lock)
3681 l->lv_lock |= VAR_LOCKED;
3682 else
3683 l->lv_lock &= ~VAR_LOCKED;
3684 if (deep < 0 || deep > 1)
3685 /* recursive: lock/unlock the items the List contains */
3686 for (li = l->lv_first; li != NULL; li = li->li_next)
3687 item_lock(&li->li_tv, deep - 1, lock);
3688 }
3689 break;
3690 case VAR_DICT:
3691 if ((d = tv->vval.v_dict) != NULL)
3692 {
3693 if (lock)
3694 d->dv_lock |= VAR_LOCKED;
3695 else
3696 d->dv_lock &= ~VAR_LOCKED;
3697 if (deep < 0 || deep > 1)
3698 {
3699 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003700 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003701 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3702 {
3703 if (!HASHITEM_EMPTY(hi))
3704 {
3705 --todo;
3706 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3707 }
3708 }
3709 }
3710 }
3711 }
3712 --recurse;
3713}
3714
Bram Moolenaara40058a2005-07-11 22:42:07 +00003715/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003716 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3717 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003718 */
3719 static int
3720tv_islocked(tv)
3721 typval_T *tv;
3722{
3723 return (tv->v_lock & VAR_LOCKED)
3724 || (tv->v_type == VAR_LIST
3725 && tv->vval.v_list != NULL
3726 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3727 || (tv->v_type == VAR_DICT
3728 && tv->vval.v_dict != NULL
3729 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3730}
3731
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3733/*
3734 * Delete all "menutrans_" variables.
3735 */
3736 void
3737del_menutrans_vars()
3738{
Bram Moolenaar33570922005-01-25 22:26:29 +00003739 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003740 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
Bram Moolenaar33570922005-01-25 22:26:29 +00003742 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003743 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003744 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003745 {
3746 if (!HASHITEM_EMPTY(hi))
3747 {
3748 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003749 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3750 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003751 }
3752 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003753 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754}
3755#endif
3756
3757#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3758
3759/*
3760 * Local string buffer for the next two functions to store a variable name
3761 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3762 * get_user_var_name().
3763 */
3764
3765static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3766
3767static char_u *varnamebuf = NULL;
3768static int varnamebuflen = 0;
3769
3770/*
3771 * Function to concatenate a prefix and a variable name.
3772 */
3773 static char_u *
3774cat_prefix_varname(prefix, name)
3775 int prefix;
3776 char_u *name;
3777{
3778 int len;
3779
3780 len = (int)STRLEN(name) + 3;
3781 if (len > varnamebuflen)
3782 {
3783 vim_free(varnamebuf);
3784 len += 10; /* some additional space */
3785 varnamebuf = alloc(len);
3786 if (varnamebuf == NULL)
3787 {
3788 varnamebuflen = 0;
3789 return NULL;
3790 }
3791 varnamebuflen = len;
3792 }
3793 *varnamebuf = prefix;
3794 varnamebuf[1] = ':';
3795 STRCPY(varnamebuf + 2, name);
3796 return varnamebuf;
3797}
3798
3799/*
3800 * Function given to ExpandGeneric() to obtain the list of user defined
3801 * (global/buffer/window/built-in) variable names.
3802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 char_u *
3804get_user_var_name(xp, idx)
3805 expand_T *xp;
3806 int idx;
3807{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003808 static long_u gdone;
3809 static long_u bdone;
3810 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003811#ifdef FEAT_WINDOWS
3812 static long_u tdone;
3813#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003814 static int vidx;
3815 static hashitem_T *hi;
3816 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817
3818 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003819 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003820 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003821#ifdef FEAT_WINDOWS
3822 tdone = 0;
3823#endif
3824 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003825
3826 /* Global variables */
3827 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003829 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003830 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003831 else
3832 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003833 while (HASHITEM_EMPTY(hi))
3834 ++hi;
3835 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3836 return cat_prefix_varname('g', hi->hi_key);
3837 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003839
3840 /* b: variables */
3841 ht = &curbuf->b_vars.dv_hashtab;
3842 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003844 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003845 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003846 else
3847 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003848 while (HASHITEM_EMPTY(hi))
3849 ++hi;
3850 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003852 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003854 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 return (char_u *)"b:changedtick";
3856 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003857
3858 /* w: variables */
3859 ht = &curwin->w_vars.dv_hashtab;
3860 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003862 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003863 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003864 else
3865 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003866 while (HASHITEM_EMPTY(hi))
3867 ++hi;
3868 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003870
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003871#ifdef FEAT_WINDOWS
3872 /* t: variables */
3873 ht = &curtab->tp_vars.dv_hashtab;
3874 if (tdone < ht->ht_used)
3875 {
3876 if (tdone++ == 0)
3877 hi = ht->ht_array;
3878 else
3879 ++hi;
3880 while (HASHITEM_EMPTY(hi))
3881 ++hi;
3882 return cat_prefix_varname('t', hi->hi_key);
3883 }
3884#endif
3885
Bram Moolenaar33570922005-01-25 22:26:29 +00003886 /* v: variables */
3887 if (vidx < VV_LEN)
3888 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
3890 vim_free(varnamebuf);
3891 varnamebuf = NULL;
3892 varnamebuflen = 0;
3893 return NULL;
3894}
3895
3896#endif /* FEAT_CMDL_COMPL */
3897
3898/*
3899 * types for expressions.
3900 */
3901typedef enum
3902{
3903 TYPE_UNKNOWN = 0
3904 , TYPE_EQUAL /* == */
3905 , TYPE_NEQUAL /* != */
3906 , TYPE_GREATER /* > */
3907 , TYPE_GEQUAL /* >= */
3908 , TYPE_SMALLER /* < */
3909 , TYPE_SEQUAL /* <= */
3910 , TYPE_MATCH /* =~ */
3911 , TYPE_NOMATCH /* !~ */
3912} exptype_T;
3913
3914/*
3915 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003916 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3918 */
3919
3920/*
3921 * Handle zero level expression.
3922 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003923 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003924 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 * Return OK or FAIL.
3926 */
3927 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003928eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 char_u **nextcmd;
3932 int evaluate;
3933{
3934 int ret;
3935 char_u *p;
3936
3937 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003938 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 if (ret == FAIL || !ends_excmd(*p))
3940 {
3941 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003942 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 /*
3944 * Report the invalid expression unless the expression evaluation has
3945 * been cancelled due to an aborting error, an interrupt, or an
3946 * exception.
3947 */
3948 if (!aborting())
3949 EMSG2(_(e_invexpr2), arg);
3950 ret = FAIL;
3951 }
3952 if (nextcmd != NULL)
3953 *nextcmd = check_nextcmd(p);
3954
3955 return ret;
3956}
3957
3958/*
3959 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003960 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 *
3962 * "arg" must point to the first non-white of the expression.
3963 * "arg" is advanced to the next non-white after the recognized expression.
3964 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003965 * Note: "rettv.v_lock" is not set.
3966 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 * Return OK or FAIL.
3968 */
3969 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003970eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 int evaluate;
3974{
3975 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003976 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
3978 /*
3979 * Get the first variable.
3980 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003981 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 return FAIL;
3983
3984 if ((*arg)[0] == '?')
3985 {
3986 result = FALSE;
3987 if (evaluate)
3988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003989 int error = FALSE;
3990
3991 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003993 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003994 if (error)
3995 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
3997
3998 /*
3999 * Get the second variable.
4000 */
4001 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004002 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 return FAIL;
4004
4005 /*
4006 * Check for the ":".
4007 */
4008 if ((*arg)[0] != ':')
4009 {
4010 EMSG(_("E109: Missing ':' after '?'"));
4011 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004012 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 return FAIL;
4014 }
4015
4016 /*
4017 * Get the third variable.
4018 */
4019 *arg = skipwhite(*arg + 1);
4020 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4021 {
4022 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 return FAIL;
4025 }
4026 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
4029
4030 return OK;
4031}
4032
4033/*
4034 * Handle first level expression:
4035 * expr2 || expr2 || expr2 logical OR
4036 *
4037 * "arg" must point to the first non-white of the expression.
4038 * "arg" is advanced to the next non-white after the recognized expression.
4039 *
4040 * Return OK or FAIL.
4041 */
4042 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 int evaluate;
4047{
Bram Moolenaar33570922005-01-25 22:26:29 +00004048 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 long result;
4050 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004051 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
4053 /*
4054 * Get the first variable.
4055 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004056 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 return FAIL;
4058
4059 /*
4060 * Repeat until there is no following "||".
4061 */
4062 first = TRUE;
4063 result = FALSE;
4064 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4065 {
4066 if (evaluate && first)
4067 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004068 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004070 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004071 if (error)
4072 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 first = FALSE;
4074 }
4075
4076 /*
4077 * Get the second variable.
4078 */
4079 *arg = skipwhite(*arg + 2);
4080 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4081 return FAIL;
4082
4083 /*
4084 * Compute the result.
4085 */
4086 if (evaluate && !result)
4087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004088 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004091 if (error)
4092 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 }
4094 if (evaluate)
4095 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 rettv->v_type = VAR_NUMBER;
4097 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 }
4099 }
4100
4101 return OK;
4102}
4103
4104/*
4105 * Handle second level expression:
4106 * expr3 && expr3 && expr3 logical AND
4107 *
4108 * "arg" must point to the first non-white of the expression.
4109 * "arg" is advanced to the next non-white after the recognized expression.
4110 *
4111 * Return OK or FAIL.
4112 */
4113 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004114eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004116 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 int evaluate;
4118{
Bram Moolenaar33570922005-01-25 22:26:29 +00004119 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 long result;
4121 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004122 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123
4124 /*
4125 * Get the first variable.
4126 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 return FAIL;
4129
4130 /*
4131 * Repeat until there is no following "&&".
4132 */
4133 first = TRUE;
4134 result = TRUE;
4135 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4136 {
4137 if (evaluate && first)
4138 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004139 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004142 if (error)
4143 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 first = FALSE;
4145 }
4146
4147 /*
4148 * Get the second variable.
4149 */
4150 *arg = skipwhite(*arg + 2);
4151 if (eval4(arg, &var2, evaluate && result) == FAIL)
4152 return FAIL;
4153
4154 /*
4155 * Compute the result.
4156 */
4157 if (evaluate && result)
4158 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004159 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004161 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 if (error)
4163 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 }
4165 if (evaluate)
4166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 rettv->v_type = VAR_NUMBER;
4168 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 }
4170 }
4171
4172 return OK;
4173}
4174
4175/*
4176 * Handle third level expression:
4177 * var1 == var2
4178 * var1 =~ var2
4179 * var1 != var2
4180 * var1 !~ var2
4181 * var1 > var2
4182 * var1 >= var2
4183 * var1 < var2
4184 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004185 * var1 is var2
4186 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 *
4188 * "arg" must point to the first non-white of the expression.
4189 * "arg" is advanced to the next non-white after the recognized expression.
4190 *
4191 * Return OK or FAIL.
4192 */
4193 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 int evaluate;
4198{
Bram Moolenaar33570922005-01-25 22:26:29 +00004199 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 char_u *p;
4201 int i;
4202 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004203 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 int len = 2;
4205 long n1, n2;
4206 char_u *s1, *s2;
4207 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4208 regmatch_T regmatch;
4209 int ic;
4210 char_u *save_cpo;
4211
4212 /*
4213 * Get the first variable.
4214 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 return FAIL;
4217
4218 p = *arg;
4219 switch (p[0])
4220 {
4221 case '=': if (p[1] == '=')
4222 type = TYPE_EQUAL;
4223 else if (p[1] == '~')
4224 type = TYPE_MATCH;
4225 break;
4226 case '!': if (p[1] == '=')
4227 type = TYPE_NEQUAL;
4228 else if (p[1] == '~')
4229 type = TYPE_NOMATCH;
4230 break;
4231 case '>': if (p[1] != '=')
4232 {
4233 type = TYPE_GREATER;
4234 len = 1;
4235 }
4236 else
4237 type = TYPE_GEQUAL;
4238 break;
4239 case '<': if (p[1] != '=')
4240 {
4241 type = TYPE_SMALLER;
4242 len = 1;
4243 }
4244 else
4245 type = TYPE_SEQUAL;
4246 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004247 case 'i': if (p[1] == 's')
4248 {
4249 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4250 len = 5;
4251 if (!vim_isIDc(p[len]))
4252 {
4253 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4254 type_is = TRUE;
4255 }
4256 }
4257 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
4259
4260 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004261 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 */
4263 if (type != TYPE_UNKNOWN)
4264 {
4265 /* extra question mark appended: ignore case */
4266 if (p[len] == '?')
4267 {
4268 ic = TRUE;
4269 ++len;
4270 }
4271 /* extra '#' appended: match case */
4272 else if (p[len] == '#')
4273 {
4274 ic = FALSE;
4275 ++len;
4276 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004277 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 else
4279 ic = p_ic;
4280
4281 /*
4282 * Get the second variable.
4283 */
4284 *arg = skipwhite(p + len);
4285 if (eval5(arg, &var2, evaluate) == FAIL)
4286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 return FAIL;
4289 }
4290
4291 if (evaluate)
4292 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004293 if (type_is && rettv->v_type != var2.v_type)
4294 {
4295 /* For "is" a different type always means FALSE, for "notis"
4296 * it means TRUE. */
4297 n1 = (type == TYPE_NEQUAL);
4298 }
4299 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4300 {
4301 if (type_is)
4302 {
4303 n1 = (rettv->v_type == var2.v_type
4304 && rettv->vval.v_list == var2.vval.v_list);
4305 if (type == TYPE_NEQUAL)
4306 n1 = !n1;
4307 }
4308 else if (rettv->v_type != var2.v_type
4309 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4310 {
4311 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004312 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004313 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004314 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004315 clear_tv(rettv);
4316 clear_tv(&var2);
4317 return FAIL;
4318 }
4319 else
4320 {
4321 /* Compare two Lists for being equal or unequal. */
4322 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4323 if (type == TYPE_NEQUAL)
4324 n1 = !n1;
4325 }
4326 }
4327
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004328 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4329 {
4330 if (type_is)
4331 {
4332 n1 = (rettv->v_type == var2.v_type
4333 && rettv->vval.v_dict == var2.vval.v_dict);
4334 if (type == TYPE_NEQUAL)
4335 n1 = !n1;
4336 }
4337 else if (rettv->v_type != var2.v_type
4338 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4339 {
4340 if (rettv->v_type != var2.v_type)
4341 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4342 else
4343 EMSG(_("E736: Invalid operation for Dictionary"));
4344 clear_tv(rettv);
4345 clear_tv(&var2);
4346 return FAIL;
4347 }
4348 else
4349 {
4350 /* Compare two Dictionaries for being equal or unequal. */
4351 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4352 if (type == TYPE_NEQUAL)
4353 n1 = !n1;
4354 }
4355 }
4356
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004357 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4358 {
4359 if (rettv->v_type != var2.v_type
4360 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4361 {
4362 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004363 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004364 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004365 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004366 clear_tv(rettv);
4367 clear_tv(&var2);
4368 return FAIL;
4369 }
4370 else
4371 {
4372 /* Compare two Funcrefs for being equal or unequal. */
4373 if (rettv->vval.v_string == NULL
4374 || var2.vval.v_string == NULL)
4375 n1 = FALSE;
4376 else
4377 n1 = STRCMP(rettv->vval.v_string,
4378 var2.vval.v_string) == 0;
4379 if (type == TYPE_NEQUAL)
4380 n1 = !n1;
4381 }
4382 }
4383
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004384#ifdef FEAT_FLOAT
4385 /*
4386 * If one of the two variables is a float, compare as a float.
4387 * When using "=~" or "!~", always compare as string.
4388 */
4389 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4390 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4391 {
4392 float_T f1, f2;
4393
4394 if (rettv->v_type == VAR_FLOAT)
4395 f1 = rettv->vval.v_float;
4396 else
4397 f1 = get_tv_number(rettv);
4398 if (var2.v_type == VAR_FLOAT)
4399 f2 = var2.vval.v_float;
4400 else
4401 f2 = get_tv_number(&var2);
4402 n1 = FALSE;
4403 switch (type)
4404 {
4405 case TYPE_EQUAL: n1 = (f1 == f2); break;
4406 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4407 case TYPE_GREATER: n1 = (f1 > f2); break;
4408 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4409 case TYPE_SMALLER: n1 = (f1 < f2); break;
4410 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4411 case TYPE_UNKNOWN:
4412 case TYPE_MATCH:
4413 case TYPE_NOMATCH: break; /* avoid gcc warning */
4414 }
4415 }
4416#endif
4417
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 /*
4419 * If one of the two variables is a number, compare as a number.
4420 * When using "=~" or "!~", always compare as string.
4421 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004422 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4424 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004425 n1 = get_tv_number(rettv);
4426 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 switch (type)
4428 {
4429 case TYPE_EQUAL: n1 = (n1 == n2); break;
4430 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4431 case TYPE_GREATER: n1 = (n1 > n2); break;
4432 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4433 case TYPE_SMALLER: n1 = (n1 < n2); break;
4434 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4435 case TYPE_UNKNOWN:
4436 case TYPE_MATCH:
4437 case TYPE_NOMATCH: break; /* avoid gcc warning */
4438 }
4439 }
4440 else
4441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004442 s1 = get_tv_string_buf(rettv, buf1);
4443 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4445 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4446 else
4447 i = 0;
4448 n1 = FALSE;
4449 switch (type)
4450 {
4451 case TYPE_EQUAL: n1 = (i == 0); break;
4452 case TYPE_NEQUAL: n1 = (i != 0); break;
4453 case TYPE_GREATER: n1 = (i > 0); break;
4454 case TYPE_GEQUAL: n1 = (i >= 0); break;
4455 case TYPE_SMALLER: n1 = (i < 0); break;
4456 case TYPE_SEQUAL: n1 = (i <= 0); break;
4457
4458 case TYPE_MATCH:
4459 case TYPE_NOMATCH:
4460 /* avoid 'l' flag in 'cpoptions' */
4461 save_cpo = p_cpo;
4462 p_cpo = (char_u *)"";
4463 regmatch.regprog = vim_regcomp(s2,
4464 RE_MAGIC + RE_STRING);
4465 regmatch.rm_ic = ic;
4466 if (regmatch.regprog != NULL)
4467 {
4468 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4469 vim_free(regmatch.regprog);
4470 if (type == TYPE_NOMATCH)
4471 n1 = !n1;
4472 }
4473 p_cpo = save_cpo;
4474 break;
4475
4476 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4477 }
4478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004479 clear_tv(rettv);
4480 clear_tv(&var2);
4481 rettv->v_type = VAR_NUMBER;
4482 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 }
4484 }
4485
4486 return OK;
4487}
4488
4489/*
4490 * Handle fourth level expression:
4491 * + number addition
4492 * - number subtraction
4493 * . string concatenation
4494 *
4495 * "arg" must point to the first non-white of the expression.
4496 * "arg" is advanced to the next non-white after the recognized expression.
4497 *
4498 * Return OK or FAIL.
4499 */
4500 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004501eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 int evaluate;
4505{
Bram Moolenaar33570922005-01-25 22:26:29 +00004506 typval_T var2;
4507 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 int op;
4509 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004510#ifdef FEAT_FLOAT
4511 float_T f1 = 0, f2 = 0;
4512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 char_u *s1, *s2;
4514 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4515 char_u *p;
4516
4517 /*
4518 * Get the first variable.
4519 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004520 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 return FAIL;
4522
4523 /*
4524 * Repeat computing, until no '+', '-' or '.' is following.
4525 */
4526 for (;;)
4527 {
4528 op = **arg;
4529 if (op != '+' && op != '-' && op != '.')
4530 break;
4531
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004532 if ((op != '+' || rettv->v_type != VAR_LIST)
4533#ifdef FEAT_FLOAT
4534 && (op == '.' || rettv->v_type != VAR_FLOAT)
4535#endif
4536 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004537 {
4538 /* For "list + ...", an illegal use of the first operand as
4539 * a number cannot be determined before evaluating the 2nd
4540 * operand: if this is also a list, all is ok.
4541 * For "something . ...", "something - ..." or "non-list + ...",
4542 * we know that the first operand needs to be a string or number
4543 * without evaluating the 2nd operand. So check before to avoid
4544 * side effects after an error. */
4545 if (evaluate && get_tv_string_chk(rettv) == NULL)
4546 {
4547 clear_tv(rettv);
4548 return FAIL;
4549 }
4550 }
4551
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 /*
4553 * Get the second variable.
4554 */
4555 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004556 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004558 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 return FAIL;
4560 }
4561
4562 if (evaluate)
4563 {
4564 /*
4565 * Compute the result.
4566 */
4567 if (op == '.')
4568 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004569 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4570 s2 = get_tv_string_buf_chk(&var2, buf2);
4571 if (s2 == NULL) /* type error ? */
4572 {
4573 clear_tv(rettv);
4574 clear_tv(&var2);
4575 return FAIL;
4576 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004577 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004578 clear_tv(rettv);
4579 rettv->v_type = VAR_STRING;
4580 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004582 else if (op == '+' && rettv->v_type == VAR_LIST
4583 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004584 {
4585 /* concatenate Lists */
4586 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4587 &var3) == FAIL)
4588 {
4589 clear_tv(rettv);
4590 clear_tv(&var2);
4591 return FAIL;
4592 }
4593 clear_tv(rettv);
4594 *rettv = var3;
4595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 else
4597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004598 int error = FALSE;
4599
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004600#ifdef FEAT_FLOAT
4601 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004602 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004603 f1 = rettv->vval.v_float;
4604 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004605 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004606 else
4607#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004608 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004609 n1 = get_tv_number_chk(rettv, &error);
4610 if (error)
4611 {
4612 /* This can only happen for "list + non-list". For
4613 * "non-list + ..." or "something - ...", we returned
4614 * before evaluating the 2nd operand. */
4615 clear_tv(rettv);
4616 return FAIL;
4617 }
4618#ifdef FEAT_FLOAT
4619 if (var2.v_type == VAR_FLOAT)
4620 f1 = n1;
4621#endif
4622 }
4623#ifdef FEAT_FLOAT
4624 if (var2.v_type == VAR_FLOAT)
4625 {
4626 f2 = var2.vval.v_float;
4627 n2 = 0;
4628 }
4629 else
4630#endif
4631 {
4632 n2 = get_tv_number_chk(&var2, &error);
4633 if (error)
4634 {
4635 clear_tv(rettv);
4636 clear_tv(&var2);
4637 return FAIL;
4638 }
4639#ifdef FEAT_FLOAT
4640 if (rettv->v_type == VAR_FLOAT)
4641 f2 = n2;
4642#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004643 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004644 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004645
4646#ifdef FEAT_FLOAT
4647 /* If there is a float on either side the result is a float. */
4648 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4649 {
4650 if (op == '+')
4651 f1 = f1 + f2;
4652 else
4653 f1 = f1 - f2;
4654 rettv->v_type = VAR_FLOAT;
4655 rettv->vval.v_float = f1;
4656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004658#endif
4659 {
4660 if (op == '+')
4661 n1 = n1 + n2;
4662 else
4663 n1 = n1 - n2;
4664 rettv->v_type = VAR_NUMBER;
4665 rettv->vval.v_number = n1;
4666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004668 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 }
4670 }
4671 return OK;
4672}
4673
4674/*
4675 * Handle fifth level expression:
4676 * * number multiplication
4677 * / number division
4678 * % number modulo
4679 *
4680 * "arg" must point to the first non-white of the expression.
4681 * "arg" is advanced to the next non-white after the recognized expression.
4682 *
4683 * Return OK or FAIL.
4684 */
4685 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004686eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004690 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691{
Bram Moolenaar33570922005-01-25 22:26:29 +00004692 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 int op;
4694 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004695#ifdef FEAT_FLOAT
4696 int use_float = FALSE;
4697 float_T f1 = 0, f2;
4698#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004699 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
4701 /*
4702 * Get the first variable.
4703 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004704 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 return FAIL;
4706
4707 /*
4708 * Repeat computing, until no '*', '/' or '%' is following.
4709 */
4710 for (;;)
4711 {
4712 op = **arg;
4713 if (op != '*' && op != '/' && op != '%')
4714 break;
4715
4716 if (evaluate)
4717 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004718#ifdef FEAT_FLOAT
4719 if (rettv->v_type == VAR_FLOAT)
4720 {
4721 f1 = rettv->vval.v_float;
4722 use_float = TRUE;
4723 n1 = 0;
4724 }
4725 else
4726#endif
4727 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004728 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004729 if (error)
4730 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 }
4732 else
4733 n1 = 0;
4734
4735 /*
4736 * Get the second variable.
4737 */
4738 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004739 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 return FAIL;
4741
4742 if (evaluate)
4743 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004744#ifdef FEAT_FLOAT
4745 if (var2.v_type == VAR_FLOAT)
4746 {
4747 if (!use_float)
4748 {
4749 f1 = n1;
4750 use_float = TRUE;
4751 }
4752 f2 = var2.vval.v_float;
4753 n2 = 0;
4754 }
4755 else
4756#endif
4757 {
4758 n2 = get_tv_number_chk(&var2, &error);
4759 clear_tv(&var2);
4760 if (error)
4761 return FAIL;
4762#ifdef FEAT_FLOAT
4763 if (use_float)
4764 f2 = n2;
4765#endif
4766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767
4768 /*
4769 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004770 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004772#ifdef FEAT_FLOAT
4773 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004775 if (op == '*')
4776 f1 = f1 * f2;
4777 else if (op == '/')
4778 {
4779 /* We rely on the floating point library to handle divide
4780 * by zero to result in "inf" and not a crash. */
4781 f1 = f1 / f2;
4782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004784 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004785 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786 return FAIL;
4787 }
4788 rettv->v_type = VAR_FLOAT;
4789 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 }
4791 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004794 if (op == '*')
4795 n1 = n1 * n2;
4796 else if (op == '/')
4797 {
4798 if (n2 == 0) /* give an error message? */
4799 {
4800 if (n1 == 0)
4801 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4802 else if (n1 < 0)
4803 n1 = -0x7fffffffL;
4804 else
4805 n1 = 0x7fffffffL;
4806 }
4807 else
4808 n1 = n1 / n2;
4809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811 {
4812 if (n2 == 0) /* give an error message? */
4813 n1 = 0;
4814 else
4815 n1 = n1 % n2;
4816 }
4817 rettv->v_type = VAR_NUMBER;
4818 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 }
4821 }
4822
4823 return OK;
4824}
4825
4826/*
4827 * Handle sixth level expression:
4828 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004829 * "string" string constant
4830 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 * &option-name option value
4832 * @r register contents
4833 * identifier variable value
4834 * function() function call
4835 * $VAR environment variable
4836 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004837 * [expr, expr] List
4838 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 *
4840 * Also handle:
4841 * ! in front logical NOT
4842 * - in front unary minus
4843 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004844 * trailing [] subscript in String or List
4845 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 *
4847 * "arg" must point to the first non-white of the expression.
4848 * "arg" is advanced to the next non-white after the recognized expression.
4849 *
4850 * Return OK or FAIL.
4851 */
4852 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004853eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004857 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 long n;
4860 int len;
4861 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 char_u *start_leader, *end_leader;
4863 int ret = OK;
4864 char_u *alias;
4865
4866 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004867 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004868 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004870 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
4872 /*
4873 * Skip '!' and '-' characters. They are handled later.
4874 */
4875 start_leader = *arg;
4876 while (**arg == '!' || **arg == '-' || **arg == '+')
4877 *arg = skipwhite(*arg + 1);
4878 end_leader = *arg;
4879
4880 switch (**arg)
4881 {
4882 /*
4883 * Number constant.
4884 */
4885 case '0':
4886 case '1':
4887 case '2':
4888 case '3':
4889 case '4':
4890 case '5':
4891 case '6':
4892 case '7':
4893 case '8':
4894 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895 {
4896#ifdef FEAT_FLOAT
4897 char_u *p = skipdigits(*arg + 1);
4898 int get_float = FALSE;
4899
4900 /* We accept a float when the format matches
4901 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004902 * strict to avoid backwards compatibility problems.
4903 * Don't look for a float after the "." operator, so that
4904 * ":let vers = 1.2.3" doesn't fail. */
4905 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004907 get_float = TRUE;
4908 p = skipdigits(p + 2);
4909 if (*p == 'e' || *p == 'E')
4910 {
4911 ++p;
4912 if (*p == '-' || *p == '+')
4913 ++p;
4914 if (!vim_isdigit(*p))
4915 get_float = FALSE;
4916 else
4917 p = skipdigits(p + 1);
4918 }
4919 if (ASCII_ISALPHA(*p) || *p == '.')
4920 get_float = FALSE;
4921 }
4922 if (get_float)
4923 {
4924 float_T f;
4925
4926 *arg += string2float(*arg, &f);
4927 if (evaluate)
4928 {
4929 rettv->v_type = VAR_FLOAT;
4930 rettv->vval.v_float = f;
4931 }
4932 }
4933 else
4934#endif
4935 {
4936 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4937 *arg += len;
4938 if (evaluate)
4939 {
4940 rettv->v_type = VAR_NUMBER;
4941 rettv->vval.v_number = n;
4942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 }
4944 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946
4947 /*
4948 * String constant: "string".
4949 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004950 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 break;
4952
4953 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004954 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004956 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004957 break;
4958
4959 /*
4960 * List: [expr, expr]
4961 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004962 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 break;
4964
4965 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004966 * Dictionary: {key: val, key: val}
4967 */
4968 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4969 break;
4970
4971 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004972 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004974 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 break;
4976
4977 /*
4978 * Environment variable: $VAR.
4979 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004980 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 break;
4982
4983 /*
4984 * Register contents: @r.
4985 */
4986 case '@': ++*arg;
4987 if (evaluate)
4988 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004989 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004990 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 if (**arg != NUL)
4993 ++*arg;
4994 break;
4995
4996 /*
4997 * nested expression: (expression).
4998 */
4999 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005000 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 if (**arg == ')')
5002 ++*arg;
5003 else if (ret == OK)
5004 {
5005 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005006 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 ret = FAIL;
5008 }
5009 break;
5010
Bram Moolenaar8c711452005-01-14 21:53:12 +00005011 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 break;
5013 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014
5015 if (ret == NOTDONE)
5016 {
5017 /*
5018 * Must be a variable or function name.
5019 * Can also be a curly-braces kind of name: {expr}.
5020 */
5021 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005022 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005023 if (alias != NULL)
5024 s = alias;
5025
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005026 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 ret = FAIL;
5028 else
5029 {
5030 if (**arg == '(') /* recursive! */
5031 {
5032 /* If "s" is the name of a variable of type VAR_FUNC
5033 * use its contents. */
5034 s = deref_func_name(s, &len);
5035
5036 /* Invoke the function. */
5037 ret = get_func_tv(s, len, rettv, arg,
5038 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005039 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 /* Stop the expression evaluation when immediately
5041 * aborting on error, or when an interrupt occurred or
5042 * an exception was thrown but not caught. */
5043 if (aborting())
5044 {
5045 if (ret == OK)
5046 clear_tv(rettv);
5047 ret = FAIL;
5048 }
5049 }
5050 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005051 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005052 else
5053 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 }
5055
5056 if (alias != NULL)
5057 vim_free(alias);
5058 }
5059
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 *arg = skipwhite(*arg);
5061
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005062 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5063 * expr(expr). */
5064 if (ret == OK)
5065 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066
5067 /*
5068 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5069 */
5070 if (ret == OK && evaluate && end_leader > start_leader)
5071 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005072 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005073 int val = 0;
5074#ifdef FEAT_FLOAT
5075 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005076
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005077 if (rettv->v_type == VAR_FLOAT)
5078 f = rettv->vval.v_float;
5079 else
5080#endif
5081 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005082 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005084 clear_tv(rettv);
5085 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005087 else
5088 {
5089 while (end_leader > start_leader)
5090 {
5091 --end_leader;
5092 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005093 {
5094#ifdef FEAT_FLOAT
5095 if (rettv->v_type == VAR_FLOAT)
5096 f = !f;
5097 else
5098#endif
5099 val = !val;
5100 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005101 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005102 {
5103#ifdef FEAT_FLOAT
5104 if (rettv->v_type == VAR_FLOAT)
5105 f = -f;
5106 else
5107#endif
5108 val = -val;
5109 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005110 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005111#ifdef FEAT_FLOAT
5112 if (rettv->v_type == VAR_FLOAT)
5113 {
5114 clear_tv(rettv);
5115 rettv->vval.v_float = f;
5116 }
5117 else
5118#endif
5119 {
5120 clear_tv(rettv);
5121 rettv->v_type = VAR_NUMBER;
5122 rettv->vval.v_number = val;
5123 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
5126
5127 return ret;
5128}
5129
5130/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005131 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5132 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005133 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5134 */
5135 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005136eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005137 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005138 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005139 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005140 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005141{
5142 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005143 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005144 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 long len = -1;
5146 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005147 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005149
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005150 if (rettv->v_type == VAR_FUNC
5151#ifdef FEAT_FLOAT
5152 || rettv->v_type == VAR_FLOAT
5153#endif
5154 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005155 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005156 if (verbose)
5157 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 return FAIL;
5159 }
5160
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005162 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 /*
5164 * dict.name
5165 */
5166 key = *arg + 1;
5167 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5168 ;
5169 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005170 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005172 }
5173 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005175 /*
5176 * something[idx]
5177 *
5178 * Get the (first) variable from inside the [].
5179 */
5180 *arg = skipwhite(*arg + 1);
5181 if (**arg == ':')
5182 empty1 = TRUE;
5183 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5184 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005185 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5186 {
5187 /* not a number or string */
5188 clear_tv(&var1);
5189 return FAIL;
5190 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005191
5192 /*
5193 * Get the second variable from inside the [:].
5194 */
5195 if (**arg == ':')
5196 {
5197 range = TRUE;
5198 *arg = skipwhite(*arg + 1);
5199 if (**arg == ']')
5200 empty2 = TRUE;
5201 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005203 if (!empty1)
5204 clear_tv(&var1);
5205 return FAIL;
5206 }
5207 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5208 {
5209 /* not a number or string */
5210 if (!empty1)
5211 clear_tv(&var1);
5212 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 return FAIL;
5214 }
5215 }
5216
5217 /* Check for the ']'. */
5218 if (**arg != ']')
5219 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005220 if (verbose)
5221 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 clear_tv(&var1);
5223 if (range)
5224 clear_tv(&var2);
5225 return FAIL;
5226 }
5227 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228 }
5229
5230 if (evaluate)
5231 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232 n1 = 0;
5233 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005234 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005235 n1 = get_tv_number(&var1);
5236 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237 }
5238 if (range)
5239 {
5240 if (empty2)
5241 n2 = -1;
5242 else
5243 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005244 n2 = get_tv_number(&var2);
5245 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246 }
5247 }
5248
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005249 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 {
5251 case VAR_NUMBER:
5252 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005253 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 len = (long)STRLEN(s);
5255 if (range)
5256 {
5257 /* The resulting variable is a substring. If the indexes
5258 * are out of range the result is empty. */
5259 if (n1 < 0)
5260 {
5261 n1 = len + n1;
5262 if (n1 < 0)
5263 n1 = 0;
5264 }
5265 if (n2 < 0)
5266 n2 = len + n2;
5267 else if (n2 >= len)
5268 n2 = len;
5269 if (n1 >= len || n2 < 0 || n1 > n2)
5270 s = NULL;
5271 else
5272 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5273 }
5274 else
5275 {
5276 /* The resulting variable is a string of a single
5277 * character. If the index is too big or negative the
5278 * result is empty. */
5279 if (n1 >= len || n1 < 0)
5280 s = NULL;
5281 else
5282 s = vim_strnsave(s + n1, 1);
5283 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005284 clear_tv(rettv);
5285 rettv->v_type = VAR_STRING;
5286 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 break;
5288
5289 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005290 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 if (n1 < 0)
5292 n1 = len + n1;
5293 if (!empty1 && (n1 < 0 || n1 >= len))
5294 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005295 /* For a range we allow invalid values and return an empty
5296 * list. A list index out of range is an error. */
5297 if (!range)
5298 {
5299 if (verbose)
5300 EMSGN(_(e_listidx), n1);
5301 return FAIL;
5302 }
5303 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 }
5305 if (range)
5306 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005307 list_T *l;
5308 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309
5310 if (n2 < 0)
5311 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005312 else if (n2 >= len)
5313 n2 = len - 1;
5314 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005315 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005316 l = list_alloc();
5317 if (l == NULL)
5318 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005319 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 n1 <= n2; ++n1)
5321 {
5322 if (list_append_tv(l, &item->li_tv) == FAIL)
5323 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005324 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325 return FAIL;
5326 }
5327 item = item->li_next;
5328 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005329 clear_tv(rettv);
5330 rettv->v_type = VAR_LIST;
5331 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005332 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333 }
5334 else
5335 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005336 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005337 clear_tv(rettv);
5338 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005339 }
5340 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341
5342 case VAR_DICT:
5343 if (range)
5344 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005345 if (verbose)
5346 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005347 if (len == -1)
5348 clear_tv(&var1);
5349 return FAIL;
5350 }
5351 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005352 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353
5354 if (len == -1)
5355 {
5356 key = get_tv_string(&var1);
5357 if (*key == NUL)
5358 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005359 if (verbose)
5360 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005361 clear_tv(&var1);
5362 return FAIL;
5363 }
5364 }
5365
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005366 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005367
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005368 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005369 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005370 if (len == -1)
5371 clear_tv(&var1);
5372 if (item == NULL)
5373 return FAIL;
5374
5375 copy_tv(&item->di_tv, &var1);
5376 clear_tv(rettv);
5377 *rettv = var1;
5378 }
5379 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 }
5381 }
5382
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 return OK;
5384}
5385
5386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 * Get an option value.
5388 * "arg" points to the '&' or '+' before the option name.
5389 * "arg" is advanced to character after the option name.
5390 * Return OK or FAIL.
5391 */
5392 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005393get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005395 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 int evaluate;
5397{
5398 char_u *option_end;
5399 long numval;
5400 char_u *stringval;
5401 int opt_type;
5402 int c;
5403 int working = (**arg == '+'); /* has("+option") */
5404 int ret = OK;
5405 int opt_flags;
5406
5407 /*
5408 * Isolate the option name and find its value.
5409 */
5410 option_end = find_option_end(arg, &opt_flags);
5411 if (option_end == NULL)
5412 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005413 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 EMSG2(_("E112: Option name missing: %s"), *arg);
5415 return FAIL;
5416 }
5417
5418 if (!evaluate)
5419 {
5420 *arg = option_end;
5421 return OK;
5422 }
5423
5424 c = *option_end;
5425 *option_end = NUL;
5426 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005427 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428
5429 if (opt_type == -3) /* invalid name */
5430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005431 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 EMSG2(_("E113: Unknown option: %s"), *arg);
5433 ret = FAIL;
5434 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005435 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 {
5437 if (opt_type == -2) /* hidden string option */
5438 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005439 rettv->v_type = VAR_STRING;
5440 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 }
5442 else if (opt_type == -1) /* hidden number option */
5443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005444 rettv->v_type = VAR_NUMBER;
5445 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 }
5447 else if (opt_type == 1) /* number option */
5448 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005449 rettv->v_type = VAR_NUMBER;
5450 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 }
5452 else /* string option */
5453 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005454 rettv->v_type = VAR_STRING;
5455 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 }
5457 }
5458 else if (working && (opt_type == -2 || opt_type == -1))
5459 ret = FAIL;
5460
5461 *option_end = c; /* put back for error messages */
5462 *arg = option_end;
5463
5464 return ret;
5465}
5466
5467/*
5468 * Allocate a variable for a string constant.
5469 * Return OK or FAIL.
5470 */
5471 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005472get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 int evaluate;
5476{
5477 char_u *p;
5478 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 int extra = 0;
5480
5481 /*
5482 * Find the end of the string, skipping backslashed characters.
5483 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005484 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 {
5486 if (*p == '\\' && p[1] != NUL)
5487 {
5488 ++p;
5489 /* A "\<x>" form occupies at least 4 characters, and produces up
5490 * to 6 characters: reserve space for 2 extra */
5491 if (*p == '<')
5492 extra += 2;
5493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 }
5495
5496 if (*p != '"')
5497 {
5498 EMSG2(_("E114: Missing quote: %s"), *arg);
5499 return FAIL;
5500 }
5501
5502 /* If only parsing, set *arg and return here */
5503 if (!evaluate)
5504 {
5505 *arg = p + 1;
5506 return OK;
5507 }
5508
5509 /*
5510 * Copy the string into allocated memory, handling backslashed
5511 * characters.
5512 */
5513 name = alloc((unsigned)(p - *arg + extra));
5514 if (name == NULL)
5515 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005516 rettv->v_type = VAR_STRING;
5517 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518
Bram Moolenaar8c711452005-01-14 21:53:12 +00005519 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 {
5521 if (*p == '\\')
5522 {
5523 switch (*++p)
5524 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005525 case 'b': *name++ = BS; ++p; break;
5526 case 'e': *name++ = ESC; ++p; break;
5527 case 'f': *name++ = FF; ++p; break;
5528 case 'n': *name++ = NL; ++p; break;
5529 case 'r': *name++ = CAR; ++p; break;
5530 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531
5532 case 'X': /* hex: "\x1", "\x12" */
5533 case 'x':
5534 case 'u': /* Unicode: "\u0023" */
5535 case 'U':
5536 if (vim_isxdigit(p[1]))
5537 {
5538 int n, nr;
5539 int c = toupper(*p);
5540
5541 if (c == 'X')
5542 n = 2;
5543 else
5544 n = 4;
5545 nr = 0;
5546 while (--n >= 0 && vim_isxdigit(p[1]))
5547 {
5548 ++p;
5549 nr = (nr << 4) + hex2nr(*p);
5550 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005551 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552#ifdef FEAT_MBYTE
5553 /* For "\u" store the number according to
5554 * 'encoding'. */
5555 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005556 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 else
5558#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005559 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 break;
5562
5563 /* octal: "\1", "\12", "\123" */
5564 case '0':
5565 case '1':
5566 case '2':
5567 case '3':
5568 case '4':
5569 case '5':
5570 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005571 case '7': *name = *p++ - '0';
5572 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574 *name = (*name << 3) + *p++ - '0';
5575 if (*p >= '0' && *p <= '7')
5576 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005578 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 break;
5580
5581 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005582 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 if (extra != 0)
5584 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 break;
5587 }
5588 /* FALLTHROUGH */
5589
Bram Moolenaar8c711452005-01-14 21:53:12 +00005590 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 break;
5592 }
5593 }
5594 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005598 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 *arg = p + 1;
5600
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 return OK;
5602}
5603
5604/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005605 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 * Return OK or FAIL.
5607 */
5608 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005609get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 int evaluate;
5613{
5614 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005615 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005616 int reduce = 0;
5617
5618 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005619 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005620 */
5621 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5622 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005624 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005625 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005626 break;
5627 ++reduce;
5628 ++p;
5629 }
5630 }
5631
Bram Moolenaar8c711452005-01-14 21:53:12 +00005632 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005633 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005634 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005635 return FAIL;
5636 }
5637
Bram Moolenaar8c711452005-01-14 21:53:12 +00005638 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005639 if (!evaluate)
5640 {
5641 *arg = p + 1;
5642 return OK;
5643 }
5644
5645 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005647 */
5648 str = alloc((unsigned)((p - *arg) - reduce));
5649 if (str == NULL)
5650 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 rettv->v_type = VAR_STRING;
5652 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005653
Bram Moolenaar8c711452005-01-14 21:53:12 +00005654 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005655 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005656 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005657 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005659 break;
5660 ++p;
5661 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005662 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005663 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005664 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005665 *arg = p + 1;
5666
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005667 return OK;
5668}
5669
5670/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005671 * Allocate a variable for a List and fill it from "*arg".
5672 * Return OK or FAIL.
5673 */
5674 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005676 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005677 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005678 int evaluate;
5679{
Bram Moolenaar33570922005-01-25 22:26:29 +00005680 list_T *l = NULL;
5681 typval_T tv;
5682 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005683
5684 if (evaluate)
5685 {
5686 l = list_alloc();
5687 if (l == NULL)
5688 return FAIL;
5689 }
5690
5691 *arg = skipwhite(*arg + 1);
5692 while (**arg != ']' && **arg != NUL)
5693 {
5694 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5695 goto failret;
5696 if (evaluate)
5697 {
5698 item = listitem_alloc();
5699 if (item != NULL)
5700 {
5701 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005702 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005703 list_append(l, item);
5704 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005705 else
5706 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005707 }
5708
5709 if (**arg == ']')
5710 break;
5711 if (**arg != ',')
5712 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005713 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005714 goto failret;
5715 }
5716 *arg = skipwhite(*arg + 1);
5717 }
5718
5719 if (**arg != ']')
5720 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005721 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005722failret:
5723 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005724 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005725 return FAIL;
5726 }
5727
5728 *arg = skipwhite(*arg + 1);
5729 if (evaluate)
5730 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005731 rettv->v_type = VAR_LIST;
5732 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005733 ++l->lv_refcount;
5734 }
5735
5736 return OK;
5737}
5738
5739/*
5740 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005741 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005742 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005743 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005744list_alloc()
5745{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005746 list_T *l;
5747
5748 l = (list_T *)alloc_clear(sizeof(list_T));
5749 if (l != NULL)
5750 {
5751 /* Prepend the list to the list of lists for garbage collection. */
5752 if (first_list != NULL)
5753 first_list->lv_used_prev = l;
5754 l->lv_used_prev = NULL;
5755 l->lv_used_next = first_list;
5756 first_list = l;
5757 }
5758 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759}
5760
5761/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005762 * Allocate an empty list for a return value.
5763 * Returns OK or FAIL.
5764 */
5765 static int
5766rettv_list_alloc(rettv)
5767 typval_T *rettv;
5768{
5769 list_T *l = list_alloc();
5770
5771 if (l == NULL)
5772 return FAIL;
5773
5774 rettv->vval.v_list = l;
5775 rettv->v_type = VAR_LIST;
5776 ++l->lv_refcount;
5777 return OK;
5778}
5779
5780/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005781 * Unreference a list: decrement the reference count and free it when it
5782 * becomes zero.
5783 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005784 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005785list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005786 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005787{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005788 if (l != NULL && --l->lv_refcount <= 0)
5789 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005790}
5791
5792/*
5793 * Free a list, including all items it points to.
5794 * Ignores the reference count.
5795 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005796 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005797list_free(l, recurse)
5798 list_T *l;
5799 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005800{
Bram Moolenaar33570922005-01-25 22:26:29 +00005801 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005803 /* Remove the list from the list of lists for garbage collection. */
5804 if (l->lv_used_prev == NULL)
5805 first_list = l->lv_used_next;
5806 else
5807 l->lv_used_prev->lv_used_next = l->lv_used_next;
5808 if (l->lv_used_next != NULL)
5809 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5810
Bram Moolenaard9fba312005-06-26 22:34:35 +00005811 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005812 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005813 /* Remove the item before deleting it. */
5814 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005815 if (recurse || (item->li_tv.v_type != VAR_LIST
5816 && item->li_tv.v_type != VAR_DICT))
5817 clear_tv(&item->li_tv);
5818 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005819 }
5820 vim_free(l);
5821}
5822
5823/*
5824 * Allocate a list item.
5825 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005826 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005827listitem_alloc()
5828{
Bram Moolenaar33570922005-01-25 22:26:29 +00005829 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005830}
5831
5832/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005833 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005834 */
5835 static void
5836listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005837 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005838{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005839 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005840 vim_free(item);
5841}
5842
5843/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005844 * Remove a list item from a List and free it. Also clears the value.
5845 */
5846 static void
5847listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005848 list_T *l;
5849 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005850{
5851 list_remove(l, item, item);
5852 listitem_free(item);
5853}
5854
5855/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856 * Get the number of items in a list.
5857 */
5858 static long
5859list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005860 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005861{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862 if (l == NULL)
5863 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005864 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865}
5866
5867/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005868 * Return TRUE when two lists have exactly the same values.
5869 */
5870 static int
5871list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005872 list_T *l1;
5873 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005874 int ic; /* ignore case for strings */
5875{
Bram Moolenaar33570922005-01-25 22:26:29 +00005876 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005877
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005878 if (l1 == NULL || l2 == NULL)
5879 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005880 if (l1 == l2)
5881 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005882 if (list_len(l1) != list_len(l2))
5883 return FALSE;
5884
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005885 for (item1 = l1->lv_first, item2 = l2->lv_first;
5886 item1 != NULL && item2 != NULL;
5887 item1 = item1->li_next, item2 = item2->li_next)
5888 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5889 return FALSE;
5890 return item1 == NULL && item2 == NULL;
5891}
5892
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01005893#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_MZSCHEME) \
5894 || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005895/*
5896 * Return the dictitem that an entry in a hashtable points to.
5897 */
5898 dictitem_T *
5899dict_lookup(hi)
5900 hashitem_T *hi;
5901{
5902 return HI2DI(hi);
5903}
5904#endif
5905
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005906/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005907 * Return TRUE when two dictionaries have exactly the same key/values.
5908 */
5909 static int
5910dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005911 dict_T *d1;
5912 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005913 int ic; /* ignore case for strings */
5914{
Bram Moolenaar33570922005-01-25 22:26:29 +00005915 hashitem_T *hi;
5916 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005917 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005918
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005919 if (d1 == NULL || d2 == NULL)
5920 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005921 if (d1 == d2)
5922 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005923 if (dict_len(d1) != dict_len(d2))
5924 return FALSE;
5925
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005926 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005927 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005928 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005929 if (!HASHITEM_EMPTY(hi))
5930 {
5931 item2 = dict_find(d2, hi->hi_key, -1);
5932 if (item2 == NULL)
5933 return FALSE;
5934 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5935 return FALSE;
5936 --todo;
5937 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005938 }
5939 return TRUE;
5940}
5941
5942/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005943 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005944 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005945 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005946 */
5947 static int
5948tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005949 typval_T *tv1;
5950 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005951 int ic; /* ignore case */
5952{
5953 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005954 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005955 static int recursive = 0; /* cach recursive loops */
5956 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005957
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005958 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005959 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005960 /* Catch lists and dicts that have an endless loop by limiting
5961 * recursiveness to 1000. We guess they are equal then. */
5962 if (recursive >= 1000)
5963 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005964
5965 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005966 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005967 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005968 ++recursive;
5969 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5970 --recursive;
5971 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005972
5973 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005974 ++recursive;
5975 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5976 --recursive;
5977 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005978
5979 case VAR_FUNC:
5980 return (tv1->vval.v_string != NULL
5981 && tv2->vval.v_string != NULL
5982 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5983
5984 case VAR_NUMBER:
5985 return tv1->vval.v_number == tv2->vval.v_number;
5986
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005987#ifdef FEAT_FLOAT
5988 case VAR_FLOAT:
5989 return tv1->vval.v_float == tv2->vval.v_float;
5990#endif
5991
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005992 case VAR_STRING:
5993 s1 = get_tv_string_buf(tv1, buf1);
5994 s2 = get_tv_string_buf(tv2, buf2);
5995 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005996 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005997
5998 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005999 return TRUE;
6000}
6001
6002/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003 * Locate item with index "n" in list "l" and return it.
6004 * A negative index is counted from the end; -1 is the last item.
6005 * Returns NULL when "n" is out of range.
6006 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006007 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006009 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010 long n;
6011{
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013 long idx;
6014
6015 if (l == NULL)
6016 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006017
6018 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006020 n = l->lv_len + n;
6021
6022 /* Check for index out of range. */
6023 if (n < 0 || n >= l->lv_len)
6024 return NULL;
6025
6026 /* When there is a cached index may start search from there. */
6027 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006029 if (n < l->lv_idx / 2)
6030 {
6031 /* closest to the start of the list */
6032 item = l->lv_first;
6033 idx = 0;
6034 }
6035 else if (n > (l->lv_idx + l->lv_len) / 2)
6036 {
6037 /* closest to the end of the list */
6038 item = l->lv_last;
6039 idx = l->lv_len - 1;
6040 }
6041 else
6042 {
6043 /* closest to the cached index */
6044 item = l->lv_idx_item;
6045 idx = l->lv_idx;
6046 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 }
6048 else
6049 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006050 if (n < l->lv_len / 2)
6051 {
6052 /* closest to the start of the list */
6053 item = l->lv_first;
6054 idx = 0;
6055 }
6056 else
6057 {
6058 /* closest to the end of the list */
6059 item = l->lv_last;
6060 idx = l->lv_len - 1;
6061 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006063
6064 while (n > idx)
6065 {
6066 /* search forward */
6067 item = item->li_next;
6068 ++idx;
6069 }
6070 while (n < idx)
6071 {
6072 /* search backward */
6073 item = item->li_prev;
6074 --idx;
6075 }
6076
6077 /* cache the used index */
6078 l->lv_idx = idx;
6079 l->lv_idx_item = item;
6080
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081 return item;
6082}
6083
6084/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006085 * Get list item "l[idx]" as a number.
6086 */
6087 static long
6088list_find_nr(l, idx, errorp)
6089 list_T *l;
6090 long idx;
6091 int *errorp; /* set to TRUE when something wrong */
6092{
6093 listitem_T *li;
6094
6095 li = list_find(l, idx);
6096 if (li == NULL)
6097 {
6098 if (errorp != NULL)
6099 *errorp = TRUE;
6100 return -1L;
6101 }
6102 return get_tv_number_chk(&li->li_tv, errorp);
6103}
6104
6105/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006106 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6107 */
6108 char_u *
6109list_find_str(l, idx)
6110 list_T *l;
6111 long idx;
6112{
6113 listitem_T *li;
6114
6115 li = list_find(l, idx - 1);
6116 if (li == NULL)
6117 {
6118 EMSGN(_(e_listidx), idx);
6119 return NULL;
6120 }
6121 return get_tv_string(&li->li_tv);
6122}
6123
6124/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006125 * Locate "item" list "l" and return its index.
6126 * Returns -1 when "item" is not in the list.
6127 */
6128 static long
6129list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006130 list_T *l;
6131 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006132{
6133 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006134 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006135
6136 if (l == NULL)
6137 return -1;
6138 idx = 0;
6139 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6140 ++idx;
6141 if (li == NULL)
6142 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006143 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006144}
6145
6146/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006147 * Append item "item" to the end of list "l".
6148 */
6149 static void
6150list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006151 list_T *l;
6152 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153{
6154 if (l->lv_last == NULL)
6155 {
6156 /* empty list */
6157 l->lv_first = item;
6158 l->lv_last = item;
6159 item->li_prev = NULL;
6160 }
6161 else
6162 {
6163 l->lv_last->li_next = item;
6164 item->li_prev = l->lv_last;
6165 l->lv_last = item;
6166 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006167 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168 item->li_next = NULL;
6169}
6170
6171/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006172 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006173 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006175 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006176list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006177 list_T *l;
6178 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006179{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006180 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181
Bram Moolenaar05159a02005-02-26 23:04:13 +00006182 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006183 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006184 copy_tv(tv, &li->li_tv);
6185 list_append(l, li);
6186 return OK;
6187}
6188
6189/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006190 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006191 * Return FAIL when out of memory.
6192 */
6193 int
6194list_append_dict(list, dict)
6195 list_T *list;
6196 dict_T *dict;
6197{
6198 listitem_T *li = listitem_alloc();
6199
6200 if (li == NULL)
6201 return FAIL;
6202 li->li_tv.v_type = VAR_DICT;
6203 li->li_tv.v_lock = 0;
6204 li->li_tv.vval.v_dict = dict;
6205 list_append(list, li);
6206 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006207 return OK;
6208}
6209
6210/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006211 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006212 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006213 * Returns FAIL when out of memory.
6214 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006215 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006216list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006217 list_T *l;
6218 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006219 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006220{
6221 listitem_T *li = listitem_alloc();
6222
6223 if (li == NULL)
6224 return FAIL;
6225 list_append(l, li);
6226 li->li_tv.v_type = VAR_STRING;
6227 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006228 if (str == NULL)
6229 li->li_tv.vval.v_string = NULL;
6230 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006231 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006232 return FAIL;
6233 return OK;
6234}
6235
6236/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006237 * Append "n" to list "l".
6238 * Returns FAIL when out of memory.
6239 */
6240 static int
6241list_append_number(l, n)
6242 list_T *l;
6243 varnumber_T n;
6244{
6245 listitem_T *li;
6246
6247 li = listitem_alloc();
6248 if (li == NULL)
6249 return FAIL;
6250 li->li_tv.v_type = VAR_NUMBER;
6251 li->li_tv.v_lock = 0;
6252 li->li_tv.vval.v_number = n;
6253 list_append(l, li);
6254 return OK;
6255}
6256
6257/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006258 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006259 * If "item" is NULL append at the end.
6260 * Return FAIL when out of memory.
6261 */
6262 static int
6263list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006264 list_T *l;
6265 typval_T *tv;
6266 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006267{
Bram Moolenaar33570922005-01-25 22:26:29 +00006268 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006269
6270 if (ni == NULL)
6271 return FAIL;
6272 copy_tv(tv, &ni->li_tv);
6273 if (item == NULL)
6274 /* Append new item at end of list. */
6275 list_append(l, ni);
6276 else
6277 {
6278 /* Insert new item before existing item. */
6279 ni->li_prev = item->li_prev;
6280 ni->li_next = item;
6281 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006282 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006283 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006284 ++l->lv_idx;
6285 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006286 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006287 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006288 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006289 l->lv_idx_item = NULL;
6290 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006291 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006292 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006293 }
6294 return OK;
6295}
6296
6297/*
6298 * Extend "l1" with "l2".
6299 * If "bef" is NULL append at the end, otherwise insert before this item.
6300 * Returns FAIL when out of memory.
6301 */
6302 static int
6303list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006304 list_T *l1;
6305 list_T *l2;
6306 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006307{
Bram Moolenaar33570922005-01-25 22:26:29 +00006308 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006309 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006310
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006311 /* We also quit the loop when we have inserted the original item count of
6312 * the list, avoid a hang when we extend a list with itself. */
6313 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006314 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6315 return FAIL;
6316 return OK;
6317}
6318
6319/*
6320 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6321 * Return FAIL when out of memory.
6322 */
6323 static int
6324list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006325 list_T *l1;
6326 list_T *l2;
6327 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006328{
Bram Moolenaar33570922005-01-25 22:26:29 +00006329 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006330
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006331 if (l1 == NULL || l2 == NULL)
6332 return FAIL;
6333
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006334 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006335 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006336 if (l == NULL)
6337 return FAIL;
6338 tv->v_type = VAR_LIST;
6339 tv->vval.v_list = l;
6340
6341 /* append all items from the second list */
6342 return list_extend(l, l2, NULL);
6343}
6344
6345/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006346 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006347 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006348 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006349 * Returns NULL when out of memory.
6350 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006351 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006352list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006353 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006354 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006355 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006356{
Bram Moolenaar33570922005-01-25 22:26:29 +00006357 list_T *copy;
6358 listitem_T *item;
6359 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006360
6361 if (orig == NULL)
6362 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006363
6364 copy = list_alloc();
6365 if (copy != NULL)
6366 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006367 if (copyID != 0)
6368 {
6369 /* Do this before adding the items, because one of the items may
6370 * refer back to this list. */
6371 orig->lv_copyID = copyID;
6372 orig->lv_copylist = copy;
6373 }
6374 for (item = orig->lv_first; item != NULL && !got_int;
6375 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006376 {
6377 ni = listitem_alloc();
6378 if (ni == NULL)
6379 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006380 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006381 {
6382 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6383 {
6384 vim_free(ni);
6385 break;
6386 }
6387 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006388 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006389 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390 list_append(copy, ni);
6391 }
6392 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006393 if (item != NULL)
6394 {
6395 list_unref(copy);
6396 copy = NULL;
6397 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006398 }
6399
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400 return copy;
6401}
6402
6403/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006404 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006405 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006406 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006407 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006408list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006409 list_T *l;
6410 listitem_T *item;
6411 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006412{
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006415 /* notify watchers */
6416 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006418 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006419 list_fix_watch(l, ip);
6420 if (ip == item2)
6421 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006423
6424 if (item2->li_next == NULL)
6425 l->lv_last = item->li_prev;
6426 else
6427 item2->li_next->li_prev = item->li_prev;
6428 if (item->li_prev == NULL)
6429 l->lv_first = item2->li_next;
6430 else
6431 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006432 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006433}
6434
6435/*
6436 * Return an allocated string with the string representation of a list.
6437 * May return NULL.
6438 */
6439 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006440list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006441 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006442 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006443{
6444 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006445
6446 if (tv->vval.v_list == NULL)
6447 return NULL;
6448 ga_init2(&ga, (int)sizeof(char), 80);
6449 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006450 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006451 {
6452 vim_free(ga.ga_data);
6453 return NULL;
6454 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006455 ga_append(&ga, ']');
6456 ga_append(&ga, NUL);
6457 return (char_u *)ga.ga_data;
6458}
6459
6460/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006461 * Join list "l" into a string in "*gap", using separator "sep".
6462 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006463 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006464 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006465 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006466list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006467 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006469 char_u *sep;
6470 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006471 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006472{
6473 int first = TRUE;
6474 char_u *tofree;
6475 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006476 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006477 char_u *s;
6478
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006479 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006480 {
6481 if (first)
6482 first = FALSE;
6483 else
6484 ga_concat(gap, sep);
6485
6486 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006487 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006488 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006489 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006490 if (s != NULL)
6491 ga_concat(gap, s);
6492 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006493 if (s == NULL)
6494 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006495 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006496 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006497 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006498}
6499
6500/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006501 * Garbage collection for lists and dictionaries.
6502 *
6503 * We use reference counts to be able to free most items right away when they
6504 * are no longer used. But for composite items it's possible that it becomes
6505 * unused while the reference count is > 0: When there is a recursive
6506 * reference. Example:
6507 * :let l = [1, 2, 3]
6508 * :let d = {9: l}
6509 * :let l[1] = d
6510 *
6511 * Since this is quite unusual we handle this with garbage collection: every
6512 * once in a while find out which lists and dicts are not referenced from any
6513 * variable.
6514 *
6515 * Here is a good reference text about garbage collection (refers to Python
6516 * but it applies to all reference-counting mechanisms):
6517 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006518 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006519
6520/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006521 * Do garbage collection for lists and dicts.
6522 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006523 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006524 int
6525garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006526{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006527 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006528 buf_T *buf;
6529 win_T *wp;
6530 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006531 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006532 int did_free;
6533 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006534#ifdef FEAT_WINDOWS
6535 tabpage_T *tp;
6536#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006537
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006538 /* Only do this once. */
6539 want_garbage_collect = FALSE;
6540 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006541 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006542
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006543 /* We advance by two because we add one for items referenced through
6544 * previous_funccal. */
6545 current_copyID += COPYID_INC;
6546 copyID = current_copyID;
6547
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006548 /*
6549 * 1. Go through all accessible variables and mark all lists and dicts
6550 * with copyID.
6551 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006552
6553 /* Don't free variables in the previous_funccal list unless they are only
6554 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006555 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006556 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6557 {
6558 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6559 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6560 }
6561
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006562 /* script-local variables */
6563 for (i = 1; i <= ga_scripts.ga_len; ++i)
6564 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6565
6566 /* buffer-local variables */
6567 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6568 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6569
6570 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006571 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006572 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6573
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006574#ifdef FEAT_WINDOWS
6575 /* tabpage-local variables */
6576 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6577 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6578#endif
6579
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006580 /* global variables */
6581 set_ref_in_ht(&globvarht, copyID);
6582
6583 /* function-local variables */
6584 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6585 {
6586 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6587 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6588 }
6589
Bram Moolenaard812df62008-11-09 12:46:09 +00006590 /* v: vars */
6591 set_ref_in_ht(&vimvarht, copyID);
6592
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006593 /*
6594 * 2. Free lists and dictionaries that are not referenced.
6595 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006596 did_free = free_unref_items(copyID);
6597
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006598 /*
6599 * 3. Check if any funccal can be freed now.
6600 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006601 for (pfc = &previous_funccal; *pfc != NULL; )
6602 {
6603 if (can_free_funccal(*pfc, copyID))
6604 {
6605 fc = *pfc;
6606 *pfc = fc->caller;
6607 free_funccal(fc, TRUE);
6608 did_free = TRUE;
6609 did_free_funccal = TRUE;
6610 }
6611 else
6612 pfc = &(*pfc)->caller;
6613 }
6614 if (did_free_funccal)
6615 /* When a funccal was freed some more items might be garbage
6616 * collected, so run again. */
6617 (void)garbage_collect();
6618
6619 return did_free;
6620}
6621
6622/*
6623 * Free lists and dictionaries that are no longer referenced.
6624 */
6625 static int
6626free_unref_items(copyID)
6627 int copyID;
6628{
6629 dict_T *dd;
6630 list_T *ll;
6631 int did_free = FALSE;
6632
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006633 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006634 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006635 */
6636 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006637 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006638 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006639 /* Free the Dictionary and ordinary items it contains, but don't
6640 * recurse into Lists and Dictionaries, they will be in the list
6641 * of dicts or list of lists. */
6642 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006643 did_free = TRUE;
6644
6645 /* restart, next dict may also have been freed */
6646 dd = first_dict;
6647 }
6648 else
6649 dd = dd->dv_used_next;
6650
6651 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006652 * Go through the list of lists and free items without the copyID.
6653 * But don't free a list that has a watcher (used in a for loop), these
6654 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006655 */
6656 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006657 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6658 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006659 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006660 /* Free the List and ordinary items it contains, but don't recurse
6661 * into Lists and Dictionaries, they will be in the list of dicts
6662 * or list of lists. */
6663 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006664 did_free = TRUE;
6665
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006666 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006667 ll = first_list;
6668 }
6669 else
6670 ll = ll->lv_used_next;
6671
6672 return did_free;
6673}
6674
6675/*
6676 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6677 */
6678 static void
6679set_ref_in_ht(ht, copyID)
6680 hashtab_T *ht;
6681 int copyID;
6682{
6683 int todo;
6684 hashitem_T *hi;
6685
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006686 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006687 for (hi = ht->ht_array; todo > 0; ++hi)
6688 if (!HASHITEM_EMPTY(hi))
6689 {
6690 --todo;
6691 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6692 }
6693}
6694
6695/*
6696 * Mark all lists and dicts referenced through list "l" with "copyID".
6697 */
6698 static void
6699set_ref_in_list(l, copyID)
6700 list_T *l;
6701 int copyID;
6702{
6703 listitem_T *li;
6704
6705 for (li = l->lv_first; li != NULL; li = li->li_next)
6706 set_ref_in_item(&li->li_tv, copyID);
6707}
6708
6709/*
6710 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6711 */
6712 static void
6713set_ref_in_item(tv, copyID)
6714 typval_T *tv;
6715 int copyID;
6716{
6717 dict_T *dd;
6718 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006719
6720 switch (tv->v_type)
6721 {
6722 case VAR_DICT:
6723 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006724 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006725 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006726 /* Didn't see this dict yet. */
6727 dd->dv_copyID = copyID;
6728 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006729 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006730 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006731
6732 case VAR_LIST:
6733 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006734 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006735 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006736 /* Didn't see this list yet. */
6737 ll->lv_copyID = copyID;
6738 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006739 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006740 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006741 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006742 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006743}
6744
6745/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006746 * Allocate an empty header for a dictionary.
6747 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006748 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006749dict_alloc()
6750{
Bram Moolenaar33570922005-01-25 22:26:29 +00006751 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006752
Bram Moolenaar33570922005-01-25 22:26:29 +00006753 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006754 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006755 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006756 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006757 if (first_dict != NULL)
6758 first_dict->dv_used_prev = d;
6759 d->dv_used_next = first_dict;
6760 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006761 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006762
Bram Moolenaar33570922005-01-25 22:26:29 +00006763 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006764 d->dv_lock = 0;
6765 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006766 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006767 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006768 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006769}
6770
6771/*
6772 * Unreference a Dictionary: decrement the reference count and free it when it
6773 * becomes zero.
6774 */
6775 static void
6776dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006777 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006778{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006779 if (d != NULL && --d->dv_refcount <= 0)
6780 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006781}
6782
6783/*
6784 * Free a Dictionary, including all items it contains.
6785 * Ignores the reference count.
6786 */
6787 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006788dict_free(d, recurse)
6789 dict_T *d;
6790 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006791{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006792 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006793 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006794 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006795
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006796 /* Remove the dict from the list of dicts for garbage collection. */
6797 if (d->dv_used_prev == NULL)
6798 first_dict = d->dv_used_next;
6799 else
6800 d->dv_used_prev->dv_used_next = d->dv_used_next;
6801 if (d->dv_used_next != NULL)
6802 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6803
6804 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006805 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006806 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006807 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006808 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006809 if (!HASHITEM_EMPTY(hi))
6810 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006811 /* Remove the item before deleting it, just in case there is
6812 * something recursive causing trouble. */
6813 di = HI2DI(hi);
6814 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006815 if (recurse || (di->di_tv.v_type != VAR_LIST
6816 && di->di_tv.v_type != VAR_DICT))
6817 clear_tv(&di->di_tv);
6818 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006819 --todo;
6820 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006821 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006822 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006823 vim_free(d);
6824}
6825
6826/*
6827 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006828 * The "key" is copied to the new item.
6829 * Note that the value of the item "di_tv" still needs to be initialized!
6830 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006831 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006832 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006833dictitem_alloc(key)
6834 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006835{
Bram Moolenaar33570922005-01-25 22:26:29 +00006836 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006837
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006838 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006839 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006840 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006841 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006842 di->di_flags = 0;
6843 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006844 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006845}
6846
6847/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006848 * Make a copy of a Dictionary item.
6849 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006850 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006851dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006852 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006853{
Bram Moolenaar33570922005-01-25 22:26:29 +00006854 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006855
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006856 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6857 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006858 if (di != NULL)
6859 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006860 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006861 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006862 copy_tv(&org->di_tv, &di->di_tv);
6863 }
6864 return di;
6865}
6866
6867/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006868 * Remove item "item" from Dictionary "dict" and free it.
6869 */
6870 static void
6871dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006872 dict_T *dict;
6873 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006874{
Bram Moolenaar33570922005-01-25 22:26:29 +00006875 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006876
Bram Moolenaar33570922005-01-25 22:26:29 +00006877 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006878 if (HASHITEM_EMPTY(hi))
6879 EMSG2(_(e_intern2), "dictitem_remove()");
6880 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006881 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006882 dictitem_free(item);
6883}
6884
6885/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006886 * Free a dict item. Also clears the value.
6887 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006888 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006889dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006890 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006891{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006892 clear_tv(&item->di_tv);
6893 vim_free(item);
6894}
6895
6896/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006897 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6898 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006899 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006900 * Returns NULL when out of memory.
6901 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006902 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006903dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006904 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006905 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006906 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006907{
Bram Moolenaar33570922005-01-25 22:26:29 +00006908 dict_T *copy;
6909 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006910 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006911 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006912
6913 if (orig == NULL)
6914 return NULL;
6915
6916 copy = dict_alloc();
6917 if (copy != NULL)
6918 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006919 if (copyID != 0)
6920 {
6921 orig->dv_copyID = copyID;
6922 orig->dv_copydict = copy;
6923 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006924 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006925 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006926 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006927 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006928 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006929 --todo;
6930
6931 di = dictitem_alloc(hi->hi_key);
6932 if (di == NULL)
6933 break;
6934 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006935 {
6936 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6937 copyID) == FAIL)
6938 {
6939 vim_free(di);
6940 break;
6941 }
6942 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006943 else
6944 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6945 if (dict_add(copy, di) == FAIL)
6946 {
6947 dictitem_free(di);
6948 break;
6949 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006950 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006951 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006952
Bram Moolenaare9a41262005-01-15 22:18:47 +00006953 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006954 if (todo > 0)
6955 {
6956 dict_unref(copy);
6957 copy = NULL;
6958 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006959 }
6960
6961 return copy;
6962}
6963
6964/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006965 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006966 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006967 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006968 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006969dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006970 dict_T *d;
6971 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006972{
Bram Moolenaar33570922005-01-25 22:26:29 +00006973 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006974}
6975
Bram Moolenaar8c711452005-01-14 21:53:12 +00006976/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006977 * Add a number or string entry to dictionary "d".
6978 * When "str" is NULL use number "nr", otherwise use "str".
6979 * Returns FAIL when out of memory and when key already exists.
6980 */
6981 int
6982dict_add_nr_str(d, key, nr, str)
6983 dict_T *d;
6984 char *key;
6985 long nr;
6986 char_u *str;
6987{
6988 dictitem_T *item;
6989
6990 item = dictitem_alloc((char_u *)key);
6991 if (item == NULL)
6992 return FAIL;
6993 item->di_tv.v_lock = 0;
6994 if (str == NULL)
6995 {
6996 item->di_tv.v_type = VAR_NUMBER;
6997 item->di_tv.vval.v_number = nr;
6998 }
6999 else
7000 {
7001 item->di_tv.v_type = VAR_STRING;
7002 item->di_tv.vval.v_string = vim_strsave(str);
7003 }
7004 if (dict_add(d, item) == FAIL)
7005 {
7006 dictitem_free(item);
7007 return FAIL;
7008 }
7009 return OK;
7010}
7011
7012/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007013 * Get the number of items in a Dictionary.
7014 */
7015 static long
7016dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007017 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007018{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007019 if (d == NULL)
7020 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007021 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007022}
7023
7024/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007025 * Find item "key[len]" in Dictionary "d".
7026 * If "len" is negative use strlen(key).
7027 * Returns NULL when not found.
7028 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007029 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007030dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007031 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007032 char_u *key;
7033 int len;
7034{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007035#define AKEYLEN 200
7036 char_u buf[AKEYLEN];
7037 char_u *akey;
7038 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007039 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007040
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007041 if (len < 0)
7042 akey = key;
7043 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007044 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007045 tofree = akey = vim_strnsave(key, len);
7046 if (akey == NULL)
7047 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007048 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007049 else
7050 {
7051 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007052 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007053 akey = buf;
7054 }
7055
Bram Moolenaar33570922005-01-25 22:26:29 +00007056 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007057 vim_free(tofree);
7058 if (HASHITEM_EMPTY(hi))
7059 return NULL;
7060 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007061}
7062
7063/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007064 * Get a string item from a dictionary.
7065 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007066 * Returns NULL if the entry doesn't exist or out of memory.
7067 */
7068 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007069get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007070 dict_T *d;
7071 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007072 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007073{
7074 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007075 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007076
7077 di = dict_find(d, key, -1);
7078 if (di == NULL)
7079 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007080 s = get_tv_string(&di->di_tv);
7081 if (save && s != NULL)
7082 s = vim_strsave(s);
7083 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007084}
7085
7086/*
7087 * Get a number item from a dictionary.
7088 * Returns 0 if the entry doesn't exist or out of memory.
7089 */
7090 long
7091get_dict_number(d, key)
7092 dict_T *d;
7093 char_u *key;
7094{
7095 dictitem_T *di;
7096
7097 di = dict_find(d, key, -1);
7098 if (di == NULL)
7099 return 0;
7100 return get_tv_number(&di->di_tv);
7101}
7102
7103/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104 * Return an allocated string with the string representation of a Dictionary.
7105 * May return NULL.
7106 */
7107 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007108dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007110 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007111{
7112 garray_T ga;
7113 int first = TRUE;
7114 char_u *tofree;
7115 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007116 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007117 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007118 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007119 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007120
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007121 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007122 return NULL;
7123 ga_init2(&ga, (int)sizeof(char), 80);
7124 ga_append(&ga, '{');
7125
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007126 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007127 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007128 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007129 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007130 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131 --todo;
7132
7133 if (first)
7134 first = FALSE;
7135 else
7136 ga_concat(&ga, (char_u *)", ");
7137
7138 tofree = string_quote(hi->hi_key, FALSE);
7139 if (tofree != NULL)
7140 {
7141 ga_concat(&ga, tofree);
7142 vim_free(tofree);
7143 }
7144 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007145 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007146 if (s != NULL)
7147 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007148 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007149 if (s == NULL)
7150 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007151 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007152 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007153 if (todo > 0)
7154 {
7155 vim_free(ga.ga_data);
7156 return NULL;
7157 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007158
7159 ga_append(&ga, '}');
7160 ga_append(&ga, NUL);
7161 return (char_u *)ga.ga_data;
7162}
7163
7164/*
7165 * Allocate a variable for a Dictionary and fill it from "*arg".
7166 * Return OK or FAIL. Returns NOTDONE for {expr}.
7167 */
7168 static int
7169get_dict_tv(arg, rettv, evaluate)
7170 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007171 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172 int evaluate;
7173{
Bram Moolenaar33570922005-01-25 22:26:29 +00007174 dict_T *d = NULL;
7175 typval_T tvkey;
7176 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007177 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007178 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007179 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007180 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007181
7182 /*
7183 * First check if it's not a curly-braces thing: {expr}.
7184 * Must do this without evaluating, otherwise a function may be called
7185 * twice. Unfortunately this means we need to call eval1() twice for the
7186 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007187 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007188 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007189 if (*start != '}')
7190 {
7191 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7192 return FAIL;
7193 if (*start == '}')
7194 return NOTDONE;
7195 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007196
7197 if (evaluate)
7198 {
7199 d = dict_alloc();
7200 if (d == NULL)
7201 return FAIL;
7202 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007203 tvkey.v_type = VAR_UNKNOWN;
7204 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007205
7206 *arg = skipwhite(*arg + 1);
7207 while (**arg != '}' && **arg != NUL)
7208 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007209 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210 goto failret;
7211 if (**arg != ':')
7212 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007213 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007214 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215 goto failret;
7216 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007217 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007219 key = get_tv_string_buf_chk(&tvkey, buf);
7220 if (key == NULL || *key == NUL)
7221 {
7222 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7223 if (key != NULL)
7224 EMSG(_(e_emptykey));
7225 clear_tv(&tvkey);
7226 goto failret;
7227 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229
7230 *arg = skipwhite(*arg + 1);
7231 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7232 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007233 if (evaluate)
7234 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235 goto failret;
7236 }
7237 if (evaluate)
7238 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007239 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240 if (item != NULL)
7241 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007242 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007243 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244 clear_tv(&tv);
7245 goto failret;
7246 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007247 item = dictitem_alloc(key);
7248 clear_tv(&tvkey);
7249 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007252 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007253 if (dict_add(d, item) == FAIL)
7254 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255 }
7256 }
7257
7258 if (**arg == '}')
7259 break;
7260 if (**arg != ',')
7261 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007262 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263 goto failret;
7264 }
7265 *arg = skipwhite(*arg + 1);
7266 }
7267
7268 if (**arg != '}')
7269 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007270 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271failret:
7272 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007273 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274 return FAIL;
7275 }
7276
7277 *arg = skipwhite(*arg + 1);
7278 if (evaluate)
7279 {
7280 rettv->v_type = VAR_DICT;
7281 rettv->vval.v_dict = d;
7282 ++d->dv_refcount;
7283 }
7284
7285 return OK;
7286}
7287
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007289 * Return a string with the string representation of a variable.
7290 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007291 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007292 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007293 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007294 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007295 */
7296 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007297echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007299 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007300 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007301 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007302{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007303 static int recurse = 0;
7304 char_u *r = NULL;
7305
Bram Moolenaar33570922005-01-25 22:26:29 +00007306 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007307 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007308 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007309 *tofree = NULL;
7310 return NULL;
7311 }
7312 ++recurse;
7313
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007314 switch (tv->v_type)
7315 {
7316 case VAR_FUNC:
7317 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318 r = tv->vval.v_string;
7319 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007320
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007321 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007322 if (tv->vval.v_list == NULL)
7323 {
7324 *tofree = NULL;
7325 r = NULL;
7326 }
7327 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7328 {
7329 *tofree = NULL;
7330 r = (char_u *)"[...]";
7331 }
7332 else
7333 {
7334 tv->vval.v_list->lv_copyID = copyID;
7335 *tofree = list2string(tv, copyID);
7336 r = *tofree;
7337 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007338 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007339
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007341 if (tv->vval.v_dict == NULL)
7342 {
7343 *tofree = NULL;
7344 r = NULL;
7345 }
7346 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7347 {
7348 *tofree = NULL;
7349 r = (char_u *)"{...}";
7350 }
7351 else
7352 {
7353 tv->vval.v_dict->dv_copyID = copyID;
7354 *tofree = dict2string(tv, copyID);
7355 r = *tofree;
7356 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007357 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007358
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007359 case VAR_STRING:
7360 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007361 *tofree = NULL;
7362 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007363 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007364
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007365#ifdef FEAT_FLOAT
7366 case VAR_FLOAT:
7367 *tofree = NULL;
7368 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7369 r = numbuf;
7370 break;
7371#endif
7372
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007373 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007374 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007375 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007376 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007377
7378 --recurse;
7379 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007380}
7381
7382/*
7383 * Return a string with the string representation of a variable.
7384 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7385 * "numbuf" is used for a number.
7386 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007387 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007388 */
7389 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007390tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007391 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007392 char_u **tofree;
7393 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007394 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007395{
7396 switch (tv->v_type)
7397 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007398 case VAR_FUNC:
7399 *tofree = string_quote(tv->vval.v_string, TRUE);
7400 return *tofree;
7401 case VAR_STRING:
7402 *tofree = string_quote(tv->vval.v_string, FALSE);
7403 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007404#ifdef FEAT_FLOAT
7405 case VAR_FLOAT:
7406 *tofree = NULL;
7407 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7408 return numbuf;
7409#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007410 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007411 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007413 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007414 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007415 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007416 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007417 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007418}
7419
7420/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007421 * Return string "str" in ' quotes, doubling ' characters.
7422 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007423 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007424 */
7425 static char_u *
7426string_quote(str, function)
7427 char_u *str;
7428 int function;
7429{
Bram Moolenaar33570922005-01-25 22:26:29 +00007430 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007431 char_u *p, *r, *s;
7432
Bram Moolenaar33570922005-01-25 22:26:29 +00007433 len = (function ? 13 : 3);
7434 if (str != NULL)
7435 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007436 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007437 for (p = str; *p != NUL; mb_ptr_adv(p))
7438 if (*p == '\'')
7439 ++len;
7440 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007441 s = r = alloc(len);
7442 if (r != NULL)
7443 {
7444 if (function)
7445 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007446 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007447 r += 10;
7448 }
7449 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007450 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007451 if (str != NULL)
7452 for (p = str; *p != NUL; )
7453 {
7454 if (*p == '\'')
7455 *r++ = '\'';
7456 MB_COPY_CHAR(p, r);
7457 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007459 if (function)
7460 *r++ = ')';
7461 *r++ = NUL;
7462 }
7463 return s;
7464}
7465
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007466#ifdef FEAT_FLOAT
7467/*
7468 * Convert the string "text" to a floating point number.
7469 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7470 * this always uses a decimal point.
7471 * Returns the length of the text that was consumed.
7472 */
7473 static int
7474string2float(text, value)
7475 char_u *text;
7476 float_T *value; /* result stored here */
7477{
7478 char *s = (char *)text;
7479 float_T f;
7480
7481 f = strtod(s, &s);
7482 *value = f;
7483 return (int)((char_u *)s - text);
7484}
7485#endif
7486
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 * Get the value of an environment variable.
7489 * "arg" is pointing to the '$'. It is advanced to after the name.
7490 * If the environment variable was not set, silently assume it is empty.
7491 * Always return OK.
7492 */
7493 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007494get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 int evaluate;
7498{
7499 char_u *string = NULL;
7500 int len;
7501 int cc;
7502 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007503 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504
7505 ++*arg;
7506 name = *arg;
7507 len = get_env_len(arg);
7508 if (evaluate)
7509 {
7510 if (len != 0)
7511 {
7512 cc = name[len];
7513 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007514 /* first try vim_getenv(), fast for normal environment vars */
7515 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007517 {
7518 if (!mustfree)
7519 string = vim_strsave(string);
7520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 else
7522 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007523 if (mustfree)
7524 vim_free(string);
7525
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 /* next try expanding things like $VIM and ${HOME} */
7527 string = expand_env_save(name - 1);
7528 if (string != NULL && *string == '$')
7529 {
7530 vim_free(string);
7531 string = NULL;
7532 }
7533 }
7534 name[len] = cc;
7535 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007536 rettv->v_type = VAR_STRING;
7537 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 }
7539
7540 return OK;
7541}
7542
7543/*
7544 * Array with names and number of arguments of all internal functions
7545 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7546 */
7547static struct fst
7548{
7549 char *f_name; /* function name */
7550 char f_min_argc; /* minimal number of arguments */
7551 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007552 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007553 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554} functions[] =
7555{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007556#ifdef FEAT_FLOAT
7557 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007558 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007559#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007560 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 {"append", 2, 2, f_append},
7562 {"argc", 0, 0, f_argc},
7563 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007564 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007565#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007566 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007567 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007568 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007571 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 {"bufexists", 1, 1, f_bufexists},
7573 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7574 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7575 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7576 {"buflisted", 1, 1, f_buflisted},
7577 {"bufloaded", 1, 1, f_bufloaded},
7578 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007579 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 {"bufwinnr", 1, 1, f_bufwinnr},
7581 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007582 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007583 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007584#ifdef FEAT_FLOAT
7585 {"ceil", 1, 1, f_ceil},
7586#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007587 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 {"char2nr", 1, 1, f_char2nr},
7589 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007590 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007592#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007593 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007594 {"complete_add", 1, 1, f_complete_add},
7595 {"complete_check", 0, 0, f_complete_check},
7596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007598 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007599#ifdef FEAT_FLOAT
7600 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007601 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007602#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007603 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007605 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007606 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 {"delete", 1, 1, f_delete},
7608 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007609 {"diff_filler", 1, 1, f_diff_filler},
7610 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007611 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007613 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 {"eventhandler", 0, 0, f_eventhandler},
7615 {"executable", 1, 1, f_executable},
7616 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007617#ifdef FEAT_FLOAT
7618 {"exp", 1, 1, f_exp},
7619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007621 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007622 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7624 {"filereadable", 1, 1, f_filereadable},
7625 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007626 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007627 {"finddir", 1, 3, f_finddir},
7628 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007629#ifdef FEAT_FLOAT
7630 {"float2nr", 1, 1, f_float2nr},
7631 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007632 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007633#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007634 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 {"fnamemodify", 2, 2, f_fnamemodify},
7636 {"foldclosed", 1, 1, f_foldclosed},
7637 {"foldclosedend", 1, 1, f_foldclosedend},
7638 {"foldlevel", 1, 1, f_foldlevel},
7639 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007640 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007642 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007643 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007644 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007645 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 {"getbufvar", 2, 2, f_getbufvar},
7647 {"getchar", 0, 1, f_getchar},
7648 {"getcharmod", 0, 0, f_getcharmod},
7649 {"getcmdline", 0, 0, f_getcmdline},
7650 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007651 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007653 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007654 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {"getfsize", 1, 1, f_getfsize},
7656 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007657 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007658 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007659 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007660 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007661 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007662 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007663 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007664 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007666 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 {"getwinposx", 0, 0, f_getwinposx},
7668 {"getwinposy", 0, 0, f_getwinposy},
7669 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007670 {"glob", 1, 2, f_glob},
7671 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007673 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007674 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007675 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7677 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7678 {"histadd", 2, 2, f_histadd},
7679 {"histdel", 1, 2, f_histdel},
7680 {"histget", 1, 2, f_histget},
7681 {"histnr", 1, 1, f_histnr},
7682 {"hlID", 1, 1, f_hlID},
7683 {"hlexists", 1, 1, f_hlexists},
7684 {"hostname", 0, 0, f_hostname},
7685 {"iconv", 3, 3, f_iconv},
7686 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007687 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007688 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007690 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 {"inputrestore", 0, 0, f_inputrestore},
7692 {"inputsave", 0, 0, f_inputsave},
7693 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007694 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007696 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007698 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007701 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 {"libcall", 3, 3, f_libcall},
7703 {"libcallnr", 3, 3, f_libcallnr},
7704 {"line", 1, 1, f_line},
7705 {"line2byte", 1, 1, f_line2byte},
7706 {"lispindent", 1, 1, f_lispindent},
7707 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007708#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007709 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007710 {"log10", 1, 1, f_log10},
7711#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007712 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007713 {"maparg", 1, 3, f_maparg},
7714 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007715 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007716 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007717 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007718 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007719 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007720 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007721 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007722 {"max", 1, 1, f_max},
7723 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007724#ifdef vim_mkdir
7725 {"mkdir", 1, 3, f_mkdir},
7726#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007727 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007728#ifdef FEAT_MZSCHEME
7729 {"mzeval", 1, 1, f_mzeval},
7730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 {"nextnonblank", 1, 1, f_nextnonblank},
7732 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007733 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007734#ifdef FEAT_FLOAT
7735 {"pow", 2, 2, f_pow},
7736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007738 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007739 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007741 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007742 {"reltime", 0, 2, f_reltime},
7743 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 {"remote_expr", 2, 3, f_remote_expr},
7745 {"remote_foreground", 1, 1, f_remote_foreground},
7746 {"remote_peek", 1, 2, f_remote_peek},
7747 {"remote_read", 1, 1, f_remote_read},
7748 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007749 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007751 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007753 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007754#ifdef FEAT_FLOAT
7755 {"round", 1, 1, f_round},
7756#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007757 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007758 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007759 {"searchpair", 3, 7, f_searchpair},
7760 {"searchpairpos", 3, 7, f_searchpairpos},
7761 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {"server2client", 2, 2, f_server2client},
7763 {"serverlist", 0, 0, f_serverlist},
7764 {"setbufvar", 3, 3, f_setbufvar},
7765 {"setcmdpos", 1, 1, f_setcmdpos},
7766 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007767 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007768 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007769 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007770 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007772 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007774 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007776#ifdef FEAT_FLOAT
7777 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007778 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007779#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007780 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007781 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007782 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007783 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007784 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007785#ifdef FEAT_FLOAT
7786 {"sqrt", 1, 1, f_sqrt},
7787 {"str2float", 1, 1, f_str2float},
7788#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007789 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790#ifdef HAVE_STRFTIME
7791 {"strftime", 1, 2, f_strftime},
7792#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007793 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007794 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 {"strlen", 1, 1, f_strlen},
7796 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007797 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 {"strtrans", 1, 1, f_strtrans},
7799 {"submatch", 1, 1, f_submatch},
7800 {"substitute", 4, 4, f_substitute},
7801 {"synID", 3, 3, f_synID},
7802 {"synIDattr", 2, 3, f_synIDattr},
7803 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007804 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007805 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007806 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007807 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007808 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007809 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007810 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007811#ifdef FEAT_FLOAT
7812 {"tan", 1, 1, f_tan},
7813 {"tanh", 1, 1, f_tanh},
7814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007816 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 {"tolower", 1, 1, f_tolower},
7818 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007819 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007820#ifdef FEAT_FLOAT
7821 {"trunc", 1, 1, f_trunc},
7822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007824 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 {"virtcol", 1, 1, f_virtcol},
7826 {"visualmode", 0, 1, f_visualmode},
7827 {"winbufnr", 1, 1, f_winbufnr},
7828 {"wincol", 0, 0, f_wincol},
7829 {"winheight", 1, 1, f_winheight},
7830 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007831 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007833 {"winrestview", 1, 1, f_winrestview},
7834 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007836 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837};
7838
7839#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7840
7841/*
7842 * Function given to ExpandGeneric() to obtain the list of internal
7843 * or user defined function names.
7844 */
7845 char_u *
7846get_function_name(xp, idx)
7847 expand_T *xp;
7848 int idx;
7849{
7850 static int intidx = -1;
7851 char_u *name;
7852
7853 if (idx == 0)
7854 intidx = -1;
7855 if (intidx < 0)
7856 {
7857 name = get_user_func_name(xp, idx);
7858 if (name != NULL)
7859 return name;
7860 }
7861 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7862 {
7863 STRCPY(IObuff, functions[intidx].f_name);
7864 STRCAT(IObuff, "(");
7865 if (functions[intidx].f_max_argc == 0)
7866 STRCAT(IObuff, ")");
7867 return IObuff;
7868 }
7869
7870 return NULL;
7871}
7872
7873/*
7874 * Function given to ExpandGeneric() to obtain the list of internal or
7875 * user defined variable or function names.
7876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 char_u *
7878get_expr_name(xp, idx)
7879 expand_T *xp;
7880 int idx;
7881{
7882 static int intidx = -1;
7883 char_u *name;
7884
7885 if (idx == 0)
7886 intidx = -1;
7887 if (intidx < 0)
7888 {
7889 name = get_function_name(xp, idx);
7890 if (name != NULL)
7891 return name;
7892 }
7893 return get_user_var_name(xp, ++intidx);
7894}
7895
7896#endif /* FEAT_CMDL_COMPL */
7897
7898/*
7899 * Find internal function in table above.
7900 * Return index, or -1 if not found
7901 */
7902 static int
7903find_internal_func(name)
7904 char_u *name; /* name of the function */
7905{
7906 int first = 0;
7907 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7908 int cmp;
7909 int x;
7910
7911 /*
7912 * Find the function name in the table. Binary search.
7913 */
7914 while (first <= last)
7915 {
7916 x = first + ((unsigned)(last - first) >> 1);
7917 cmp = STRCMP(name, functions[x].f_name);
7918 if (cmp < 0)
7919 last = x - 1;
7920 else if (cmp > 0)
7921 first = x + 1;
7922 else
7923 return x;
7924 }
7925 return -1;
7926}
7927
7928/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007929 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7930 * name it contains, otherwise return "name".
7931 */
7932 static char_u *
7933deref_func_name(name, lenp)
7934 char_u *name;
7935 int *lenp;
7936{
Bram Moolenaar33570922005-01-25 22:26:29 +00007937 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007938 int cc;
7939
7940 cc = name[*lenp];
7941 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007942 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007943 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007944 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007945 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007946 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007947 {
7948 *lenp = 0;
7949 return (char_u *)""; /* just in case */
7950 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007951 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007953 }
7954
7955 return name;
7956}
7957
7958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 * Allocate a variable for the result of a function.
7960 * Return OK or FAIL.
7961 */
7962 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007963get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7964 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 char_u *name; /* name of the function */
7966 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 char_u **arg; /* argument, pointing to the '(' */
7969 linenr_T firstline; /* first line of range */
7970 linenr_T lastline; /* last line of range */
7971 int *doesrange; /* return: function handled range */
7972 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007973 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974{
7975 char_u *argp;
7976 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007977 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 int argcount = 0; /* number of arguments found */
7979
7980 /*
7981 * Get the arguments.
7982 */
7983 argp = *arg;
7984 while (argcount < MAX_FUNC_ARGS)
7985 {
7986 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7987 if (*argp == ')' || *argp == ',' || *argp == NUL)
7988 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7990 {
7991 ret = FAIL;
7992 break;
7993 }
7994 ++argcount;
7995 if (*argp != ',')
7996 break;
7997 }
7998 if (*argp == ')')
7999 ++argp;
8000 else
8001 ret = FAIL;
8002
8003 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008004 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008005 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008007 {
8008 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008009 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008010 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008011 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013
8014 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008015 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016
8017 *arg = skipwhite(argp);
8018 return ret;
8019}
8020
8021
8022/*
8023 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008024 * Return OK when the function can't be called, FAIL otherwise.
8025 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 */
8027 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008028call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008029 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 char_u *name; /* name of the function */
8031 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008032 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008034 typval_T *argvars; /* vars for arguments, must have "argcount"
8035 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 linenr_T firstline; /* first line of range */
8037 linenr_T lastline; /* last line of range */
8038 int *doesrange; /* return: function handled range */
8039 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008040 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041{
8042 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043#define ERROR_UNKNOWN 0
8044#define ERROR_TOOMANY 1
8045#define ERROR_TOOFEW 2
8046#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008047#define ERROR_DICT 4
8048#define ERROR_NONE 5
8049#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 int error = ERROR_NONE;
8051 int i;
8052 int llen;
8053 ufunc_T *fp;
8054 int cc;
8055#define FLEN_FIXED 40
8056 char_u fname_buf[FLEN_FIXED + 1];
8057 char_u *fname;
8058
8059 /*
8060 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8061 * Change <SNR>123_name() to K_SNR 123_name().
8062 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8063 */
8064 cc = name[len];
8065 name[len] = NUL;
8066 llen = eval_fname_script(name);
8067 if (llen > 0)
8068 {
8069 fname_buf[0] = K_SPECIAL;
8070 fname_buf[1] = KS_EXTRA;
8071 fname_buf[2] = (int)KE_SNR;
8072 i = 3;
8073 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8074 {
8075 if (current_SID <= 0)
8076 error = ERROR_SCRIPT;
8077 else
8078 {
8079 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8080 i = (int)STRLEN(fname_buf);
8081 }
8082 }
8083 if (i + STRLEN(name + llen) < FLEN_FIXED)
8084 {
8085 STRCPY(fname_buf + i, name + llen);
8086 fname = fname_buf;
8087 }
8088 else
8089 {
8090 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8091 if (fname == NULL)
8092 error = ERROR_OTHER;
8093 else
8094 {
8095 mch_memmove(fname, fname_buf, (size_t)i);
8096 STRCPY(fname + i, name + llen);
8097 }
8098 }
8099 }
8100 else
8101 fname = name;
8102
8103 *doesrange = FALSE;
8104
8105
8106 /* execute the function if no errors detected and executing */
8107 if (evaluate && error == ERROR_NONE)
8108 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008109 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8110 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 error = ERROR_UNKNOWN;
8112
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008113 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {
8115 /*
8116 * User defined function.
8117 */
8118 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008119
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008121 /* Trigger FuncUndefined event, may load the function. */
8122 if (fp == NULL
8123 && apply_autocmds(EVENT_FUNCUNDEFINED,
8124 fname, fname, TRUE, NULL)
8125 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008127 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 fp = find_func(fname);
8129 }
8130#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008131 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008132 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008133 {
8134 /* loaded a package, search for the function again */
8135 fp = find_func(fname);
8136 }
8137
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 if (fp != NULL)
8139 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008140 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008142 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008144 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008146 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008147 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 else
8149 {
8150 /*
8151 * Call the user function.
8152 * Save and restore search patterns, script variables and
8153 * redo buffer.
8154 */
8155 save_search_patterns();
8156 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008157 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008158 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008159 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008160 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8161 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8162 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008163 /* Function was unreferenced while being used, free it
8164 * now. */
8165 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 restoreRedobuff();
8167 restore_search_patterns();
8168 error = ERROR_NONE;
8169 }
8170 }
8171 }
8172 else
8173 {
8174 /*
8175 * Find the function name in the table, call its implementation.
8176 */
8177 i = find_internal_func(fname);
8178 if (i >= 0)
8179 {
8180 if (argcount < functions[i].f_min_argc)
8181 error = ERROR_TOOFEW;
8182 else if (argcount > functions[i].f_max_argc)
8183 error = ERROR_TOOMANY;
8184 else
8185 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008186 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008187 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 error = ERROR_NONE;
8189 }
8190 }
8191 }
8192 /*
8193 * The function call (or "FuncUndefined" autocommand sequence) might
8194 * have been aborted by an error, an interrupt, or an explicitly thrown
8195 * exception that has not been caught so far. This situation can be
8196 * tested for by calling aborting(). For an error in an internal
8197 * function or for the "E132" error in call_user_func(), however, the
8198 * throw point at which the "force_abort" flag (temporarily reset by
8199 * emsg()) is normally updated has not been reached yet. We need to
8200 * update that flag first to make aborting() reliable.
8201 */
8202 update_force_abort();
8203 }
8204 if (error == ERROR_NONE)
8205 ret = OK;
8206
8207 /*
8208 * Report an error unless the argument evaluation or function call has been
8209 * cancelled due to an aborting error, an interrupt, or an exception.
8210 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008211 if (!aborting())
8212 {
8213 switch (error)
8214 {
8215 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008216 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008217 break;
8218 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008219 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008220 break;
8221 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008222 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008223 name);
8224 break;
8225 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008226 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008227 name);
8228 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008229 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008230 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008231 name);
8232 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008233 }
8234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235
8236 name[len] = cc;
8237 if (fname != name && fname != fname_buf)
8238 vim_free(fname);
8239
8240 return ret;
8241}
8242
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008243/*
8244 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008245 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008246 */
8247 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008248emsg_funcname(ermsg, name)
8249 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008250 char_u *name;
8251{
8252 char_u *p;
8253
8254 if (*name == K_SPECIAL)
8255 p = concat_str((char_u *)"<SNR>", name + 3);
8256 else
8257 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008258 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008259 if (p != name)
8260 vim_free(p);
8261}
8262
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008263/*
8264 * Return TRUE for a non-zero Number and a non-empty String.
8265 */
8266 static int
8267non_zero_arg(argvars)
8268 typval_T *argvars;
8269{
8270 return ((argvars[0].v_type == VAR_NUMBER
8271 && argvars[0].vval.v_number != 0)
8272 || (argvars[0].v_type == VAR_STRING
8273 && argvars[0].vval.v_string != NULL
8274 && *argvars[0].vval.v_string != NUL));
8275}
8276
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277/*********************************************
8278 * Implementation of the built-in functions
8279 */
8280
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008281#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008282static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8283
8284/*
8285 * Get the float value of "argvars[0]" into "f".
8286 * Returns FAIL when the argument is not a Number or Float.
8287 */
8288 static int
8289get_float_arg(argvars, f)
8290 typval_T *argvars;
8291 float_T *f;
8292{
8293 if (argvars[0].v_type == VAR_FLOAT)
8294 {
8295 *f = argvars[0].vval.v_float;
8296 return OK;
8297 }
8298 if (argvars[0].v_type == VAR_NUMBER)
8299 {
8300 *f = (float_T)argvars[0].vval.v_number;
8301 return OK;
8302 }
8303 EMSG(_("E808: Number or Float required"));
8304 return FAIL;
8305}
8306
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008307/*
8308 * "abs(expr)" function
8309 */
8310 static void
8311f_abs(argvars, rettv)
8312 typval_T *argvars;
8313 typval_T *rettv;
8314{
8315 if (argvars[0].v_type == VAR_FLOAT)
8316 {
8317 rettv->v_type = VAR_FLOAT;
8318 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8319 }
8320 else
8321 {
8322 varnumber_T n;
8323 int error = FALSE;
8324
8325 n = get_tv_number_chk(&argvars[0], &error);
8326 if (error)
8327 rettv->vval.v_number = -1;
8328 else if (n > 0)
8329 rettv->vval.v_number = n;
8330 else
8331 rettv->vval.v_number = -n;
8332 }
8333}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008334
8335/*
8336 * "acos()" function
8337 */
8338 static void
8339f_acos(argvars, rettv)
8340 typval_T *argvars;
8341 typval_T *rettv;
8342{
8343 float_T f;
8344
8345 rettv->v_type = VAR_FLOAT;
8346 if (get_float_arg(argvars, &f) == OK)
8347 rettv->vval.v_float = acos(f);
8348 else
8349 rettv->vval.v_float = 0.0;
8350}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008351#endif
8352
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008354 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 */
8356 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008357f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008358 typval_T *argvars;
8359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360{
Bram Moolenaar33570922005-01-25 22:26:29 +00008361 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008363 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008364 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008366 if ((l = argvars[0].vval.v_list) != NULL
8367 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8368 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008369 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008370 }
8371 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008372 EMSG(_(e_listreq));
8373}
8374
8375/*
8376 * "append(lnum, string/list)" function
8377 */
8378 static void
8379f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008380 typval_T *argvars;
8381 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008382{
8383 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008384 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008385 list_T *l = NULL;
8386 listitem_T *li = NULL;
8387 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008388 long added = 0;
8389
Bram Moolenaar0d660222005-01-07 21:51:51 +00008390 lnum = get_tv_lnum(argvars);
8391 if (lnum >= 0
8392 && lnum <= curbuf->b_ml.ml_line_count
8393 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008394 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008395 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008396 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008397 l = argvars[1].vval.v_list;
8398 if (l == NULL)
8399 return;
8400 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008401 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008402 for (;;)
8403 {
8404 if (l == NULL)
8405 tv = &argvars[1]; /* append a string */
8406 else if (li == NULL)
8407 break; /* end of list */
8408 else
8409 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008410 line = get_tv_string_chk(tv);
8411 if (line == NULL) /* type error */
8412 {
8413 rettv->vval.v_number = 1; /* Failed */
8414 break;
8415 }
8416 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008417 ++added;
8418 if (l == NULL)
8419 break;
8420 li = li->li_next;
8421 }
8422
8423 appended_lines_mark(lnum, added);
8424 if (curwin->w_cursor.lnum > lnum)
8425 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008427 else
8428 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429}
8430
8431/*
8432 * "argc()" function
8433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008435f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008436 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008439 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440}
8441
8442/*
8443 * "argidx()" function
8444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008446f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008447 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008450 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451}
8452
8453/*
8454 * "argv(nr)" function
8455 */
8456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008457f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008458 typval_T *argvars;
8459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460{
8461 int idx;
8462
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008463 if (argvars[0].v_type != VAR_UNKNOWN)
8464 {
8465 idx = get_tv_number_chk(&argvars[0], NULL);
8466 if (idx >= 0 && idx < ARGCOUNT)
8467 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8468 else
8469 rettv->vval.v_string = NULL;
8470 rettv->v_type = VAR_STRING;
8471 }
8472 else if (rettv_list_alloc(rettv) == OK)
8473 for (idx = 0; idx < ARGCOUNT; ++idx)
8474 list_append_string(rettv->vval.v_list,
8475 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476}
8477
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008478#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008479/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008480 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008481 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008482 static void
8483f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008484 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008485 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008486{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008487 float_T f;
8488
8489 rettv->v_type = VAR_FLOAT;
8490 if (get_float_arg(argvars, &f) == OK)
8491 rettv->vval.v_float = asin(f);
8492 else
8493 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008494}
8495
8496/*
8497 * "atan()" function
8498 */
8499 static void
8500f_atan(argvars, rettv)
8501 typval_T *argvars;
8502 typval_T *rettv;
8503{
8504 float_T f;
8505
8506 rettv->v_type = VAR_FLOAT;
8507 if (get_float_arg(argvars, &f) == OK)
8508 rettv->vval.v_float = atan(f);
8509 else
8510 rettv->vval.v_float = 0.0;
8511}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008512
8513/*
8514 * "atan2()" function
8515 */
8516 static void
8517f_atan2(argvars, rettv)
8518 typval_T *argvars;
8519 typval_T *rettv;
8520{
8521 float_T fx, fy;
8522
8523 rettv->v_type = VAR_FLOAT;
8524 if (get_float_arg(argvars, &fx) == OK
8525 && get_float_arg(&argvars[1], &fy) == OK)
8526 rettv->vval.v_float = atan2(fx, fy);
8527 else
8528 rettv->vval.v_float = 0.0;
8529}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008530#endif
8531
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532/*
8533 * "browse(save, title, initdir, default)" function
8534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008536f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008537 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539{
8540#ifdef FEAT_BROWSE
8541 int save;
8542 char_u *title;
8543 char_u *initdir;
8544 char_u *defname;
8545 char_u buf[NUMBUFLEN];
8546 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008547 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008549 save = get_tv_number_chk(&argvars[0], &error);
8550 title = get_tv_string_chk(&argvars[1]);
8551 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8552 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008554 if (error || title == NULL || initdir == NULL || defname == NULL)
8555 rettv->vval.v_string = NULL;
8556 else
8557 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008558 do_browse(save ? BROWSE_SAVE : 0,
8559 title, defname, NULL, initdir, NULL, curbuf);
8560#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008561 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008562#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008563 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008564}
8565
8566/*
8567 * "browsedir(title, initdir)" function
8568 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008570f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008571 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008572 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008573{
8574#ifdef FEAT_BROWSE
8575 char_u *title;
8576 char_u *initdir;
8577 char_u buf[NUMBUFLEN];
8578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008579 title = get_tv_string_chk(&argvars[0]);
8580 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008581
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008582 if (title == NULL || initdir == NULL)
8583 rettv->vval.v_string = NULL;
8584 else
8585 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008586 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591}
8592
Bram Moolenaar33570922005-01-25 22:26:29 +00008593static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008594
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595/*
8596 * Find a buffer by number or exact name.
8597 */
8598 static buf_T *
8599find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008600 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601{
8602 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008604 if (avar->v_type == VAR_NUMBER)
8605 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008606 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008608 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008609 if (buf == NULL)
8610 {
8611 /* No full path name match, try a match with a URL or a "nofile"
8612 * buffer, these don't use the full path. */
8613 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8614 if (buf->b_fname != NULL
8615 && (path_with_url(buf->b_fname)
8616#ifdef FEAT_QUICKFIX
8617 || bt_nofile(buf)
8618#endif
8619 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008620 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008621 break;
8622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 }
8624 return buf;
8625}
8626
8627/*
8628 * "bufexists(expr)" function
8629 */
8630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008631f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008632 typval_T *argvars;
8633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008635 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636}
8637
8638/*
8639 * "buflisted(expr)" function
8640 */
8641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008642f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008643 typval_T *argvars;
8644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645{
8646 buf_T *buf;
8647
8648 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650}
8651
8652/*
8653 * "bufloaded(expr)" function
8654 */
8655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008656f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008657 typval_T *argvars;
8658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659{
8660 buf_T *buf;
8661
8662 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008663 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664}
8665
Bram Moolenaar33570922005-01-25 22:26:29 +00008666static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008667
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668/*
8669 * Get buffer by number or pattern.
8670 */
8671 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008672get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008673 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 int save_magic;
8677 char_u *save_cpo;
8678 buf_T *buf;
8679
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008680 if (tv->v_type == VAR_NUMBER)
8681 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008682 if (tv->v_type != VAR_STRING)
8683 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 if (name == NULL || *name == NUL)
8685 return curbuf;
8686 if (name[0] == '$' && name[1] == NUL)
8687 return lastbuf;
8688
8689 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8690 save_magic = p_magic;
8691 p_magic = TRUE;
8692 save_cpo = p_cpo;
8693 p_cpo = (char_u *)"";
8694
8695 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8696 TRUE, FALSE));
8697
8698 p_magic = save_magic;
8699 p_cpo = save_cpo;
8700
8701 /* If not found, try expanding the name, like done for bufexists(). */
8702 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008703 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704
8705 return buf;
8706}
8707
8708/*
8709 * "bufname(expr)" function
8710 */
8711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008713 typval_T *argvars;
8714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715{
8716 buf_T *buf;
8717
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008718 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008720 buf = get_buf_tv(&argvars[0]);
8721 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008723 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008725 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 --emsg_off;
8727}
8728
8729/*
8730 * "bufnr(expr)" function
8731 */
8732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008733f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008734 typval_T *argvars;
8735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736{
8737 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008738 int error = FALSE;
8739 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008741 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008743 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008744 --emsg_off;
8745
8746 /* If the buffer isn't found and the second argument is not zero create a
8747 * new buffer. */
8748 if (buf == NULL
8749 && argvars[1].v_type != VAR_UNKNOWN
8750 && get_tv_number_chk(&argvars[1], &error) != 0
8751 && !error
8752 && (name = get_tv_string_chk(&argvars[0])) != NULL
8753 && !error)
8754 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8755
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008757 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008759 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760}
8761
8762/*
8763 * "bufwinnr(nr)" function
8764 */
8765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008766f_bufwinnr(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#ifdef FEAT_WINDOWS
8771 win_T *wp;
8772 int winnr = 0;
8773#endif
8774 buf_T *buf;
8775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008776 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008778 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779#ifdef FEAT_WINDOWS
8780 for (wp = firstwin; wp; wp = wp->w_next)
8781 {
8782 ++winnr;
8783 if (wp->w_buffer == buf)
8784 break;
8785 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008786 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008788 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789#endif
8790 --emsg_off;
8791}
8792
8793/*
8794 * "byte2line(byte)" function
8795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008797f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008798 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800{
8801#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008802 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803#else
8804 long boff = 0;
8805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008806 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008808 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008810 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 (linenr_T)0, &boff);
8812#endif
8813}
8814
8815/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008816 * "byteidx()" function
8817 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008819f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008820 typval_T *argvars;
8821 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008822{
8823#ifdef FEAT_MBYTE
8824 char_u *t;
8825#endif
8826 char_u *str;
8827 long idx;
8828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008829 str = get_tv_string_chk(&argvars[0]);
8830 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008832 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008833 return;
8834
8835#ifdef FEAT_MBYTE
8836 t = str;
8837 for ( ; idx > 0; idx--)
8838 {
8839 if (*t == NUL) /* EOL reached */
8840 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008841 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008842 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008843 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008844#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008845 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008846 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008847#endif
8848}
8849
8850/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008851 * "call(func, arglist)" function
8852 */
8853 static void
8854f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008855 typval_T *argvars;
8856 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008857{
8858 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008859 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008860 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008861 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008862 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008863 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008864
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008865 if (argvars[1].v_type != VAR_LIST)
8866 {
8867 EMSG(_(e_listreq));
8868 return;
8869 }
8870 if (argvars[1].vval.v_list == NULL)
8871 return;
8872
8873 if (argvars[0].v_type == VAR_FUNC)
8874 func = argvars[0].vval.v_string;
8875 else
8876 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008877 if (*func == NUL)
8878 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008879
Bram Moolenaare9a41262005-01-15 22:18:47 +00008880 if (argvars[2].v_type != VAR_UNKNOWN)
8881 {
8882 if (argvars[2].v_type != VAR_DICT)
8883 {
8884 EMSG(_(e_dictreq));
8885 return;
8886 }
8887 selfdict = argvars[2].vval.v_dict;
8888 }
8889
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008890 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8891 item = item->li_next)
8892 {
8893 if (argc == MAX_FUNC_ARGS)
8894 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008895 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008896 break;
8897 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008898 /* Make a copy of each argument. This is needed to be able to set
8899 * v_lock to VAR_FIXED in the copy without changing the original list.
8900 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008901 copy_tv(&item->li_tv, &argv[argc++]);
8902 }
8903
8904 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008905 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008906 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8907 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008908
8909 /* Free the arguments. */
8910 while (argc > 0)
8911 clear_tv(&argv[--argc]);
8912}
8913
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008914#ifdef FEAT_FLOAT
8915/*
8916 * "ceil({float})" function
8917 */
8918 static void
8919f_ceil(argvars, rettv)
8920 typval_T *argvars;
8921 typval_T *rettv;
8922{
8923 float_T f;
8924
8925 rettv->v_type = VAR_FLOAT;
8926 if (get_float_arg(argvars, &f) == OK)
8927 rettv->vval.v_float = ceil(f);
8928 else
8929 rettv->vval.v_float = 0.0;
8930}
8931#endif
8932
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008933/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008934 * "changenr()" function
8935 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008936 static void
8937f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008938 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008939 typval_T *rettv;
8940{
8941 rettv->vval.v_number = curbuf->b_u_seq_cur;
8942}
8943
8944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 * "char2nr(string)" function
8946 */
8947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008948f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008949 typval_T *argvars;
8950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951{
8952#ifdef FEAT_MBYTE
8953 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008954 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 else
8956#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008957 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958}
8959
8960/*
8961 * "cindent(lnum)" function
8962 */
8963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008964f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008965 typval_T *argvars;
8966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967{
8968#ifdef FEAT_CINDENT
8969 pos_T pos;
8970 linenr_T lnum;
8971
8972 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008973 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8975 {
8976 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 curwin->w_cursor = pos;
8979 }
8980 else
8981#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008982 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983}
8984
8985/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008986 * "clearmatches()" function
8987 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008988 static void
8989f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008990 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008991 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008992{
8993#ifdef FEAT_SEARCH_EXTRA
8994 clear_matches(curwin);
8995#endif
8996}
8997
8998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 * "col(string)" function
9000 */
9001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002f_col(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 colnr_T col = 0;
9007 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009008 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009010 fp = var2fpos(&argvars[0], FALSE, &fnum);
9011 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 {
9013 if (fp->col == MAXCOL)
9014 {
9015 /* '> can be MAXCOL, get the length of the line then */
9016 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009017 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 else
9019 col = MAXCOL;
9020 }
9021 else
9022 {
9023 col = fp->col + 1;
9024#ifdef FEAT_VIRTUALEDIT
9025 /* col(".") when the cursor is on the NUL at the end of the line
9026 * because of "coladd" can be seen as an extra column. */
9027 if (virtual_active() && fp == &curwin->w_cursor)
9028 {
9029 char_u *p = ml_get_cursor();
9030
9031 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9032 curwin->w_virtcol - curwin->w_cursor.coladd))
9033 {
9034# ifdef FEAT_MBYTE
9035 int l;
9036
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009037 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 col += l;
9039# else
9040 if (*p != NUL && p[1] == NUL)
9041 ++col;
9042# endif
9043 }
9044 }
9045#endif
9046 }
9047 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009048 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049}
9050
Bram Moolenaar572cb562005-08-05 21:35:02 +00009051#if defined(FEAT_INS_EXPAND)
9052/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009053 * "complete()" function
9054 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009055 static void
9056f_complete(argvars, rettv)
9057 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009058 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009059{
9060 int startcol;
9061
9062 if ((State & INSERT) == 0)
9063 {
9064 EMSG(_("E785: complete() can only be used in Insert mode"));
9065 return;
9066 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009067
9068 /* Check for undo allowed here, because if something was already inserted
9069 * the line was already saved for undo and this check isn't done. */
9070 if (!undo_allowed())
9071 return;
9072
Bram Moolenaarade00832006-03-10 21:46:58 +00009073 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9074 {
9075 EMSG(_(e_invarg));
9076 return;
9077 }
9078
9079 startcol = get_tv_number_chk(&argvars[0], NULL);
9080 if (startcol <= 0)
9081 return;
9082
9083 set_completion(startcol - 1, argvars[1].vval.v_list);
9084}
9085
9086/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009087 * "complete_add()" function
9088 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009089 static void
9090f_complete_add(argvars, rettv)
9091 typval_T *argvars;
9092 typval_T *rettv;
9093{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009094 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009095}
9096
9097/*
9098 * "complete_check()" function
9099 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009100 static void
9101f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009102 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009103 typval_T *rettv;
9104{
9105 int saved = RedrawingDisabled;
9106
9107 RedrawingDisabled = 0;
9108 ins_compl_check_keys(0);
9109 rettv->vval.v_number = compl_interrupted;
9110 RedrawingDisabled = saved;
9111}
9112#endif
9113
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114/*
9115 * "confirm(message, buttons[, default [, type]])" function
9116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009118f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009119 typval_T *argvars UNUSED;
9120 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121{
9122#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9123 char_u *message;
9124 char_u *buttons = NULL;
9125 char_u buf[NUMBUFLEN];
9126 char_u buf2[NUMBUFLEN];
9127 int def = 1;
9128 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009129 char_u *typestr;
9130 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009132 message = get_tv_string_chk(&argvars[0]);
9133 if (message == NULL)
9134 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009135 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009137 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9138 if (buttons == NULL)
9139 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009140 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009142 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009143 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009145 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9146 if (typestr == NULL)
9147 error = TRUE;
9148 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009150 switch (TOUPPER_ASC(*typestr))
9151 {
9152 case 'E': type = VIM_ERROR; break;
9153 case 'Q': type = VIM_QUESTION; break;
9154 case 'I': type = VIM_INFO; break;
9155 case 'W': type = VIM_WARNING; break;
9156 case 'G': type = VIM_GENERIC; break;
9157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 }
9159 }
9160 }
9161 }
9162
9163 if (buttons == NULL || *buttons == NUL)
9164 buttons = (char_u *)_("&Ok");
9165
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009166 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009167 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169#endif
9170}
9171
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009172/*
9173 * "copy()" function
9174 */
9175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009176f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009177 typval_T *argvars;
9178 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009179{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009180 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009181}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009183#ifdef FEAT_FLOAT
9184/*
9185 * "cos()" function
9186 */
9187 static void
9188f_cos(argvars, rettv)
9189 typval_T *argvars;
9190 typval_T *rettv;
9191{
9192 float_T f;
9193
9194 rettv->v_type = VAR_FLOAT;
9195 if (get_float_arg(argvars, &f) == OK)
9196 rettv->vval.v_float = cos(f);
9197 else
9198 rettv->vval.v_float = 0.0;
9199}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009200
9201/*
9202 * "cosh()" function
9203 */
9204 static void
9205f_cosh(argvars, rettv)
9206 typval_T *argvars;
9207 typval_T *rettv;
9208{
9209 float_T f;
9210
9211 rettv->v_type = VAR_FLOAT;
9212 if (get_float_arg(argvars, &f) == OK)
9213 rettv->vval.v_float = cosh(f);
9214 else
9215 rettv->vval.v_float = 0.0;
9216}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009217#endif
9218
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009220 * "count()" function
9221 */
9222 static void
9223f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009224 typval_T *argvars;
9225 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009226{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009227 long n = 0;
9228 int ic = FALSE;
9229
Bram Moolenaare9a41262005-01-15 22:18:47 +00009230 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009231 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009232 listitem_T *li;
9233 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009234 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009235
Bram Moolenaare9a41262005-01-15 22:18:47 +00009236 if ((l = argvars[0].vval.v_list) != NULL)
9237 {
9238 li = l->lv_first;
9239 if (argvars[2].v_type != VAR_UNKNOWN)
9240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009241 int error = FALSE;
9242
9243 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009244 if (argvars[3].v_type != VAR_UNKNOWN)
9245 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009246 idx = get_tv_number_chk(&argvars[3], &error);
9247 if (!error)
9248 {
9249 li = list_find(l, idx);
9250 if (li == NULL)
9251 EMSGN(_(e_listidx), idx);
9252 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009253 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009254 if (error)
9255 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009256 }
9257
9258 for ( ; li != NULL; li = li->li_next)
9259 if (tv_equal(&li->li_tv, &argvars[1], ic))
9260 ++n;
9261 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009262 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009263 else if (argvars[0].v_type == VAR_DICT)
9264 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009265 int todo;
9266 dict_T *d;
9267 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009268
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009269 if ((d = argvars[0].vval.v_dict) != NULL)
9270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009271 int error = FALSE;
9272
Bram Moolenaare9a41262005-01-15 22:18:47 +00009273 if (argvars[2].v_type != VAR_UNKNOWN)
9274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009275 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009276 if (argvars[3].v_type != VAR_UNKNOWN)
9277 EMSG(_(e_invarg));
9278 }
9279
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009280 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009281 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009282 {
9283 if (!HASHITEM_EMPTY(hi))
9284 {
9285 --todo;
9286 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9287 ++n;
9288 }
9289 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009290 }
9291 }
9292 else
9293 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009294 rettv->vval.v_number = n;
9295}
9296
9297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9299 *
9300 * Checks the existence of a cscope connection.
9301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009304 typval_T *argvars UNUSED;
9305 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306{
9307#ifdef FEAT_CSCOPE
9308 int num = 0;
9309 char_u *dbpath = NULL;
9310 char_u *prepend = NULL;
9311 char_u buf[NUMBUFLEN];
9312
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009313 if (argvars[0].v_type != VAR_UNKNOWN
9314 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009316 num = (int)get_tv_number(&argvars[0]);
9317 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009318 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009319 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 }
9321
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009322 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323#endif
9324}
9325
9326/*
9327 * "cursor(lnum, col)" function
9328 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009329 * Moves the cursor to the specified line and column.
9330 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009333f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009334 typval_T *argvars;
9335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336{
9337 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009338#ifdef FEAT_VIRTUALEDIT
9339 long coladd = 0;
9340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009342 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009343 if (argvars[1].v_type == VAR_UNKNOWN)
9344 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009345 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009346
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009347 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009348 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009349 line = pos.lnum;
9350 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009351#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009352 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009353#endif
9354 }
9355 else
9356 {
9357 line = get_tv_lnum(argvars);
9358 col = get_tv_number_chk(&argvars[1], NULL);
9359#ifdef FEAT_VIRTUALEDIT
9360 if (argvars[2].v_type != VAR_UNKNOWN)
9361 coladd = get_tv_number_chk(&argvars[2], NULL);
9362#endif
9363 }
9364 if (line < 0 || col < 0
9365#ifdef FEAT_VIRTUALEDIT
9366 || coladd < 0
9367#endif
9368 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009369 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 if (line > 0)
9371 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 if (col > 0)
9373 curwin->w_cursor.col = col - 1;
9374#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009375 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376#endif
9377
9378 /* Make sure the cursor is in a valid position. */
9379 check_cursor();
9380#ifdef FEAT_MBYTE
9381 /* Correct cursor for multi-byte character. */
9382 if (has_mbyte)
9383 mb_adjust_cursor();
9384#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009385
9386 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009387 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388}
9389
9390/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009391 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 */
9393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009395 typval_T *argvars;
9396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009398 int noref = 0;
9399
9400 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009401 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009402 if (noref < 0 || noref > 1)
9403 EMSG(_(e_invarg));
9404 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009405 {
9406 current_copyID += COPYID_INC;
9407 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409}
9410
9411/*
9412 * "delete()" function
9413 */
9414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009415f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009416 typval_T *argvars;
9417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418{
9419 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009422 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423}
9424
9425/*
9426 * "did_filetype()" function
9427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009430 typval_T *argvars UNUSED;
9431 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432{
9433#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009434 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435#endif
9436}
9437
9438/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009439 * "diff_filler()" function
9440 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009442f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009443 typval_T *argvars UNUSED;
9444 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009445{
9446#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009447 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009448#endif
9449}
9450
9451/*
9452 * "diff_hlID()" function
9453 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009455f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009456 typval_T *argvars UNUSED;
9457 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009458{
9459#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009460 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009461 static linenr_T prev_lnum = 0;
9462 static int changedtick = 0;
9463 static int fnum = 0;
9464 static int change_start = 0;
9465 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009466 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009467 int filler_lines;
9468 int col;
9469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 if (lnum < 0) /* ignore type error in {lnum} arg */
9471 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009472 if (lnum != prev_lnum
9473 || changedtick != curbuf->b_changedtick
9474 || fnum != curbuf->b_fnum)
9475 {
9476 /* New line, buffer, change: need to get the values. */
9477 filler_lines = diff_check(curwin, lnum);
9478 if (filler_lines < 0)
9479 {
9480 if (filler_lines == -1)
9481 {
9482 change_start = MAXCOL;
9483 change_end = -1;
9484 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9485 hlID = HLF_ADD; /* added line */
9486 else
9487 hlID = HLF_CHD; /* changed line */
9488 }
9489 else
9490 hlID = HLF_ADD; /* added line */
9491 }
9492 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009493 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009494 prev_lnum = lnum;
9495 changedtick = curbuf->b_changedtick;
9496 fnum = curbuf->b_fnum;
9497 }
9498
9499 if (hlID == HLF_CHD || hlID == HLF_TXD)
9500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009501 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009502 if (col >= change_start && col <= change_end)
9503 hlID = HLF_TXD; /* changed text */
9504 else
9505 hlID = HLF_CHD; /* changed line */
9506 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009507 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009508#endif
9509}
9510
9511/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009512 * "empty({expr})" function
9513 */
9514 static void
9515f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009516 typval_T *argvars;
9517 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009518{
9519 int n;
9520
9521 switch (argvars[0].v_type)
9522 {
9523 case VAR_STRING:
9524 case VAR_FUNC:
9525 n = argvars[0].vval.v_string == NULL
9526 || *argvars[0].vval.v_string == NUL;
9527 break;
9528 case VAR_NUMBER:
9529 n = argvars[0].vval.v_number == 0;
9530 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009531#ifdef FEAT_FLOAT
9532 case VAR_FLOAT:
9533 n = argvars[0].vval.v_float == 0.0;
9534 break;
9535#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009536 case VAR_LIST:
9537 n = argvars[0].vval.v_list == NULL
9538 || argvars[0].vval.v_list->lv_first == NULL;
9539 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009540 case VAR_DICT:
9541 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009542 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009543 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009544 default:
9545 EMSG2(_(e_intern2), "f_empty()");
9546 n = 0;
9547 }
9548
9549 rettv->vval.v_number = n;
9550}
9551
9552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 * "escape({string}, {chars})" function
9554 */
9555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009557 typval_T *argvars;
9558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559{
9560 char_u buf[NUMBUFLEN];
9561
Bram Moolenaar758711c2005-02-02 23:11:38 +00009562 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9563 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565}
9566
9567/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009568 * "eval()" function
9569 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009570 static void
9571f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009572 typval_T *argvars;
9573 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009574{
9575 char_u *s;
9576
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009577 s = get_tv_string_chk(&argvars[0]);
9578 if (s != NULL)
9579 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009581 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9582 {
9583 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009584 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009586 else if (*s != NUL)
9587 EMSG(_(e_trailing));
9588}
9589
9590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 * "eventhandler()" function
9592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009595 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599}
9600
9601/*
9602 * "executable()" function
9603 */
9604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009605f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009606 typval_T *argvars;
9607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009609 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610}
9611
9612/*
9613 * "exists()" function
9614 */
9615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009617 typval_T *argvars;
9618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619{
9620 char_u *p;
9621 char_u *name;
9622 int n = FALSE;
9623 int len = 0;
9624
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 if (*p == '$') /* environment variable */
9627 {
9628 /* first try "normal" environment variables (fast) */
9629 if (mch_getenv(p + 1) != NULL)
9630 n = TRUE;
9631 else
9632 {
9633 /* try expanding things like $VIM and ${HOME} */
9634 p = expand_env_save(p);
9635 if (p != NULL && *p != '$')
9636 n = TRUE;
9637 vim_free(p);
9638 }
9639 }
9640 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009642 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009643 if (*skipwhite(p) != NUL)
9644 n = FALSE; /* trailing garbage */
9645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 else if (*p == '*') /* internal or user defined function */
9647 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009648 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 }
9650 else if (*p == ':')
9651 {
9652 n = cmd_exists(p + 1);
9653 }
9654 else if (*p == '#')
9655 {
9656#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009657 if (p[1] == '#')
9658 n = autocmd_supported(p + 2);
9659 else
9660 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661#endif
9662 }
9663 else /* internal variable */
9664 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009665 char_u *tofree;
9666 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009668 /* get_name_len() takes care of expanding curly braces */
9669 name = p;
9670 len = get_name_len(&p, &tofree, TRUE, FALSE);
9671 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009673 if (tofree != NULL)
9674 name = tofree;
9675 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9676 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009678 /* handle d.key, l[idx], f(expr) */
9679 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9680 if (n)
9681 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 }
9683 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009684 if (*p != NUL)
9685 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009687 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 }
9689
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009690 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691}
9692
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009693#ifdef FEAT_FLOAT
9694/*
9695 * "exp()" function
9696 */
9697 static void
9698f_exp(argvars, rettv)
9699 typval_T *argvars;
9700 typval_T *rettv;
9701{
9702 float_T f;
9703
9704 rettv->v_type = VAR_FLOAT;
9705 if (get_float_arg(argvars, &f) == OK)
9706 rettv->vval.v_float = exp(f);
9707 else
9708 rettv->vval.v_float = 0.0;
9709}
9710#endif
9711
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712/*
9713 * "expand()" function
9714 */
9715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009716f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009717 typval_T *argvars;
9718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719{
9720 char_u *s;
9721 int len;
9722 char_u *errormsg;
9723 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9724 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009725 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727 rettv->v_type = VAR_STRING;
9728 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 if (*s == '%' || *s == '#' || *s == '<')
9730 {
9731 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009732 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 --emsg_off;
9734 }
9735 else
9736 {
9737 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009738 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009739 if (argvars[1].v_type != VAR_UNKNOWN
9740 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009742 if (!error)
9743 {
9744 ExpandInit(&xpc);
9745 xpc.xp_context = EXPAND_FILES;
9746 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009747 }
9748 else
9749 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 }
9751}
9752
9753/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009754 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009755 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009756 */
9757 static void
9758f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009759 typval_T *argvars;
9760 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009761{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009762 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009763 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009764 list_T *l1, *l2;
9765 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009766 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009767 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009768
Bram Moolenaare9a41262005-01-15 22:18:47 +00009769 l1 = argvars[0].vval.v_list;
9770 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009771 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9772 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009773 {
9774 if (argvars[2].v_type != VAR_UNKNOWN)
9775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009776 before = get_tv_number_chk(&argvars[2], &error);
9777 if (error)
9778 return; /* type error; errmsg already given */
9779
Bram Moolenaar758711c2005-02-02 23:11:38 +00009780 if (before == l1->lv_len)
9781 item = NULL;
9782 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009783 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009784 item = list_find(l1, before);
9785 if (item == NULL)
9786 {
9787 EMSGN(_(e_listidx), before);
9788 return;
9789 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009790 }
9791 }
9792 else
9793 item = NULL;
9794 list_extend(l1, l2, item);
9795
Bram Moolenaare9a41262005-01-15 22:18:47 +00009796 copy_tv(&argvars[0], rettv);
9797 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009798 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009799 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9800 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009801 dict_T *d1, *d2;
9802 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009803 char_u *action;
9804 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009805 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009806 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009807
9808 d1 = argvars[0].vval.v_dict;
9809 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009810 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9811 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009812 {
9813 /* Check the third argument. */
9814 if (argvars[2].v_type != VAR_UNKNOWN)
9815 {
9816 static char *(av[]) = {"keep", "force", "error"};
9817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009818 action = get_tv_string_chk(&argvars[2]);
9819 if (action == NULL)
9820 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009821 for (i = 0; i < 3; ++i)
9822 if (STRCMP(action, av[i]) == 0)
9823 break;
9824 if (i == 3)
9825 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009826 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009827 return;
9828 }
9829 }
9830 else
9831 action = (char_u *)"force";
9832
9833 /* Go over all entries in the second dict and add them to the
9834 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009835 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009836 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009837 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009838 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009839 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009840 --todo;
9841 di1 = dict_find(d1, hi2->hi_key, -1);
9842 if (di1 == NULL)
9843 {
9844 di1 = dictitem_copy(HI2DI(hi2));
9845 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9846 dictitem_free(di1);
9847 }
9848 else if (*action == 'e')
9849 {
9850 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9851 break;
9852 }
9853 else if (*action == 'f')
9854 {
9855 clear_tv(&di1->di_tv);
9856 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9857 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009858 }
9859 }
9860
Bram Moolenaare9a41262005-01-15 22:18:47 +00009861 copy_tv(&argvars[0], rettv);
9862 }
9863 }
9864 else
9865 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009866}
9867
9868/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009869 * "feedkeys()" function
9870 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009871 static void
9872f_feedkeys(argvars, rettv)
9873 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009874 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009875{
9876 int remap = TRUE;
9877 char_u *keys, *flags;
9878 char_u nbuf[NUMBUFLEN];
9879 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009880 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009881
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009882 /* This is not allowed in the sandbox. If the commands would still be
9883 * executed in the sandbox it would be OK, but it probably happens later,
9884 * when "sandbox" is no longer set. */
9885 if (check_secure())
9886 return;
9887
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009888 keys = get_tv_string(&argvars[0]);
9889 if (*keys != NUL)
9890 {
9891 if (argvars[1].v_type != VAR_UNKNOWN)
9892 {
9893 flags = get_tv_string_buf(&argvars[1], nbuf);
9894 for ( ; *flags != NUL; ++flags)
9895 {
9896 switch (*flags)
9897 {
9898 case 'n': remap = FALSE; break;
9899 case 'm': remap = TRUE; break;
9900 case 't': typed = TRUE; break;
9901 }
9902 }
9903 }
9904
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009905 /* Need to escape K_SPECIAL and CSI before putting the string in the
9906 * typeahead buffer. */
9907 keys_esc = vim_strsave_escape_csi(keys);
9908 if (keys_esc != NULL)
9909 {
9910 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009911 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009912 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009913 if (vgetc_busy)
9914 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009915 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009916 }
9917}
9918
9919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 * "filereadable()" function
9921 */
9922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009923f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009924 typval_T *argvars;
9925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926{
Bram Moolenaarc236c162008-07-13 17:41:49 +00009927 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 char_u *p;
9929 int n;
9930
Bram Moolenaarc236c162008-07-13 17:41:49 +00009931#ifndef O_NONBLOCK
9932# define O_NONBLOCK 0
9933#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009934 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +00009935 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
9936 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 {
9938 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009939 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 }
9941 else
9942 n = FALSE;
9943
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945}
9946
9947/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009948 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 * rights to write into.
9950 */
9951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009952f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009953 typval_T *argvars;
9954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009956 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957}
9958
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009959static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009960
9961 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009962findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009963 typval_T *argvars;
9964 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009965 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009966{
9967#ifdef FEAT_SEARCHPATH
9968 char_u *fname;
9969 char_u *fresult = NULL;
9970 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9971 char_u *p;
9972 char_u pathbuf[NUMBUFLEN];
9973 int count = 1;
9974 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009975 int error = FALSE;
9976#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009977
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009978 rettv->vval.v_string = NULL;
9979 rettv->v_type = VAR_STRING;
9980
9981#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009983
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009984 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009985 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009986 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9987 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009988 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009989 else
9990 {
9991 if (*p != NUL)
9992 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009994 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009995 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009996 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009997 }
9998
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009999 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10000 error = TRUE;
10001
10002 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010003 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010004 do
10005 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010006 if (rettv->v_type == VAR_STRING)
10007 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010008 fresult = find_file_in_path_option(first ? fname : NULL,
10009 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010010 0, first, path,
10011 find_what,
10012 curbuf->b_ffname,
10013 find_what == FINDFILE_DIR
10014 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010015 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010016
10017 if (fresult != NULL && rettv->v_type == VAR_LIST)
10018 list_append_string(rettv->vval.v_list, fresult, -1);
10019
10020 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010021 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010022
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010023 if (rettv->v_type == VAR_STRING)
10024 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010025#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010026}
10027
Bram Moolenaar33570922005-01-25 22:26:29 +000010028static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10029static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010030
10031/*
10032 * Implementation of map() and filter().
10033 */
10034 static void
10035filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010036 typval_T *argvars;
10037 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010038 int map;
10039{
10040 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010041 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010042 listitem_T *li, *nli;
10043 list_T *l = NULL;
10044 dictitem_T *di;
10045 hashtab_T *ht;
10046 hashitem_T *hi;
10047 dict_T *d = NULL;
10048 typval_T save_val;
10049 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010050 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010051 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010052 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010053 int save_did_emsg;
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010054 int index = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010055
Bram Moolenaare9a41262005-01-15 22:18:47 +000010056 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010057 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010058 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010059 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010060 return;
10061 }
10062 else if (argvars[0].v_type == VAR_DICT)
10063 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010064 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010065 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010066 return;
10067 }
10068 else
10069 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010070 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010071 return;
10072 }
10073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010074 expr = get_tv_string_buf_chk(&argvars[1], buf);
10075 /* On type errors, the preceding call has already displayed an error
10076 * message. Avoid a misleading error message for an empty string that
10077 * was not passed as argument. */
10078 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010080 prepare_vimvar(VV_VAL, &save_val);
10081 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010082
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010083 /* We reset "did_emsg" to be able to detect whether an error
10084 * occurred during evaluation of the expression. */
10085 save_did_emsg = did_emsg;
10086 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010087
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010088 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010089 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010091 vimvars[VV_KEY].vv_type = VAR_STRING;
10092
10093 ht = &d->dv_hashtab;
10094 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010095 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010096 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 if (!HASHITEM_EMPTY(hi))
10099 {
10100 --todo;
10101 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010102 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010103 break;
10104 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010105 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010106 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010107 break;
10108 if (!map && rem)
10109 dictitem_remove(d, di);
10110 clear_tv(&vimvars[VV_KEY].vv_tv);
10111 }
10112 }
10113 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010114 }
10115 else
10116 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010117 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10118
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010119 for (li = l->lv_first; li != NULL; li = nli)
10120 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010121 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010122 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010123 nli = li->li_next;
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010124 vimvars[VV_KEY].vv_nr = index;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010125 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010126 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010127 break;
10128 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010129 listitem_remove(l, li);
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010130 ++index;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010131 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010132 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010133
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010134 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010135 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010136
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010137 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010138 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010139
10140 copy_tv(&argvars[0], rettv);
10141}
10142
10143 static int
10144filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010145 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010146 char_u *expr;
10147 int map;
10148 int *remp;
10149{
Bram Moolenaar33570922005-01-25 22:26:29 +000010150 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010151 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010152 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010153
Bram Moolenaar33570922005-01-25 22:26:29 +000010154 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010155 s = expr;
10156 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010157 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010158 if (*s != NUL) /* check for trailing chars after expr */
10159 {
10160 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010161 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010162 }
10163 if (map)
10164 {
10165 /* map(): replace the list item value */
10166 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010167 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010168 *tv = rettv;
10169 }
10170 else
10171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010172 int error = FALSE;
10173
Bram Moolenaare9a41262005-01-15 22:18:47 +000010174 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010175 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010176 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010177 /* On type error, nothing has been removed; return FAIL to stop the
10178 * loop. The error message was given by get_tv_number_chk(). */
10179 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010180 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010181 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010182 retval = OK;
10183theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010184 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010185 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010186}
10187
10188/*
10189 * "filter()" function
10190 */
10191 static void
10192f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010193 typval_T *argvars;
10194 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010195{
10196 filter_map(argvars, rettv, FALSE);
10197}
10198
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010199/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010200 * "finddir({fname}[, {path}[, {count}]])" function
10201 */
10202 static void
10203f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010204 typval_T *argvars;
10205 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010206{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010207 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010208}
10209
10210/*
10211 * "findfile({fname}[, {path}[, {count}]])" function
10212 */
10213 static void
10214f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010215 typval_T *argvars;
10216 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010217{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010218 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010219}
10220
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010221#ifdef FEAT_FLOAT
10222/*
10223 * "float2nr({float})" function
10224 */
10225 static void
10226f_float2nr(argvars, rettv)
10227 typval_T *argvars;
10228 typval_T *rettv;
10229{
10230 float_T f;
10231
10232 if (get_float_arg(argvars, &f) == OK)
10233 {
10234 if (f < -0x7fffffff)
10235 rettv->vval.v_number = -0x7fffffff;
10236 else if (f > 0x7fffffff)
10237 rettv->vval.v_number = 0x7fffffff;
10238 else
10239 rettv->vval.v_number = (varnumber_T)f;
10240 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010241}
10242
10243/*
10244 * "floor({float})" function
10245 */
10246 static void
10247f_floor(argvars, rettv)
10248 typval_T *argvars;
10249 typval_T *rettv;
10250{
10251 float_T f;
10252
10253 rettv->v_type = VAR_FLOAT;
10254 if (get_float_arg(argvars, &f) == OK)
10255 rettv->vval.v_float = floor(f);
10256 else
10257 rettv->vval.v_float = 0.0;
10258}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010259
10260/*
10261 * "fmod()" function
10262 */
10263 static void
10264f_fmod(argvars, rettv)
10265 typval_T *argvars;
10266 typval_T *rettv;
10267{
10268 float_T fx, fy;
10269
10270 rettv->v_type = VAR_FLOAT;
10271 if (get_float_arg(argvars, &fx) == OK
10272 && get_float_arg(&argvars[1], &fy) == OK)
10273 rettv->vval.v_float = fmod(fx, fy);
10274 else
10275 rettv->vval.v_float = 0.0;
10276}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010277#endif
10278
Bram Moolenaar0d660222005-01-07 21:51:51 +000010279/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010280 * "fnameescape({string})" function
10281 */
10282 static void
10283f_fnameescape(argvars, rettv)
10284 typval_T *argvars;
10285 typval_T *rettv;
10286{
10287 rettv->vval.v_string = vim_strsave_fnameescape(
10288 get_tv_string(&argvars[0]), FALSE);
10289 rettv->v_type = VAR_STRING;
10290}
10291
10292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 * "fnamemodify({fname}, {mods})" function
10294 */
10295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010296f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010297 typval_T *argvars;
10298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299{
10300 char_u *fname;
10301 char_u *mods;
10302 int usedlen = 0;
10303 int len;
10304 char_u *fbuf = NULL;
10305 char_u buf[NUMBUFLEN];
10306
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010307 fname = get_tv_string_chk(&argvars[0]);
10308 mods = get_tv_string_buf_chk(&argvars[1], buf);
10309 if (fname == NULL || mods == NULL)
10310 fname = NULL;
10311 else
10312 {
10313 len = (int)STRLEN(fname);
10314 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010317 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010319 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010321 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 vim_free(fbuf);
10323}
10324
Bram Moolenaar33570922005-01-25 22:26:29 +000010325static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326
10327/*
10328 * "foldclosed()" function
10329 */
10330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010331foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010332 typval_T *argvars;
10333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 int end;
10335{
10336#ifdef FEAT_FOLDING
10337 linenr_T lnum;
10338 linenr_T first, last;
10339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010340 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10342 {
10343 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10344 {
10345 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010346 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010348 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 return;
10350 }
10351 }
10352#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010353 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354}
10355
10356/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010357 * "foldclosed()" function
10358 */
10359 static void
10360f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010361 typval_T *argvars;
10362 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010363{
10364 foldclosed_both(argvars, rettv, FALSE);
10365}
10366
10367/*
10368 * "foldclosedend()" function
10369 */
10370 static void
10371f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010372 typval_T *argvars;
10373 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010374{
10375 foldclosed_both(argvars, rettv, TRUE);
10376}
10377
10378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 * "foldlevel()" function
10380 */
10381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010382f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010383 typval_T *argvars;
10384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385{
10386#ifdef FEAT_FOLDING
10387 linenr_T lnum;
10388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010389 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010391 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393}
10394
10395/*
10396 * "foldtext()" function
10397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010399f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010400 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402{
10403#ifdef FEAT_FOLDING
10404 linenr_T lnum;
10405 char_u *s;
10406 char_u *r;
10407 int len;
10408 char *txt;
10409#endif
10410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010411 rettv->v_type = VAR_STRING;
10412 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010414 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10415 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10416 <= curbuf->b_ml.ml_line_count
10417 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 {
10419 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010420 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10421 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 {
10423 if (!linewhite(lnum))
10424 break;
10425 ++lnum;
10426 }
10427
10428 /* Find interesting text in this line. */
10429 s = skipwhite(ml_get(lnum));
10430 /* skip C comment-start */
10431 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010432 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010434 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010435 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010436 {
10437 s = skipwhite(ml_get(lnum + 1));
10438 if (*s == '*')
10439 s = skipwhite(s + 1);
10440 }
10441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 txt = _("+-%s%3ld lines: ");
10443 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010444 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 + 20 /* for %3ld */
10446 + STRLEN(s))); /* concatenated */
10447 if (r != NULL)
10448 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010449 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10450 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10451 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 len = (int)STRLEN(r);
10453 STRCAT(r, s);
10454 /* remove 'foldmarker' and 'commentstring' */
10455 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010456 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 }
10458 }
10459#endif
10460}
10461
10462/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010463 * "foldtextresult(lnum)" function
10464 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010466f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010467 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010468 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010469{
10470#ifdef FEAT_FOLDING
10471 linenr_T lnum;
10472 char_u *text;
10473 char_u buf[51];
10474 foldinfo_T foldinfo;
10475 int fold_count;
10476#endif
10477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010478 rettv->v_type = VAR_STRING;
10479 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010480#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010481 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010482 /* treat illegal types and illegal string values for {lnum} the same */
10483 if (lnum < 0)
10484 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010485 fold_count = foldedCount(curwin, lnum, &foldinfo);
10486 if (fold_count > 0)
10487 {
10488 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10489 &foldinfo, buf);
10490 if (text == buf)
10491 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010492 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010493 }
10494#endif
10495}
10496
10497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 * "foreground()" function
10499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010501f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010502 typval_T *argvars UNUSED;
10503 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505#ifdef FEAT_GUI
10506 if (gui.in_use)
10507 gui_mch_set_foreground();
10508#else
10509# ifdef WIN32
10510 win32_set_foreground();
10511# endif
10512#endif
10513}
10514
10515/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010516 * "function()" function
10517 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010519f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010520 typval_T *argvars;
10521 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010522{
10523 char_u *s;
10524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010525 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010526 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010527 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010528 /* Don't check an autoload name for existence here. */
10529 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010530 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010531 else
10532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010533 rettv->vval.v_string = vim_strsave(s);
10534 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010535 }
10536}
10537
10538/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010539 * "garbagecollect()" function
10540 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010541 static void
10542f_garbagecollect(argvars, rettv)
10543 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010544 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010545{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010546 /* This is postponed until we are back at the toplevel, because we may be
10547 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10548 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010549
10550 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10551 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010552}
10553
10554/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010555 * "get()" function
10556 */
10557 static void
10558f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010559 typval_T *argvars;
10560 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010561{
Bram Moolenaar33570922005-01-25 22:26:29 +000010562 listitem_T *li;
10563 list_T *l;
10564 dictitem_T *di;
10565 dict_T *d;
10566 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010567
Bram Moolenaare9a41262005-01-15 22:18:47 +000010568 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010569 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010570 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010572 int error = FALSE;
10573
10574 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10575 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010577 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010578 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010579 else if (argvars[0].v_type == VAR_DICT)
10580 {
10581 if ((d = argvars[0].vval.v_dict) != NULL)
10582 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010583 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010584 if (di != NULL)
10585 tv = &di->di_tv;
10586 }
10587 }
10588 else
10589 EMSG2(_(e_listdictarg), "get()");
10590
10591 if (tv == NULL)
10592 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010593 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010594 copy_tv(&argvars[2], rettv);
10595 }
10596 else
10597 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010598}
10599
Bram Moolenaar342337a2005-07-21 21:11:17 +000010600static 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 +000010601
10602/*
10603 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010604 * Return a range (from start to end) of lines in rettv from the specified
10605 * buffer.
10606 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010607 */
10608 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010609get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010610 buf_T *buf;
10611 linenr_T start;
10612 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010613 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010614 typval_T *rettv;
10615{
10616 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010617
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010618 if (retlist && rettv_list_alloc(rettv) == FAIL)
10619 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010620
10621 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10622 return;
10623
10624 if (!retlist)
10625 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010626 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10627 p = ml_get_buf(buf, start, FALSE);
10628 else
10629 p = (char_u *)"";
10630
10631 rettv->v_type = VAR_STRING;
10632 rettv->vval.v_string = vim_strsave(p);
10633 }
10634 else
10635 {
10636 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010637 return;
10638
10639 if (start < 1)
10640 start = 1;
10641 if (end > buf->b_ml.ml_line_count)
10642 end = buf->b_ml.ml_line_count;
10643 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010644 if (list_append_string(rettv->vval.v_list,
10645 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010646 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010647 }
10648}
10649
10650/*
10651 * "getbufline()" function
10652 */
10653 static void
10654f_getbufline(argvars, rettv)
10655 typval_T *argvars;
10656 typval_T *rettv;
10657{
10658 linenr_T lnum;
10659 linenr_T end;
10660 buf_T *buf;
10661
10662 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10663 ++emsg_off;
10664 buf = get_buf_tv(&argvars[0]);
10665 --emsg_off;
10666
Bram Moolenaar661b1822005-07-28 22:36:45 +000010667 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010668 if (argvars[2].v_type == VAR_UNKNOWN)
10669 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010670 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010671 end = get_tv_lnum_buf(&argvars[2], buf);
10672
Bram Moolenaar342337a2005-07-21 21:11:17 +000010673 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010674}
10675
Bram Moolenaar0d660222005-01-07 21:51:51 +000010676/*
10677 * "getbufvar()" function
10678 */
10679 static void
10680f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010681 typval_T *argvars;
10682 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010683{
10684 buf_T *buf;
10685 buf_T *save_curbuf;
10686 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010687 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010689 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10690 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010691 ++emsg_off;
10692 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010693
10694 rettv->v_type = VAR_STRING;
10695 rettv->vval.v_string = NULL;
10696
10697 if (buf != NULL && varname != NULL)
10698 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010699 /* set curbuf to be our buf, temporarily */
10700 save_curbuf = curbuf;
10701 curbuf = buf;
10702
Bram Moolenaar0d660222005-01-07 21:51:51 +000010703 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010704 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010705 else
10706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010707 if (*varname == NUL)
10708 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10709 * scope prefix before the NUL byte is required by
10710 * find_var_in_ht(). */
10711 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010712 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010713 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010714 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010715 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010716 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010717
10718 /* restore previous notion of curbuf */
10719 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010720 }
10721
10722 --emsg_off;
10723}
10724
10725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 * "getchar()" function
10727 */
10728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010729f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010730 typval_T *argvars;
10731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732{
10733 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010734 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010736 /* Position the cursor. Needed after a message that ends in a space. */
10737 windgoto(msg_row, msg_col);
10738
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 ++no_mapping;
10740 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010741 for (;;)
10742 {
10743 if (argvars[0].v_type == VAR_UNKNOWN)
10744 /* getchar(): blocking wait. */
10745 n = safe_vgetc();
10746 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10747 /* getchar(1): only check if char avail */
10748 n = vpeekc();
10749 else if (error || vpeekc() == NUL)
10750 /* illegal argument or getchar(0) and no char avail: return zero */
10751 n = 0;
10752 else
10753 /* getchar(0) and char avail: return char */
10754 n = safe_vgetc();
10755 if (n == K_IGNORE)
10756 continue;
10757 break;
10758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 --no_mapping;
10760 --allow_keys;
10761
Bram Moolenaar219b8702006-11-01 14:32:36 +000010762 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10763 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10764 vimvars[VV_MOUSE_COL].vv_nr = 0;
10765
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010766 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 if (IS_SPECIAL(n) || mod_mask != 0)
10768 {
10769 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10770 int i = 0;
10771
10772 /* Turn a special key into three bytes, plus modifier. */
10773 if (mod_mask != 0)
10774 {
10775 temp[i++] = K_SPECIAL;
10776 temp[i++] = KS_MODIFIER;
10777 temp[i++] = mod_mask;
10778 }
10779 if (IS_SPECIAL(n))
10780 {
10781 temp[i++] = K_SPECIAL;
10782 temp[i++] = K_SECOND(n);
10783 temp[i++] = K_THIRD(n);
10784 }
10785#ifdef FEAT_MBYTE
10786 else if (has_mbyte)
10787 i += (*mb_char2bytes)(n, temp + i);
10788#endif
10789 else
10790 temp[i++] = n;
10791 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010792 rettv->v_type = VAR_STRING;
10793 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010794
10795#ifdef FEAT_MOUSE
10796 if (n == K_LEFTMOUSE
10797 || n == K_LEFTMOUSE_NM
10798 || n == K_LEFTDRAG
10799 || n == K_LEFTRELEASE
10800 || n == K_LEFTRELEASE_NM
10801 || n == K_MIDDLEMOUSE
10802 || n == K_MIDDLEDRAG
10803 || n == K_MIDDLERELEASE
10804 || n == K_RIGHTMOUSE
10805 || n == K_RIGHTDRAG
10806 || n == K_RIGHTRELEASE
10807 || n == K_X1MOUSE
10808 || n == K_X1DRAG
10809 || n == K_X1RELEASE
10810 || n == K_X2MOUSE
10811 || n == K_X2DRAG
10812 || n == K_X2RELEASE
10813 || n == K_MOUSEDOWN
10814 || n == K_MOUSEUP)
10815 {
10816 int row = mouse_row;
10817 int col = mouse_col;
10818 win_T *win;
10819 linenr_T lnum;
10820# ifdef FEAT_WINDOWS
10821 win_T *wp;
10822# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010823 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010824
10825 if (row >= 0 && col >= 0)
10826 {
10827 /* Find the window at the mouse coordinates and compute the
10828 * text position. */
10829 win = mouse_find_win(&row, &col);
10830 (void)mouse_comp_pos(win, &row, &col, &lnum);
10831# ifdef FEAT_WINDOWS
10832 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010833 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010834# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010835 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010836 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10837 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10838 }
10839 }
10840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 }
10842}
10843
10844/*
10845 * "getcharmod()" function
10846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010848f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010849 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853}
10854
10855/*
10856 * "getcmdline()" function
10857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010859f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010860 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010863 rettv->v_type = VAR_STRING;
10864 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865}
10866
10867/*
10868 * "getcmdpos()" function
10869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010871f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010872 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010875 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876}
10877
10878/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010879 * "getcmdtype()" function
10880 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010881 static void
10882f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010883 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010884 typval_T *rettv;
10885{
10886 rettv->v_type = VAR_STRING;
10887 rettv->vval.v_string = alloc(2);
10888 if (rettv->vval.v_string != NULL)
10889 {
10890 rettv->vval.v_string[0] = get_cmdline_type();
10891 rettv->vval.v_string[1] = NUL;
10892 }
10893}
10894
10895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 * "getcwd()" function
10897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010899f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010900 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902{
10903 char_u cwd[MAXPATHL];
10904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010905 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010907 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 else
10909 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010910 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010912 if (rettv->vval.v_string != NULL)
10913 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914#endif
10915 }
10916}
10917
10918/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010919 * "getfontname()" function
10920 */
10921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010922f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010923 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010924 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010925{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926 rettv->v_type = VAR_STRING;
10927 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010928#ifdef FEAT_GUI
10929 if (gui.in_use)
10930 {
10931 GuiFont font;
10932 char_u *name = NULL;
10933
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010934 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010935 {
10936 /* Get the "Normal" font. Either the name saved by
10937 * hl_set_font_name() or from the font ID. */
10938 font = gui.norm_font;
10939 name = hl_get_font_name();
10940 }
10941 else
10942 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010943 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010944 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10945 return;
10946 font = gui_mch_get_font(name, FALSE);
10947 if (font == NOFONT)
10948 return; /* Invalid font name, return empty string. */
10949 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010950 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010951 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010952 gui_mch_free_font(font);
10953 }
10954#endif
10955}
10956
10957/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010958 * "getfperm({fname})" function
10959 */
10960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010961f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010962 typval_T *argvars;
10963 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010964{
10965 char_u *fname;
10966 struct stat st;
10967 char_u *perm = NULL;
10968 char_u flags[] = "rwx";
10969 int i;
10970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010971 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010972
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010973 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010974 if (mch_stat((char *)fname, &st) >= 0)
10975 {
10976 perm = vim_strsave((char_u *)"---------");
10977 if (perm != NULL)
10978 {
10979 for (i = 0; i < 9; i++)
10980 {
10981 if (st.st_mode & (1 << (8 - i)))
10982 perm[i] = flags[i % 3];
10983 }
10984 }
10985 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010986 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010987}
10988
10989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 * "getfsize({fname})" function
10991 */
10992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010993f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010994 typval_T *argvars;
10995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996{
10997 char_u *fname;
10998 struct stat st;
10999
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011000 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011002 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003
11004 if (mch_stat((char *)fname, &st) >= 0)
11005 {
11006 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011007 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011011
11012 /* non-perfect check for overflow */
11013 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11014 rettv->vval.v_number = -2;
11015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 }
11017 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011018 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019}
11020
11021/*
11022 * "getftime({fname})" function
11023 */
11024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011026 typval_T *argvars;
11027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028{
11029 char_u *fname;
11030 struct stat st;
11031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011032 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033
11034 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011035 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011037 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038}
11039
11040/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011041 * "getftype({fname})" function
11042 */
11043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011045 typval_T *argvars;
11046 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011047{
11048 char_u *fname;
11049 struct stat st;
11050 char_u *type = NULL;
11051 char *t;
11052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011053 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011055 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011056 if (mch_lstat((char *)fname, &st) >= 0)
11057 {
11058#ifdef S_ISREG
11059 if (S_ISREG(st.st_mode))
11060 t = "file";
11061 else if (S_ISDIR(st.st_mode))
11062 t = "dir";
11063# ifdef S_ISLNK
11064 else if (S_ISLNK(st.st_mode))
11065 t = "link";
11066# endif
11067# ifdef S_ISBLK
11068 else if (S_ISBLK(st.st_mode))
11069 t = "bdev";
11070# endif
11071# ifdef S_ISCHR
11072 else if (S_ISCHR(st.st_mode))
11073 t = "cdev";
11074# endif
11075# ifdef S_ISFIFO
11076 else if (S_ISFIFO(st.st_mode))
11077 t = "fifo";
11078# endif
11079# ifdef S_ISSOCK
11080 else if (S_ISSOCK(st.st_mode))
11081 t = "fifo";
11082# endif
11083 else
11084 t = "other";
11085#else
11086# ifdef S_IFMT
11087 switch (st.st_mode & S_IFMT)
11088 {
11089 case S_IFREG: t = "file"; break;
11090 case S_IFDIR: t = "dir"; break;
11091# ifdef S_IFLNK
11092 case S_IFLNK: t = "link"; break;
11093# endif
11094# ifdef S_IFBLK
11095 case S_IFBLK: t = "bdev"; break;
11096# endif
11097# ifdef S_IFCHR
11098 case S_IFCHR: t = "cdev"; break;
11099# endif
11100# ifdef S_IFIFO
11101 case S_IFIFO: t = "fifo"; break;
11102# endif
11103# ifdef S_IFSOCK
11104 case S_IFSOCK: t = "socket"; break;
11105# endif
11106 default: t = "other";
11107 }
11108# else
11109 if (mch_isdir(fname))
11110 t = "dir";
11111 else
11112 t = "file";
11113# endif
11114#endif
11115 type = vim_strsave((char_u *)t);
11116 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011117 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011118}
11119
11120/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011121 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011122 */
11123 static void
11124f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011125 typval_T *argvars;
11126 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011127{
11128 linenr_T lnum;
11129 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011130 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011131
11132 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011133 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011134 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011135 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011136 retlist = FALSE;
11137 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011138 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011139 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011140 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011141 retlist = TRUE;
11142 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011143
Bram Moolenaar342337a2005-07-21 21:11:17 +000011144 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011145}
11146
11147/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011148 * "getmatches()" function
11149 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011150 static void
11151f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011152 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011153 typval_T *rettv;
11154{
11155#ifdef FEAT_SEARCH_EXTRA
11156 dict_T *dict;
11157 matchitem_T *cur = curwin->w_match_head;
11158
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011159 if (rettv_list_alloc(rettv) == OK)
11160 {
11161 while (cur != NULL)
11162 {
11163 dict = dict_alloc();
11164 if (dict == NULL)
11165 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011166 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11167 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11168 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11169 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11170 list_append_dict(rettv->vval.v_list, dict);
11171 cur = cur->next;
11172 }
11173 }
11174#endif
11175}
11176
11177/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011178 * "getpid()" function
11179 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011180 static void
11181f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011182 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011183 typval_T *rettv;
11184{
11185 rettv->vval.v_number = mch_get_pid();
11186}
11187
11188/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011189 * "getpos(string)" function
11190 */
11191 static void
11192f_getpos(argvars, rettv)
11193 typval_T *argvars;
11194 typval_T *rettv;
11195{
11196 pos_T *fp;
11197 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011198 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011199
11200 if (rettv_list_alloc(rettv) == OK)
11201 {
11202 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011203 fp = var2fpos(&argvars[0], TRUE, &fnum);
11204 if (fnum != -1)
11205 list_append_number(l, (varnumber_T)fnum);
11206 else
11207 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011208 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11209 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011210 list_append_number(l, (fp != NULL)
11211 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011212 : (varnumber_T)0);
11213 list_append_number(l,
11214#ifdef FEAT_VIRTUALEDIT
11215 (fp != NULL) ? (varnumber_T)fp->coladd :
11216#endif
11217 (varnumber_T)0);
11218 }
11219 else
11220 rettv->vval.v_number = FALSE;
11221}
11222
11223/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011224 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011225 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011226 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011227f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011228 typval_T *argvars UNUSED;
11229 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011230{
11231#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011232 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011233#endif
11234
Bram Moolenaar2641f772005-03-25 21:58:17 +000011235#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011236 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011237 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011238 wp = NULL;
11239 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11240 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011241 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011242 if (wp == NULL)
11243 return;
11244 }
11245
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011246 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011247 }
11248#endif
11249}
11250
11251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 * "getreg()" function
11253 */
11254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011256 typval_T *argvars;
11257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258{
11259 char_u *strregname;
11260 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011261 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011262 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011264 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011266 strregname = get_tv_string_chk(&argvars[0]);
11267 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011268 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011269 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011272 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 regname = (strregname == NULL ? '"' : *strregname);
11274 if (regname == 0)
11275 regname = '"';
11276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011277 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011278 rettv->vval.v_string = error ? NULL :
11279 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280}
11281
11282/*
11283 * "getregtype()" function
11284 */
11285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011287 typval_T *argvars;
11288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289{
11290 char_u *strregname;
11291 int regname;
11292 char_u buf[NUMBUFLEN + 2];
11293 long reglen = 0;
11294
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011295 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011296 {
11297 strregname = get_tv_string_chk(&argvars[0]);
11298 if (strregname == NULL) /* type error; errmsg already given */
11299 {
11300 rettv->v_type = VAR_STRING;
11301 rettv->vval.v_string = NULL;
11302 return;
11303 }
11304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 else
11306 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011307 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308
11309 regname = (strregname == NULL ? '"' : *strregname);
11310 if (regname == 0)
11311 regname = '"';
11312
11313 buf[0] = NUL;
11314 buf[1] = NUL;
11315 switch (get_reg_type(regname, &reglen))
11316 {
11317 case MLINE: buf[0] = 'V'; break;
11318 case MCHAR: buf[0] = 'v'; break;
11319#ifdef FEAT_VISUAL
11320 case MBLOCK:
11321 buf[0] = Ctrl_V;
11322 sprintf((char *)buf + 1, "%ld", reglen + 1);
11323 break;
11324#endif
11325 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011326 rettv->v_type = VAR_STRING;
11327 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328}
11329
11330/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011331 * "gettabwinvar()" function
11332 */
11333 static void
11334f_gettabwinvar(argvars, rettv)
11335 typval_T *argvars;
11336 typval_T *rettv;
11337{
11338 getwinvar(argvars, rettv, 1);
11339}
11340
11341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 * "getwinposx()" function
11343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011345f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011346 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350#ifdef FEAT_GUI
11351 if (gui.in_use)
11352 {
11353 int x, y;
11354
11355 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011356 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357 }
11358#endif
11359}
11360
11361/*
11362 * "getwinposy()" function
11363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011365f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011366 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011369 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370#ifdef FEAT_GUI
11371 if (gui.in_use)
11372 {
11373 int x, y;
11374
11375 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377 }
11378#endif
11379}
11380
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011381/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011382 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011383 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011384 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011385find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011386 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011387 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011388{
11389#ifdef FEAT_WINDOWS
11390 win_T *wp;
11391#endif
11392 int nr;
11393
11394 nr = get_tv_number_chk(vp, NULL);
11395
11396#ifdef FEAT_WINDOWS
11397 if (nr < 0)
11398 return NULL;
11399 if (nr == 0)
11400 return curwin;
11401
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011402 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11403 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011404 if (--nr <= 0)
11405 break;
11406 return wp;
11407#else
11408 if (nr == 0 || nr == 1)
11409 return curwin;
11410 return NULL;
11411#endif
11412}
11413
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414/*
11415 * "getwinvar()" function
11416 */
11417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011419 typval_T *argvars;
11420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011422 getwinvar(argvars, rettv, 0);
11423}
11424
11425/*
11426 * getwinvar() and gettabwinvar()
11427 */
11428 static void
11429getwinvar(argvars, rettv, off)
11430 typval_T *argvars;
11431 typval_T *rettv;
11432 int off; /* 1 for gettabwinvar() */
11433{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 win_T *win, *oldcurwin;
11435 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011436 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011437 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011439#ifdef FEAT_WINDOWS
11440 if (off == 1)
11441 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11442 else
11443 tp = curtab;
11444#endif
11445 win = find_win_by_nr(&argvars[off], tp);
11446 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011447 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 rettv->v_type = VAR_STRING;
11450 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451
11452 if (win != NULL && varname != NULL)
11453 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011454 /* Set curwin to be our win, temporarily. Also set curbuf, so
11455 * that we can get buffer-local options. */
11456 oldcurwin = curwin;
11457 curwin = win;
11458 curbuf = win->w_buffer;
11459
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011461 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462 else
11463 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011464 if (*varname == NUL)
11465 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11466 * scope prefix before the NUL byte is required by
11467 * find_var_in_ht(). */
11468 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011470 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011472 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011474
11475 /* restore previous notion of curwin */
11476 curwin = oldcurwin;
11477 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478 }
11479
11480 --emsg_off;
11481}
11482
11483/*
11484 * "glob()" function
11485 */
11486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011487f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011488 typval_T *argvars;
11489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011491 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011493 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011495 /* When the optional second argument is non-zero, don't remove matches
11496 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11497 if (argvars[1].v_type != VAR_UNKNOWN
11498 && get_tv_number_chk(&argvars[1], &error))
11499 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011501 if (!error)
11502 {
11503 ExpandInit(&xpc);
11504 xpc.xp_context = EXPAND_FILES;
11505 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11506 NULL, flags, WILD_ALL);
11507 }
11508 else
11509 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510}
11511
11512/*
11513 * "globpath()" function
11514 */
11515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011516f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011517 typval_T *argvars;
11518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011520 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011522 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011523 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011525 /* When the optional second argument is non-zero, don't remove matches
11526 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11527 if (argvars[2].v_type != VAR_UNKNOWN
11528 && get_tv_number_chk(&argvars[2], &error))
11529 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011530 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011531 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011532 rettv->vval.v_string = NULL;
11533 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011534 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11535 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536}
11537
11538/*
11539 * "has()" function
11540 */
11541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011542f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011543 typval_T *argvars;
11544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545{
11546 int i;
11547 char_u *name;
11548 int n = FALSE;
11549 static char *(has_list[]) =
11550 {
11551#ifdef AMIGA
11552 "amiga",
11553# ifdef FEAT_ARP
11554 "arp",
11555# endif
11556#endif
11557#ifdef __BEOS__
11558 "beos",
11559#endif
11560#ifdef MSDOS
11561# ifdef DJGPP
11562 "dos32",
11563# else
11564 "dos16",
11565# endif
11566#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011567#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568 "mac",
11569#endif
11570#if defined(MACOS_X_UNIX)
11571 "macunix",
11572#endif
11573#ifdef OS2
11574 "os2",
11575#endif
11576#ifdef __QNX__
11577 "qnx",
11578#endif
11579#ifdef RISCOS
11580 "riscos",
11581#endif
11582#ifdef UNIX
11583 "unix",
11584#endif
11585#ifdef VMS
11586 "vms",
11587#endif
11588#ifdef WIN16
11589 "win16",
11590#endif
11591#ifdef WIN32
11592 "win32",
11593#endif
11594#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11595 "win32unix",
11596#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011597#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598 "win64",
11599#endif
11600#ifdef EBCDIC
11601 "ebcdic",
11602#endif
11603#ifndef CASE_INSENSITIVE_FILENAME
11604 "fname_case",
11605#endif
11606#ifdef FEAT_ARABIC
11607 "arabic",
11608#endif
11609#ifdef FEAT_AUTOCMD
11610 "autocmd",
11611#endif
11612#ifdef FEAT_BEVAL
11613 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011614# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11615 "balloon_multiline",
11616# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617#endif
11618#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11619 "builtin_terms",
11620# ifdef ALL_BUILTIN_TCAPS
11621 "all_builtin_terms",
11622# endif
11623#endif
11624#ifdef FEAT_BYTEOFF
11625 "byte_offset",
11626#endif
11627#ifdef FEAT_CINDENT
11628 "cindent",
11629#endif
11630#ifdef FEAT_CLIENTSERVER
11631 "clientserver",
11632#endif
11633#ifdef FEAT_CLIPBOARD
11634 "clipboard",
11635#endif
11636#ifdef FEAT_CMDL_COMPL
11637 "cmdline_compl",
11638#endif
11639#ifdef FEAT_CMDHIST
11640 "cmdline_hist",
11641#endif
11642#ifdef FEAT_COMMENTS
11643 "comments",
11644#endif
11645#ifdef FEAT_CRYPT
11646 "cryptv",
11647#endif
11648#ifdef FEAT_CSCOPE
11649 "cscope",
11650#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011651#ifdef CURSOR_SHAPE
11652 "cursorshape",
11653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654#ifdef DEBUG
11655 "debug",
11656#endif
11657#ifdef FEAT_CON_DIALOG
11658 "dialog_con",
11659#endif
11660#ifdef FEAT_GUI_DIALOG
11661 "dialog_gui",
11662#endif
11663#ifdef FEAT_DIFF
11664 "diff",
11665#endif
11666#ifdef FEAT_DIGRAPHS
11667 "digraphs",
11668#endif
11669#ifdef FEAT_DND
11670 "dnd",
11671#endif
11672#ifdef FEAT_EMACS_TAGS
11673 "emacs_tags",
11674#endif
11675 "eval", /* always present, of course! */
11676#ifdef FEAT_EX_EXTRA
11677 "ex_extra",
11678#endif
11679#ifdef FEAT_SEARCH_EXTRA
11680 "extra_search",
11681#endif
11682#ifdef FEAT_FKMAP
11683 "farsi",
11684#endif
11685#ifdef FEAT_SEARCHPATH
11686 "file_in_path",
11687#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011688#if defined(UNIX) && !defined(USE_SYSTEM)
11689 "filterpipe",
11690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691#ifdef FEAT_FIND_ID
11692 "find_in_path",
11693#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011694#ifdef FEAT_FLOAT
11695 "float",
11696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697#ifdef FEAT_FOLDING
11698 "folding",
11699#endif
11700#ifdef FEAT_FOOTER
11701 "footer",
11702#endif
11703#if !defined(USE_SYSTEM) && defined(UNIX)
11704 "fork",
11705#endif
11706#ifdef FEAT_GETTEXT
11707 "gettext",
11708#endif
11709#ifdef FEAT_GUI
11710 "gui",
11711#endif
11712#ifdef FEAT_GUI_ATHENA
11713# ifdef FEAT_GUI_NEXTAW
11714 "gui_neXtaw",
11715# else
11716 "gui_athena",
11717# endif
11718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719#ifdef FEAT_GUI_GTK
11720 "gui_gtk",
11721# ifdef HAVE_GTK2
11722 "gui_gtk2",
11723# endif
11724#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011725#ifdef FEAT_GUI_GNOME
11726 "gui_gnome",
11727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728#ifdef FEAT_GUI_MAC
11729 "gui_mac",
11730#endif
11731#ifdef FEAT_GUI_MOTIF
11732 "gui_motif",
11733#endif
11734#ifdef FEAT_GUI_PHOTON
11735 "gui_photon",
11736#endif
11737#ifdef FEAT_GUI_W16
11738 "gui_win16",
11739#endif
11740#ifdef FEAT_GUI_W32
11741 "gui_win32",
11742#endif
11743#ifdef FEAT_HANGULIN
11744 "hangul_input",
11745#endif
11746#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11747 "iconv",
11748#endif
11749#ifdef FEAT_INS_EXPAND
11750 "insert_expand",
11751#endif
11752#ifdef FEAT_JUMPLIST
11753 "jumplist",
11754#endif
11755#ifdef FEAT_KEYMAP
11756 "keymap",
11757#endif
11758#ifdef FEAT_LANGMAP
11759 "langmap",
11760#endif
11761#ifdef FEAT_LIBCALL
11762 "libcall",
11763#endif
11764#ifdef FEAT_LINEBREAK
11765 "linebreak",
11766#endif
11767#ifdef FEAT_LISP
11768 "lispindent",
11769#endif
11770#ifdef FEAT_LISTCMDS
11771 "listcmds",
11772#endif
11773#ifdef FEAT_LOCALMAP
11774 "localmap",
11775#endif
11776#ifdef FEAT_MENU
11777 "menu",
11778#endif
11779#ifdef FEAT_SESSION
11780 "mksession",
11781#endif
11782#ifdef FEAT_MODIFY_FNAME
11783 "modify_fname",
11784#endif
11785#ifdef FEAT_MOUSE
11786 "mouse",
11787#endif
11788#ifdef FEAT_MOUSESHAPE
11789 "mouseshape",
11790#endif
11791#if defined(UNIX) || defined(VMS)
11792# ifdef FEAT_MOUSE_DEC
11793 "mouse_dec",
11794# endif
11795# ifdef FEAT_MOUSE_GPM
11796 "mouse_gpm",
11797# endif
11798# ifdef FEAT_MOUSE_JSB
11799 "mouse_jsbterm",
11800# endif
11801# ifdef FEAT_MOUSE_NET
11802 "mouse_netterm",
11803# endif
11804# ifdef FEAT_MOUSE_PTERM
11805 "mouse_pterm",
11806# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011807# ifdef FEAT_SYSMOUSE
11808 "mouse_sysmouse",
11809# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810# ifdef FEAT_MOUSE_XTERM
11811 "mouse_xterm",
11812# endif
11813#endif
11814#ifdef FEAT_MBYTE
11815 "multi_byte",
11816#endif
11817#ifdef FEAT_MBYTE_IME
11818 "multi_byte_ime",
11819#endif
11820#ifdef FEAT_MULTI_LANG
11821 "multi_lang",
11822#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011823#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011824#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011825 "mzscheme",
11826#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828#ifdef FEAT_OLE
11829 "ole",
11830#endif
11831#ifdef FEAT_OSFILETYPE
11832 "osfiletype",
11833#endif
11834#ifdef FEAT_PATH_EXTRA
11835 "path_extra",
11836#endif
11837#ifdef FEAT_PERL
11838#ifndef DYNAMIC_PERL
11839 "perl",
11840#endif
11841#endif
11842#ifdef FEAT_PYTHON
11843#ifndef DYNAMIC_PYTHON
11844 "python",
11845#endif
11846#endif
11847#ifdef FEAT_POSTSCRIPT
11848 "postscript",
11849#endif
11850#ifdef FEAT_PRINTER
11851 "printer",
11852#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011853#ifdef FEAT_PROFILE
11854 "profile",
11855#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011856#ifdef FEAT_RELTIME
11857 "reltime",
11858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859#ifdef FEAT_QUICKFIX
11860 "quickfix",
11861#endif
11862#ifdef FEAT_RIGHTLEFT
11863 "rightleft",
11864#endif
11865#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11866 "ruby",
11867#endif
11868#ifdef FEAT_SCROLLBIND
11869 "scrollbind",
11870#endif
11871#ifdef FEAT_CMDL_INFO
11872 "showcmd",
11873 "cmdline_info",
11874#endif
11875#ifdef FEAT_SIGNS
11876 "signs",
11877#endif
11878#ifdef FEAT_SMARTINDENT
11879 "smartindent",
11880#endif
11881#ifdef FEAT_SNIFF
11882 "sniff",
11883#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000011884#ifdef STARTUPTIME
11885 "startuptime",
11886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887#ifdef FEAT_STL_OPT
11888 "statusline",
11889#endif
11890#ifdef FEAT_SUN_WORKSHOP
11891 "sun_workshop",
11892#endif
11893#ifdef FEAT_NETBEANS_INTG
11894 "netbeans_intg",
11895#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011896#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011897 "spell",
11898#endif
11899#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900 "syntax",
11901#endif
11902#if defined(USE_SYSTEM) || !defined(UNIX)
11903 "system",
11904#endif
11905#ifdef FEAT_TAG_BINS
11906 "tag_binary",
11907#endif
11908#ifdef FEAT_TAG_OLDSTATIC
11909 "tag_old_static",
11910#endif
11911#ifdef FEAT_TAG_ANYWHITE
11912 "tag_any_white",
11913#endif
11914#ifdef FEAT_TCL
11915# ifndef DYNAMIC_TCL
11916 "tcl",
11917# endif
11918#endif
11919#ifdef TERMINFO
11920 "terminfo",
11921#endif
11922#ifdef FEAT_TERMRESPONSE
11923 "termresponse",
11924#endif
11925#ifdef FEAT_TEXTOBJ
11926 "textobjects",
11927#endif
11928#ifdef HAVE_TGETENT
11929 "tgetent",
11930#endif
11931#ifdef FEAT_TITLE
11932 "title",
11933#endif
11934#ifdef FEAT_TOOLBAR
11935 "toolbar",
11936#endif
11937#ifdef FEAT_USR_CMDS
11938 "user-commands", /* was accidentally included in 5.4 */
11939 "user_commands",
11940#endif
11941#ifdef FEAT_VIMINFO
11942 "viminfo",
11943#endif
11944#ifdef FEAT_VERTSPLIT
11945 "vertsplit",
11946#endif
11947#ifdef FEAT_VIRTUALEDIT
11948 "virtualedit",
11949#endif
11950#ifdef FEAT_VISUAL
11951 "visual",
11952#endif
11953#ifdef FEAT_VISUALEXTRA
11954 "visualextra",
11955#endif
11956#ifdef FEAT_VREPLACE
11957 "vreplace",
11958#endif
11959#ifdef FEAT_WILDIGN
11960 "wildignore",
11961#endif
11962#ifdef FEAT_WILDMENU
11963 "wildmenu",
11964#endif
11965#ifdef FEAT_WINDOWS
11966 "windows",
11967#endif
11968#ifdef FEAT_WAK
11969 "winaltkeys",
11970#endif
11971#ifdef FEAT_WRITEBACKUP
11972 "writebackup",
11973#endif
11974#ifdef FEAT_XIM
11975 "xim",
11976#endif
11977#ifdef FEAT_XFONTSET
11978 "xfontset",
11979#endif
11980#ifdef USE_XSMP
11981 "xsmp",
11982#endif
11983#ifdef USE_XSMP_INTERACT
11984 "xsmp_interact",
11985#endif
11986#ifdef FEAT_XCLIPBOARD
11987 "xterm_clipboard",
11988#endif
11989#ifdef FEAT_XTERM_SAVE
11990 "xterm_save",
11991#endif
11992#if defined(UNIX) && defined(FEAT_X11)
11993 "X11",
11994#endif
11995 NULL
11996 };
11997
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011998 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999 for (i = 0; has_list[i] != NULL; ++i)
12000 if (STRICMP(name, has_list[i]) == 0)
12001 {
12002 n = TRUE;
12003 break;
12004 }
12005
12006 if (n == FALSE)
12007 {
12008 if (STRNICMP(name, "patch", 5) == 0)
12009 n = has_patch(atoi((char *)name + 5));
12010 else if (STRICMP(name, "vim_starting") == 0)
12011 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012012#ifdef FEAT_MBYTE
12013 else if (STRICMP(name, "multi_byte_encoding") == 0)
12014 n = has_mbyte;
12015#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012016#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12017 else if (STRICMP(name, "balloon_multiline") == 0)
12018 n = multiline_balloon_available();
12019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020#ifdef DYNAMIC_TCL
12021 else if (STRICMP(name, "tcl") == 0)
12022 n = tcl_enabled(FALSE);
12023#endif
12024#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12025 else if (STRICMP(name, "iconv") == 0)
12026 n = iconv_enabled(FALSE);
12027#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012028#ifdef DYNAMIC_MZSCHEME
12029 else if (STRICMP(name, "mzscheme") == 0)
12030 n = mzscheme_enabled(FALSE);
12031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032#ifdef DYNAMIC_RUBY
12033 else if (STRICMP(name, "ruby") == 0)
12034 n = ruby_enabled(FALSE);
12035#endif
12036#ifdef DYNAMIC_PYTHON
12037 else if (STRICMP(name, "python") == 0)
12038 n = python_enabled(FALSE);
12039#endif
12040#ifdef DYNAMIC_PERL
12041 else if (STRICMP(name, "perl") == 0)
12042 n = perl_enabled(FALSE);
12043#endif
12044#ifdef FEAT_GUI
12045 else if (STRICMP(name, "gui_running") == 0)
12046 n = (gui.in_use || gui.starting);
12047# ifdef FEAT_GUI_W32
12048 else if (STRICMP(name, "gui_win32s") == 0)
12049 n = gui_is_win32s();
12050# endif
12051# ifdef FEAT_BROWSE
12052 else if (STRICMP(name, "browse") == 0)
12053 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12054# endif
12055#endif
12056#ifdef FEAT_SYN_HL
12057 else if (STRICMP(name, "syntax_items") == 0)
12058 n = syntax_present(curbuf);
12059#endif
12060#if defined(WIN3264)
12061 else if (STRICMP(name, "win95") == 0)
12062 n = mch_windows95();
12063#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012064#ifdef FEAT_NETBEANS_INTG
12065 else if (STRICMP(name, "netbeans_enabled") == 0)
12066 n = usingNetbeans;
12067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068 }
12069
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012070 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071}
12072
12073/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012074 * "has_key()" function
12075 */
12076 static void
12077f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012078 typval_T *argvars;
12079 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012080{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012081 if (argvars[0].v_type != VAR_DICT)
12082 {
12083 EMSG(_(e_dictreq));
12084 return;
12085 }
12086 if (argvars[0].vval.v_dict == NULL)
12087 return;
12088
12089 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012090 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012091}
12092
12093/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012094 * "haslocaldir()" function
12095 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012096 static void
12097f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012098 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012099 typval_T *rettv;
12100{
12101 rettv->vval.v_number = (curwin->w_localdir != NULL);
12102}
12103
12104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105 * "hasmapto()" function
12106 */
12107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012108f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012109 typval_T *argvars;
12110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111{
12112 char_u *name;
12113 char_u *mode;
12114 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012115 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012117 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012118 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119 mode = (char_u *)"nvo";
12120 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012122 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012123 if (argvars[2].v_type != VAR_UNKNOWN)
12124 abbr = get_tv_number(&argvars[2]);
12125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126
Bram Moolenaar2c932302006-03-18 21:42:09 +000012127 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012128 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012129 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012130 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131}
12132
12133/*
12134 * "histadd()" function
12135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012137f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012138 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140{
12141#ifdef FEAT_CMDHIST
12142 int histype;
12143 char_u *str;
12144 char_u buf[NUMBUFLEN];
12145#endif
12146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012147 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148 if (check_restricted() || check_secure())
12149 return;
12150#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012151 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12152 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 if (histype >= 0)
12154 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012155 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156 if (*str != NUL)
12157 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012158 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161 return;
12162 }
12163 }
12164#endif
12165}
12166
12167/*
12168 * "histdel()" function
12169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012171f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012172 typval_T *argvars UNUSED;
12173 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174{
12175#ifdef FEAT_CMDHIST
12176 int n;
12177 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012178 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012180 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12181 if (str == NULL)
12182 n = 0;
12183 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012185 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012186 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012188 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012189 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190 else
12191 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012192 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012193 get_tv_string_buf(&argvars[1], buf));
12194 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195#endif
12196}
12197
12198/*
12199 * "histget()" function
12200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012202f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012203 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205{
12206#ifdef FEAT_CMDHIST
12207 int type;
12208 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012209 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012211 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12212 if (str == NULL)
12213 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012215 {
12216 type = get_histtype(str);
12217 if (argvars[1].v_type == VAR_UNKNOWN)
12218 idx = get_history_idx(type);
12219 else
12220 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12221 /* -1 on type error */
12222 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012225 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012227 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228}
12229
12230/*
12231 * "histnr()" function
12232 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012234f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012235 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237{
12238 int i;
12239
12240#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012241 char_u *history = get_tv_string_chk(&argvars[0]);
12242
12243 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244 if (i >= HIST_CMD && i < HIST_COUNT)
12245 i = get_history_idx(i);
12246 else
12247#endif
12248 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012249 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250}
12251
12252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253 * "highlightID(name)" function
12254 */
12255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012256f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012257 typval_T *argvars;
12258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012260 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261}
12262
12263/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012264 * "highlight_exists()" function
12265 */
12266 static void
12267f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012268 typval_T *argvars;
12269 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012270{
12271 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12272}
12273
12274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275 * "hostname()" function
12276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012278f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012279 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281{
12282 char_u hostname[256];
12283
12284 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012285 rettv->v_type = VAR_STRING;
12286 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287}
12288
12289/*
12290 * iconv() function
12291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012293f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012294 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296{
12297#ifdef FEAT_MBYTE
12298 char_u buf1[NUMBUFLEN];
12299 char_u buf2[NUMBUFLEN];
12300 char_u *from, *to, *str;
12301 vimconv_T vimconv;
12302#endif
12303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012304 rettv->v_type = VAR_STRING;
12305 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306
12307#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012308 str = get_tv_string(&argvars[0]);
12309 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12310 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311 vimconv.vc_type = CONV_NONE;
12312 convert_setup(&vimconv, from, to);
12313
12314 /* If the encodings are equal, no conversion needed. */
12315 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012316 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012318 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319
12320 convert_setup(&vimconv, NULL, NULL);
12321 vim_free(from);
12322 vim_free(to);
12323#endif
12324}
12325
12326/*
12327 * "indent()" function
12328 */
12329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012330f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012331 typval_T *argvars;
12332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333{
12334 linenr_T lnum;
12335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012336 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012340 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341}
12342
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012343/*
12344 * "index()" function
12345 */
12346 static void
12347f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012348 typval_T *argvars;
12349 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012350{
Bram Moolenaar33570922005-01-25 22:26:29 +000012351 list_T *l;
12352 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012353 long idx = 0;
12354 int ic = FALSE;
12355
12356 rettv->vval.v_number = -1;
12357 if (argvars[0].v_type != VAR_LIST)
12358 {
12359 EMSG(_(e_listreq));
12360 return;
12361 }
12362 l = argvars[0].vval.v_list;
12363 if (l != NULL)
12364 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012365 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012366 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012368 int error = FALSE;
12369
Bram Moolenaar758711c2005-02-02 23:11:38 +000012370 /* Start at specified item. Use the cached index that list_find()
12371 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012372 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012373 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012374 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012375 ic = get_tv_number_chk(&argvars[3], &error);
12376 if (error)
12377 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012378 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012379
Bram Moolenaar758711c2005-02-02 23:11:38 +000012380 for ( ; item != NULL; item = item->li_next, ++idx)
12381 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012382 {
12383 rettv->vval.v_number = idx;
12384 break;
12385 }
12386 }
12387}
12388
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389static int inputsecret_flag = 0;
12390
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012391static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12392
Bram Moolenaar071d4272004-06-13 20:20:40 +000012393/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012394 * This function is used by f_input() and f_inputdialog() functions. The third
12395 * argument to f_input() specifies the type of completion to use at the
12396 * prompt. The third argument to f_inputdialog() specifies the value to return
12397 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398 */
12399 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012400get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012401 typval_T *argvars;
12402 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012403 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012405 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406 char_u *p = NULL;
12407 int c;
12408 char_u buf[NUMBUFLEN];
12409 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012410 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012411 int xp_type = EXPAND_NOTHING;
12412 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012414 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012415 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416
12417#ifdef NO_CONSOLE_INPUT
12418 /* While starting up, there is no place to enter text. */
12419 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421#endif
12422
12423 cmd_silent = FALSE; /* Want to see the prompt. */
12424 if (prompt != NULL)
12425 {
12426 /* Only the part of the message after the last NL is considered as
12427 * prompt for the command line */
12428 p = vim_strrchr(prompt, '\n');
12429 if (p == NULL)
12430 p = prompt;
12431 else
12432 {
12433 ++p;
12434 c = *p;
12435 *p = NUL;
12436 msg_start();
12437 msg_clr_eos();
12438 msg_puts_attr(prompt, echo_attr);
12439 msg_didout = FALSE;
12440 msg_starthere();
12441 *p = c;
12442 }
12443 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012445 if (argvars[1].v_type != VAR_UNKNOWN)
12446 {
12447 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12448 if (defstr != NULL)
12449 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012451 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012452 {
12453 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012454 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012455 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012456
Bram Moolenaar4463f292005-09-25 22:20:24 +000012457 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012458
Bram Moolenaar4463f292005-09-25 22:20:24 +000012459 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12460 if (xp_name == NULL)
12461 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012462
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012463 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012464
Bram Moolenaar4463f292005-09-25 22:20:24 +000012465 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12466 &xp_arg) == FAIL)
12467 return;
12468 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012469 }
12470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012471 if (defstr != NULL)
12472 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012473 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12474 xp_type, xp_arg);
12475
12476 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012478 /* since the user typed this, no need to wait for return */
12479 need_wait_return = FALSE;
12480 msg_didout = FALSE;
12481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482 cmd_silent = cmd_silent_save;
12483}
12484
12485/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012486 * "input()" function
12487 * Also handles inputsecret() when inputsecret is set.
12488 */
12489 static void
12490f_input(argvars, rettv)
12491 typval_T *argvars;
12492 typval_T *rettv;
12493{
12494 get_user_input(argvars, rettv, FALSE);
12495}
12496
12497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498 * "inputdialog()" function
12499 */
12500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012501f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012502 typval_T *argvars;
12503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504{
12505#if defined(FEAT_GUI_TEXTDIALOG)
12506 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12507 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12508 {
12509 char_u *message;
12510 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012511 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012513 message = get_tv_string_chk(&argvars[0]);
12514 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012515 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012516 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517 else
12518 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012519 if (message != NULL && defstr != NULL
12520 && do_dialog(VIM_QUESTION, NULL, message,
12521 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012522 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523 else
12524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012525 if (message != NULL && defstr != NULL
12526 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012527 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012528 rettv->vval.v_string = vim_strsave(
12529 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012530 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012531 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012533 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534 }
12535 else
12536#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012537 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012538}
12539
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012540/*
12541 * "inputlist()" function
12542 */
12543 static void
12544f_inputlist(argvars, rettv)
12545 typval_T *argvars;
12546 typval_T *rettv;
12547{
12548 listitem_T *li;
12549 int selected;
12550 int mouse_used;
12551
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012552#ifdef NO_CONSOLE_INPUT
12553 /* While starting up, there is no place to enter text. */
12554 if (no_console_input())
12555 return;
12556#endif
12557 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12558 {
12559 EMSG2(_(e_listarg), "inputlist()");
12560 return;
12561 }
12562
12563 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012564 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012565 lines_left = Rows; /* avoid more prompt */
12566 msg_scroll = TRUE;
12567 msg_clr_eos();
12568
12569 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12570 {
12571 msg_puts(get_tv_string(&li->li_tv));
12572 msg_putchar('\n');
12573 }
12574
12575 /* Ask for choice. */
12576 selected = prompt_for_number(&mouse_used);
12577 if (mouse_used)
12578 selected -= lines_left;
12579
12580 rettv->vval.v_number = selected;
12581}
12582
12583
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12585
12586/*
12587 * "inputrestore()" function
12588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012590f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012591 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012593{
12594 if (ga_userinput.ga_len > 0)
12595 {
12596 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12598 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012599 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600 }
12601 else if (p_verbose > 1)
12602 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012603 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012604 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605 }
12606}
12607
12608/*
12609 * "inputsave()" function
12610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012612f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012613 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012616 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617 if (ga_grow(&ga_userinput, 1) == OK)
12618 {
12619 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12620 + ga_userinput.ga_len);
12621 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012622 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012623 }
12624 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012625 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626}
12627
12628/*
12629 * "inputsecret()" function
12630 */
12631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012632f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012633 typval_T *argvars;
12634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635{
12636 ++cmdline_star;
12637 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012638 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639 --cmdline_star;
12640 --inputsecret_flag;
12641}
12642
12643/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012644 * "insert()" function
12645 */
12646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012647f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012648 typval_T *argvars;
12649 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012650{
12651 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012652 listitem_T *item;
12653 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012654 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012655
12656 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012657 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012658 else if ((l = argvars[0].vval.v_list) != NULL
12659 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012660 {
12661 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012662 before = get_tv_number_chk(&argvars[2], &error);
12663 if (error)
12664 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012665
Bram Moolenaar758711c2005-02-02 23:11:38 +000012666 if (before == l->lv_len)
12667 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012668 else
12669 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012670 item = list_find(l, before);
12671 if (item == NULL)
12672 {
12673 EMSGN(_(e_listidx), before);
12674 l = NULL;
12675 }
12676 }
12677 if (l != NULL)
12678 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012679 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012680 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012681 }
12682 }
12683}
12684
12685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686 * "isdirectory()" function
12687 */
12688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012689f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012690 typval_T *argvars;
12691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012693 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694}
12695
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012696/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012697 * "islocked()" function
12698 */
12699 static void
12700f_islocked(argvars, rettv)
12701 typval_T *argvars;
12702 typval_T *rettv;
12703{
12704 lval_T lv;
12705 char_u *end;
12706 dictitem_T *di;
12707
12708 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012709 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12710 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012711 if (end != NULL && lv.ll_name != NULL)
12712 {
12713 if (*end != NUL)
12714 EMSG(_(e_trailing));
12715 else
12716 {
12717 if (lv.ll_tv == NULL)
12718 {
12719 if (check_changedtick(lv.ll_name))
12720 rettv->vval.v_number = 1; /* always locked */
12721 else
12722 {
12723 di = find_var(lv.ll_name, NULL);
12724 if (di != NULL)
12725 {
12726 /* Consider a variable locked when:
12727 * 1. the variable itself is locked
12728 * 2. the value of the variable is locked.
12729 * 3. the List or Dict value is locked.
12730 */
12731 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12732 || tv_islocked(&di->di_tv));
12733 }
12734 }
12735 }
12736 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012737 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012738 else if (lv.ll_newkey != NULL)
12739 EMSG2(_(e_dictkey), lv.ll_newkey);
12740 else if (lv.ll_list != NULL)
12741 /* List item. */
12742 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12743 else
12744 /* Dictionary item. */
12745 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12746 }
12747 }
12748
12749 clear_lval(&lv);
12750}
12751
Bram Moolenaar33570922005-01-25 22:26:29 +000012752static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012753
12754/*
12755 * Turn a dict into a list:
12756 * "what" == 0: list of keys
12757 * "what" == 1: list of values
12758 * "what" == 2: list of items
12759 */
12760 static void
12761dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012762 typval_T *argvars;
12763 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012764 int what;
12765{
Bram Moolenaar33570922005-01-25 22:26:29 +000012766 list_T *l2;
12767 dictitem_T *di;
12768 hashitem_T *hi;
12769 listitem_T *li;
12770 listitem_T *li2;
12771 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012772 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012773
Bram Moolenaar8c711452005-01-14 21:53:12 +000012774 if (argvars[0].v_type != VAR_DICT)
12775 {
12776 EMSG(_(e_dictreq));
12777 return;
12778 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012779 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012780 return;
12781
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012782 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012783 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012784
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012785 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012786 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012787 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012788 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012789 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012790 --todo;
12791 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012792
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012793 li = listitem_alloc();
12794 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012795 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012796 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012797
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012798 if (what == 0)
12799 {
12800 /* keys() */
12801 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012802 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012803 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12804 }
12805 else if (what == 1)
12806 {
12807 /* values() */
12808 copy_tv(&di->di_tv, &li->li_tv);
12809 }
12810 else
12811 {
12812 /* items() */
12813 l2 = list_alloc();
12814 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012815 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012816 li->li_tv.vval.v_list = l2;
12817 if (l2 == NULL)
12818 break;
12819 ++l2->lv_refcount;
12820
12821 li2 = listitem_alloc();
12822 if (li2 == NULL)
12823 break;
12824 list_append(l2, li2);
12825 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012826 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012827 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12828
12829 li2 = listitem_alloc();
12830 if (li2 == NULL)
12831 break;
12832 list_append(l2, li2);
12833 copy_tv(&di->di_tv, &li2->li_tv);
12834 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012835 }
12836 }
12837}
12838
12839/*
12840 * "items(dict)" function
12841 */
12842 static void
12843f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012844 typval_T *argvars;
12845 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012846{
12847 dict_list(argvars, rettv, 2);
12848}
12849
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012851 * "join()" function
12852 */
12853 static void
12854f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012855 typval_T *argvars;
12856 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012857{
12858 garray_T ga;
12859 char_u *sep;
12860
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012861 if (argvars[0].v_type != VAR_LIST)
12862 {
12863 EMSG(_(e_listreq));
12864 return;
12865 }
12866 if (argvars[0].vval.v_list == NULL)
12867 return;
12868 if (argvars[1].v_type == VAR_UNKNOWN)
12869 sep = (char_u *)" ";
12870 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012871 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012872
12873 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012874
12875 if (sep != NULL)
12876 {
12877 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012878 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012879 ga_append(&ga, NUL);
12880 rettv->vval.v_string = (char_u *)ga.ga_data;
12881 }
12882 else
12883 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012884}
12885
12886/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012887 * "keys()" function
12888 */
12889 static void
12890f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012891 typval_T *argvars;
12892 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012893{
12894 dict_list(argvars, rettv, 0);
12895}
12896
12897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898 * "last_buffer_nr()" function.
12899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012901f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012902 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904{
12905 int n = 0;
12906 buf_T *buf;
12907
12908 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12909 if (n < buf->b_fnum)
12910 n = buf->b_fnum;
12911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012912 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012913}
12914
12915/*
12916 * "len()" function
12917 */
12918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012919f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012920 typval_T *argvars;
12921 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012922{
12923 switch (argvars[0].v_type)
12924 {
12925 case VAR_STRING:
12926 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012927 rettv->vval.v_number = (varnumber_T)STRLEN(
12928 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012929 break;
12930 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012931 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012932 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012933 case VAR_DICT:
12934 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12935 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012936 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012937 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012938 break;
12939 }
12940}
12941
Bram Moolenaar33570922005-01-25 22:26:29 +000012942static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012943
12944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012945libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012946 typval_T *argvars;
12947 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012948 int type;
12949{
12950#ifdef FEAT_LIBCALL
12951 char_u *string_in;
12952 char_u **string_result;
12953 int nr_result;
12954#endif
12955
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012956 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012957 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012958 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012959
12960 if (check_restricted() || check_secure())
12961 return;
12962
12963#ifdef FEAT_LIBCALL
12964 /* The first two args must be strings, otherwise its meaningless */
12965 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12966 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012967 string_in = NULL;
12968 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012969 string_in = argvars[2].vval.v_string;
12970 if (type == VAR_NUMBER)
12971 string_result = NULL;
12972 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012973 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012974 if (mch_libcall(argvars[0].vval.v_string,
12975 argvars[1].vval.v_string,
12976 string_in,
12977 argvars[2].vval.v_number,
12978 string_result,
12979 &nr_result) == OK
12980 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012981 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012982 }
12983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984}
12985
12986/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012987 * "libcall()" function
12988 */
12989 static void
12990f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012991 typval_T *argvars;
12992 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012993{
12994 libcall_common(argvars, rettv, VAR_STRING);
12995}
12996
12997/*
12998 * "libcallnr()" function
12999 */
13000 static void
13001f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013002 typval_T *argvars;
13003 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013004{
13005 libcall_common(argvars, rettv, VAR_NUMBER);
13006}
13007
13008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009 * "line(string)" function
13010 */
13011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013012f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013013 typval_T *argvars;
13014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013015{
13016 linenr_T lnum = 0;
13017 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013018 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013020 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021 if (fp != NULL)
13022 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013023 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024}
13025
13026/*
13027 * "line2byte(lnum)" function
13028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013030f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013031 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033{
13034#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013035 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036#else
13037 linenr_T lnum;
13038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013039 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013041 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13044 if (rettv->vval.v_number >= 0)
13045 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046#endif
13047}
13048
13049/*
13050 * "lispindent(lnum)" function
13051 */
13052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013053f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013054 typval_T *argvars;
13055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056{
13057#ifdef FEAT_LISP
13058 pos_T pos;
13059 linenr_T lnum;
13060
13061 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013062 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13064 {
13065 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013066 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067 curwin->w_cursor = pos;
13068 }
13069 else
13070#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013071 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013072}
13073
13074/*
13075 * "localtime()" function
13076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013078f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013079 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013082 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083}
13084
Bram Moolenaar33570922005-01-25 22:26:29 +000013085static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086
13087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013088get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013089 typval_T *argvars;
13090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091 int exact;
13092{
13093 char_u *keys;
13094 char_u *which;
13095 char_u buf[NUMBUFLEN];
13096 char_u *keys_buf = NULL;
13097 char_u *rhs;
13098 int mode;
13099 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013100 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013101
13102 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013103 rettv->v_type = VAR_STRING;
13104 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013106 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 if (*keys == NUL)
13108 return;
13109
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013110 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013111 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013112 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013113 if (argvars[2].v_type != VAR_UNKNOWN)
13114 abbr = get_tv_number(&argvars[2]);
13115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116 else
13117 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013118 if (which == NULL)
13119 return;
13120
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121 mode = get_map_mode(&which, 0);
13122
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013123 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013124 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125 vim_free(keys_buf);
13126 if (rhs != NULL)
13127 {
13128 ga_init(&ga);
13129 ga.ga_itemsize = 1;
13130 ga.ga_growsize = 40;
13131
13132 while (*rhs != NUL)
13133 ga_concat(&ga, str2special(&rhs, FALSE));
13134
13135 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013136 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137 }
13138}
13139
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013140#ifdef FEAT_FLOAT
13141/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013142 * "log()" function
13143 */
13144 static void
13145f_log(argvars, rettv)
13146 typval_T *argvars;
13147 typval_T *rettv;
13148{
13149 float_T f;
13150
13151 rettv->v_type = VAR_FLOAT;
13152 if (get_float_arg(argvars, &f) == OK)
13153 rettv->vval.v_float = log(f);
13154 else
13155 rettv->vval.v_float = 0.0;
13156}
13157
13158/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013159 * "log10()" function
13160 */
13161 static void
13162f_log10(argvars, rettv)
13163 typval_T *argvars;
13164 typval_T *rettv;
13165{
13166 float_T f;
13167
13168 rettv->v_type = VAR_FLOAT;
13169 if (get_float_arg(argvars, &f) == OK)
13170 rettv->vval.v_float = log10(f);
13171 else
13172 rettv->vval.v_float = 0.0;
13173}
13174#endif
13175
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013177 * "map()" function
13178 */
13179 static void
13180f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013181 typval_T *argvars;
13182 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013183{
13184 filter_map(argvars, rettv, TRUE);
13185}
13186
13187/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013188 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189 */
13190 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013191f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013192 typval_T *argvars;
13193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013195 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196}
13197
13198/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013199 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013200 */
13201 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013202f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013203 typval_T *argvars;
13204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013206 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207}
13208
Bram Moolenaar33570922005-01-25 22:26:29 +000013209static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210
13211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013212find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013213 typval_T *argvars;
13214 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215 int type;
13216{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013217 char_u *str = NULL;
13218 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 char_u *pat;
13220 regmatch_T regmatch;
13221 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013222 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223 char_u *save_cpo;
13224 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013225 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013226 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013227 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013228 list_T *l = NULL;
13229 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013230 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013231 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232
13233 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13234 save_cpo = p_cpo;
13235 p_cpo = (char_u *)"";
13236
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013237 rettv->vval.v_number = -1;
13238 if (type == 3)
13239 {
13240 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013241 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013242 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013243 }
13244 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013246 rettv->v_type = VAR_STRING;
13247 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013250 if (argvars[0].v_type == VAR_LIST)
13251 {
13252 if ((l = argvars[0].vval.v_list) == NULL)
13253 goto theend;
13254 li = l->lv_first;
13255 }
13256 else
13257 expr = str = get_tv_string(&argvars[0]);
13258
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013259 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13260 if (pat == NULL)
13261 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013262
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013263 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013265 int error = FALSE;
13266
13267 start = get_tv_number_chk(&argvars[2], &error);
13268 if (error)
13269 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013270 if (l != NULL)
13271 {
13272 li = list_find(l, start);
13273 if (li == NULL)
13274 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013275 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013276 }
13277 else
13278 {
13279 if (start < 0)
13280 start = 0;
13281 if (start > (long)STRLEN(str))
13282 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013283 /* When "count" argument is there ignore matches before "start",
13284 * otherwise skip part of the string. Differs when pattern is "^"
13285 * or "\<". */
13286 if (argvars[3].v_type != VAR_UNKNOWN)
13287 startcol = start;
13288 else
13289 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013290 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013291
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013292 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013293 nth = get_tv_number_chk(&argvars[3], &error);
13294 if (error)
13295 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 }
13297
13298 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13299 if (regmatch.regprog != NULL)
13300 {
13301 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013302
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013303 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013304 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013305 if (l != NULL)
13306 {
13307 if (li == NULL)
13308 {
13309 match = FALSE;
13310 break;
13311 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013312 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013313 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013314 if (str == NULL)
13315 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013316 }
13317
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013318 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013319
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013320 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013321 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013322 if (l == NULL && !match)
13323 break;
13324
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013325 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013326 if (l != NULL)
13327 {
13328 li = li->li_next;
13329 ++idx;
13330 }
13331 else
13332 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013333#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013334 startcol = (colnr_T)(regmatch.startp[0]
13335 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013336#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013337 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013338#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013339 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013340 }
13341
13342 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013344 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013345 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013346 int i;
13347
13348 /* return list with matched string and submatches */
13349 for (i = 0; i < NSUBEXP; ++i)
13350 {
13351 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013352 {
13353 if (list_append_string(rettv->vval.v_list,
13354 (char_u *)"", 0) == FAIL)
13355 break;
13356 }
13357 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013358 regmatch.startp[i],
13359 (int)(regmatch.endp[i] - regmatch.startp[i]))
13360 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013361 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013362 }
13363 }
13364 else if (type == 2)
13365 {
13366 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013367 if (l != NULL)
13368 copy_tv(&li->li_tv, rettv);
13369 else
13370 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013372 }
13373 else if (l != NULL)
13374 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375 else
13376 {
13377 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013378 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 (varnumber_T)(regmatch.startp[0] - str);
13380 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013381 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013383 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384 }
13385 }
13386 vim_free(regmatch.regprog);
13387 }
13388
13389theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013390 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391 p_cpo = save_cpo;
13392}
13393
13394/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013395 * "match()" function
13396 */
13397 static void
13398f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013399 typval_T *argvars;
13400 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013401{
13402 find_some_match(argvars, rettv, 1);
13403}
13404
13405/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013406 * "matchadd()" function
13407 */
13408 static void
13409f_matchadd(argvars, rettv)
13410 typval_T *argvars;
13411 typval_T *rettv;
13412{
13413#ifdef FEAT_SEARCH_EXTRA
13414 char_u buf[NUMBUFLEN];
13415 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13416 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13417 int prio = 10; /* default priority */
13418 int id = -1;
13419 int error = FALSE;
13420
13421 rettv->vval.v_number = -1;
13422
13423 if (grp == NULL || pat == NULL)
13424 return;
13425 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013426 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013427 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013428 if (argvars[3].v_type != VAR_UNKNOWN)
13429 id = get_tv_number_chk(&argvars[3], &error);
13430 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013431 if (error == TRUE)
13432 return;
13433 if (id >= 1 && id <= 3)
13434 {
13435 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13436 return;
13437 }
13438
13439 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13440#endif
13441}
13442
13443/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013444 * "matcharg()" function
13445 */
13446 static void
13447f_matcharg(argvars, rettv)
13448 typval_T *argvars;
13449 typval_T *rettv;
13450{
13451 if (rettv_list_alloc(rettv) == OK)
13452 {
13453#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013454 int id = get_tv_number(&argvars[0]);
13455 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013456
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013457 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013458 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013459 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13460 {
13461 list_append_string(rettv->vval.v_list,
13462 syn_id2name(m->hlg_id), -1);
13463 list_append_string(rettv->vval.v_list, m->pattern, -1);
13464 }
13465 else
13466 {
13467 list_append_string(rettv->vval.v_list, NUL, -1);
13468 list_append_string(rettv->vval.v_list, NUL, -1);
13469 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013470 }
13471#endif
13472 }
13473}
13474
13475/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013476 * "matchdelete()" function
13477 */
13478 static void
13479f_matchdelete(argvars, rettv)
13480 typval_T *argvars;
13481 typval_T *rettv;
13482{
13483#ifdef FEAT_SEARCH_EXTRA
13484 rettv->vval.v_number = match_delete(curwin,
13485 (int)get_tv_number(&argvars[0]), TRUE);
13486#endif
13487}
13488
13489/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013490 * "matchend()" function
13491 */
13492 static void
13493f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013494 typval_T *argvars;
13495 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013496{
13497 find_some_match(argvars, rettv, 0);
13498}
13499
13500/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013501 * "matchlist()" function
13502 */
13503 static void
13504f_matchlist(argvars, rettv)
13505 typval_T *argvars;
13506 typval_T *rettv;
13507{
13508 find_some_match(argvars, rettv, 3);
13509}
13510
13511/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013512 * "matchstr()" function
13513 */
13514 static void
13515f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013516 typval_T *argvars;
13517 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013518{
13519 find_some_match(argvars, rettv, 2);
13520}
13521
Bram Moolenaar33570922005-01-25 22:26:29 +000013522static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013523
13524 static void
13525max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013526 typval_T *argvars;
13527 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013528 int domax;
13529{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013530 long n = 0;
13531 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013532 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013533
13534 if (argvars[0].v_type == VAR_LIST)
13535 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013536 list_T *l;
13537 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013538
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013539 l = argvars[0].vval.v_list;
13540 if (l != NULL)
13541 {
13542 li = l->lv_first;
13543 if (li != NULL)
13544 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013545 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013546 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013547 {
13548 li = li->li_next;
13549 if (li == NULL)
13550 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013551 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013552 if (domax ? i > n : i < n)
13553 n = i;
13554 }
13555 }
13556 }
13557 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013558 else if (argvars[0].v_type == VAR_DICT)
13559 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013560 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013561 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013562 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013563 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013564
13565 d = argvars[0].vval.v_dict;
13566 if (d != NULL)
13567 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013568 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013569 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013570 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013571 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013572 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013573 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013574 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013575 if (first)
13576 {
13577 n = i;
13578 first = FALSE;
13579 }
13580 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013581 n = i;
13582 }
13583 }
13584 }
13585 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013586 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013587 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013588 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013589}
13590
13591/*
13592 * "max()" function
13593 */
13594 static void
13595f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013596 typval_T *argvars;
13597 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013598{
13599 max_min(argvars, rettv, TRUE);
13600}
13601
13602/*
13603 * "min()" function
13604 */
13605 static void
13606f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013607 typval_T *argvars;
13608 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013609{
13610 max_min(argvars, rettv, FALSE);
13611}
13612
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013613static int mkdir_recurse __ARGS((char_u *dir, int prot));
13614
13615/*
13616 * Create the directory in which "dir" is located, and higher levels when
13617 * needed.
13618 */
13619 static int
13620mkdir_recurse(dir, prot)
13621 char_u *dir;
13622 int prot;
13623{
13624 char_u *p;
13625 char_u *updir;
13626 int r = FAIL;
13627
13628 /* Get end of directory name in "dir".
13629 * We're done when it's "/" or "c:/". */
13630 p = gettail_sep(dir);
13631 if (p <= get_past_head(dir))
13632 return OK;
13633
13634 /* If the directory exists we're done. Otherwise: create it.*/
13635 updir = vim_strnsave(dir, (int)(p - dir));
13636 if (updir == NULL)
13637 return FAIL;
13638 if (mch_isdir(updir))
13639 r = OK;
13640 else if (mkdir_recurse(updir, prot) == OK)
13641 r = vim_mkdir_emsg(updir, prot);
13642 vim_free(updir);
13643 return r;
13644}
13645
13646#ifdef vim_mkdir
13647/*
13648 * "mkdir()" function
13649 */
13650 static void
13651f_mkdir(argvars, rettv)
13652 typval_T *argvars;
13653 typval_T *rettv;
13654{
13655 char_u *dir;
13656 char_u buf[NUMBUFLEN];
13657 int prot = 0755;
13658
13659 rettv->vval.v_number = FAIL;
13660 if (check_restricted() || check_secure())
13661 return;
13662
13663 dir = get_tv_string_buf(&argvars[0], buf);
13664 if (argvars[1].v_type != VAR_UNKNOWN)
13665 {
13666 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013667 prot = get_tv_number_chk(&argvars[2], NULL);
13668 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013669 mkdir_recurse(dir, prot);
13670 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013671 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013672}
13673#endif
13674
Bram Moolenaar0d660222005-01-07 21:51:51 +000013675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676 * "mode()" function
13677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013679f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013680 typval_T *argvars;
13681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013683 char_u buf[3];
13684
13685 buf[1] = NUL;
13686 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687
13688#ifdef FEAT_VISUAL
13689 if (VIsual_active)
13690 {
13691 if (VIsual_select)
13692 buf[0] = VIsual_mode + 's' - 'v';
13693 else
13694 buf[0] = VIsual_mode;
13695 }
13696 else
13697#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013698 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13699 || State == CONFIRM)
13700 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013702 if (State == ASKMORE)
13703 buf[1] = 'm';
13704 else if (State == CONFIRM)
13705 buf[1] = '?';
13706 }
13707 else if (State == EXTERNCMD)
13708 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709 else if (State & INSERT)
13710 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013711#ifdef FEAT_VREPLACE
13712 if (State & VREPLACE_FLAG)
13713 {
13714 buf[0] = 'R';
13715 buf[1] = 'v';
13716 }
13717 else
13718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719 if (State & REPLACE_FLAG)
13720 buf[0] = 'R';
13721 else
13722 buf[0] = 'i';
13723 }
13724 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013725 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013727 if (exmode_active)
13728 buf[1] = 'v';
13729 }
13730 else if (exmode_active)
13731 {
13732 buf[0] = 'c';
13733 buf[1] = 'e';
13734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013736 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013738 if (finish_op)
13739 buf[1] = 'o';
13740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013742 /* Clear out the minor mode when the argument is not a non-zero number or
13743 * non-empty string. */
13744 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013745 buf[1] = NUL;
13746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013747 rettv->vval.v_string = vim_strsave(buf);
13748 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749}
13750
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013751#ifdef FEAT_MZSCHEME
13752/*
13753 * "mzeval()" function
13754 */
13755 static void
13756f_mzeval(argvars, rettv)
13757 typval_T *argvars;
13758 typval_T *rettv;
13759{
13760 char_u *str;
13761 char_u buf[NUMBUFLEN];
13762
13763 str = get_tv_string_buf(&argvars[0], buf);
13764 do_mzeval(str, rettv);
13765}
13766#endif
13767
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013769 * "nextnonblank()" function
13770 */
13771 static void
13772f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013773 typval_T *argvars;
13774 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013775{
13776 linenr_T lnum;
13777
13778 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013780 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013781 {
13782 lnum = 0;
13783 break;
13784 }
13785 if (*skipwhite(ml_get(lnum)) != NUL)
13786 break;
13787 }
13788 rettv->vval.v_number = lnum;
13789}
13790
13791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792 * "nr2char()" function
13793 */
13794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013795f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013796 typval_T *argvars;
13797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798{
13799 char_u buf[NUMBUFLEN];
13800
13801#ifdef FEAT_MBYTE
13802 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013803 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804 else
13805#endif
13806 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013807 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808 buf[1] = NUL;
13809 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013810 rettv->v_type = VAR_STRING;
13811 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013812}
13813
13814/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013815 * "pathshorten()" function
13816 */
13817 static void
13818f_pathshorten(argvars, rettv)
13819 typval_T *argvars;
13820 typval_T *rettv;
13821{
13822 char_u *p;
13823
13824 rettv->v_type = VAR_STRING;
13825 p = get_tv_string_chk(&argvars[0]);
13826 if (p == NULL)
13827 rettv->vval.v_string = NULL;
13828 else
13829 {
13830 p = vim_strsave(p);
13831 rettv->vval.v_string = p;
13832 if (p != NULL)
13833 shorten_dir(p);
13834 }
13835}
13836
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013837#ifdef FEAT_FLOAT
13838/*
13839 * "pow()" function
13840 */
13841 static void
13842f_pow(argvars, rettv)
13843 typval_T *argvars;
13844 typval_T *rettv;
13845{
13846 float_T fx, fy;
13847
13848 rettv->v_type = VAR_FLOAT;
13849 if (get_float_arg(argvars, &fx) == OK
13850 && get_float_arg(&argvars[1], &fy) == OK)
13851 rettv->vval.v_float = pow(fx, fy);
13852 else
13853 rettv->vval.v_float = 0.0;
13854}
13855#endif
13856
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013857/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013858 * "prevnonblank()" function
13859 */
13860 static void
13861f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013862 typval_T *argvars;
13863 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013864{
13865 linenr_T lnum;
13866
13867 lnum = get_tv_lnum(argvars);
13868 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13869 lnum = 0;
13870 else
13871 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13872 --lnum;
13873 rettv->vval.v_number = lnum;
13874}
13875
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013876#ifdef HAVE_STDARG_H
13877/* This dummy va_list is here because:
13878 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13879 * - locally in the function results in a "used before set" warning
13880 * - using va_start() to initialize it gives "function with fixed args" error */
13881static va_list ap;
13882#endif
13883
Bram Moolenaar8c711452005-01-14 21:53:12 +000013884/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013885 * "printf()" function
13886 */
13887 static void
13888f_printf(argvars, rettv)
13889 typval_T *argvars;
13890 typval_T *rettv;
13891{
13892 rettv->v_type = VAR_STRING;
13893 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013894#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013895 {
13896 char_u buf[NUMBUFLEN];
13897 int len;
13898 char_u *s;
13899 int saved_did_emsg = did_emsg;
13900 char *fmt;
13901
13902 /* Get the required length, allocate the buffer and do it for real. */
13903 did_emsg = FALSE;
13904 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013905 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013906 if (!did_emsg)
13907 {
13908 s = alloc(len + 1);
13909 if (s != NULL)
13910 {
13911 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013912 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013913 }
13914 }
13915 did_emsg |= saved_did_emsg;
13916 }
13917#endif
13918}
13919
13920/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013921 * "pumvisible()" function
13922 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013923 static void
13924f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013925 typval_T *argvars UNUSED;
13926 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013927{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013928#ifdef FEAT_INS_EXPAND
13929 if (pum_visible())
13930 rettv->vval.v_number = 1;
13931#endif
13932}
13933
13934/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013935 * "range()" function
13936 */
13937 static void
13938f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013939 typval_T *argvars;
13940 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013941{
13942 long start;
13943 long end;
13944 long stride = 1;
13945 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013946 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013948 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013949 if (argvars[1].v_type == VAR_UNKNOWN)
13950 {
13951 end = start - 1;
13952 start = 0;
13953 }
13954 else
13955 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013956 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013957 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013958 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013959 }
13960
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013961 if (error)
13962 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013963 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013964 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013965 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013966 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013967 else
13968 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013969 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013970 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013971 if (list_append_number(rettv->vval.v_list,
13972 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013973 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013974 }
13975}
13976
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013977/*
13978 * "readfile()" function
13979 */
13980 static void
13981f_readfile(argvars, rettv)
13982 typval_T *argvars;
13983 typval_T *rettv;
13984{
13985 int binary = FALSE;
13986 char_u *fname;
13987 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013988 listitem_T *li;
13989#define FREAD_SIZE 200 /* optimized for text lines */
13990 char_u buf[FREAD_SIZE];
13991 int readlen; /* size of last fread() */
13992 int buflen; /* nr of valid chars in buf[] */
13993 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13994 int tolist; /* first byte in buf[] still to be put in list */
13995 int chop; /* how many CR to chop off */
13996 char_u *prev = NULL; /* previously read bytes, if any */
13997 int prevlen = 0; /* length of "prev" if not NULL */
13998 char_u *s;
13999 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014000 long maxline = MAXLNUM;
14001 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014002
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014003 if (argvars[1].v_type != VAR_UNKNOWN)
14004 {
14005 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14006 binary = TRUE;
14007 if (argvars[2].v_type != VAR_UNKNOWN)
14008 maxline = get_tv_number(&argvars[2]);
14009 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014010
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014011 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014012 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014013
14014 /* Always open the file in binary mode, library functions have a mind of
14015 * their own about CR-LF conversion. */
14016 fname = get_tv_string(&argvars[0]);
14017 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14018 {
14019 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14020 return;
14021 }
14022
14023 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014024 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014025 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014026 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014027 buflen = filtd + readlen;
14028 tolist = 0;
14029 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14030 {
14031 if (buf[filtd] == '\n' || readlen <= 0)
14032 {
14033 /* Only when in binary mode add an empty list item when the
14034 * last line ends in a '\n'. */
14035 if (!binary && readlen == 0 && filtd == 0)
14036 break;
14037
14038 /* Found end-of-line or end-of-file: add a text line to the
14039 * list. */
14040 chop = 0;
14041 if (!binary)
14042 while (filtd - chop - 1 >= tolist
14043 && buf[filtd - chop - 1] == '\r')
14044 ++chop;
14045 len = filtd - tolist - chop;
14046 if (prev == NULL)
14047 s = vim_strnsave(buf + tolist, len);
14048 else
14049 {
14050 s = alloc((unsigned)(prevlen + len + 1));
14051 if (s != NULL)
14052 {
14053 mch_memmove(s, prev, prevlen);
14054 vim_free(prev);
14055 prev = NULL;
14056 mch_memmove(s + prevlen, buf + tolist, len);
14057 s[prevlen + len] = NUL;
14058 }
14059 }
14060 tolist = filtd + 1;
14061
14062 li = listitem_alloc();
14063 if (li == NULL)
14064 {
14065 vim_free(s);
14066 break;
14067 }
14068 li->li_tv.v_type = VAR_STRING;
14069 li->li_tv.v_lock = 0;
14070 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014071 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014072
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014073 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014074 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014075 if (readlen <= 0)
14076 break;
14077 }
14078 else if (buf[filtd] == NUL)
14079 buf[filtd] = '\n';
14080 }
14081 if (readlen <= 0)
14082 break;
14083
14084 if (tolist == 0)
14085 {
14086 /* "buf" is full, need to move text to an allocated buffer */
14087 if (prev == NULL)
14088 {
14089 prev = vim_strnsave(buf, buflen);
14090 prevlen = buflen;
14091 }
14092 else
14093 {
14094 s = alloc((unsigned)(prevlen + buflen));
14095 if (s != NULL)
14096 {
14097 mch_memmove(s, prev, prevlen);
14098 mch_memmove(s + prevlen, buf, buflen);
14099 vim_free(prev);
14100 prev = s;
14101 prevlen += buflen;
14102 }
14103 }
14104 filtd = 0;
14105 }
14106 else
14107 {
14108 mch_memmove(buf, buf + tolist, buflen - tolist);
14109 filtd -= tolist;
14110 }
14111 }
14112
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014113 /*
14114 * For a negative line count use only the lines at the end of the file,
14115 * free the rest.
14116 */
14117 if (maxline < 0)
14118 while (cnt > -maxline)
14119 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014120 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014121 --cnt;
14122 }
14123
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014124 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014125 fclose(fd);
14126}
14127
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014128#if defined(FEAT_RELTIME)
14129static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14130
14131/*
14132 * Convert a List to proftime_T.
14133 * Return FAIL when there is something wrong.
14134 */
14135 static int
14136list2proftime(arg, tm)
14137 typval_T *arg;
14138 proftime_T *tm;
14139{
14140 long n1, n2;
14141 int error = FALSE;
14142
14143 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14144 || arg->vval.v_list->lv_len != 2)
14145 return FAIL;
14146 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14147 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14148# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014149 tm->HighPart = n1;
14150 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014151# else
14152 tm->tv_sec = n1;
14153 tm->tv_usec = n2;
14154# endif
14155 return error ? FAIL : OK;
14156}
14157#endif /* FEAT_RELTIME */
14158
14159/*
14160 * "reltime()" function
14161 */
14162 static void
14163f_reltime(argvars, rettv)
14164 typval_T *argvars;
14165 typval_T *rettv;
14166{
14167#ifdef FEAT_RELTIME
14168 proftime_T res;
14169 proftime_T start;
14170
14171 if (argvars[0].v_type == VAR_UNKNOWN)
14172 {
14173 /* No arguments: get current time. */
14174 profile_start(&res);
14175 }
14176 else if (argvars[1].v_type == VAR_UNKNOWN)
14177 {
14178 if (list2proftime(&argvars[0], &res) == FAIL)
14179 return;
14180 profile_end(&res);
14181 }
14182 else
14183 {
14184 /* Two arguments: compute the difference. */
14185 if (list2proftime(&argvars[0], &start) == FAIL
14186 || list2proftime(&argvars[1], &res) == FAIL)
14187 return;
14188 profile_sub(&res, &start);
14189 }
14190
14191 if (rettv_list_alloc(rettv) == OK)
14192 {
14193 long n1, n2;
14194
14195# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014196 n1 = res.HighPart;
14197 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014198# else
14199 n1 = res.tv_sec;
14200 n2 = res.tv_usec;
14201# endif
14202 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14203 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14204 }
14205#endif
14206}
14207
14208/*
14209 * "reltimestr()" function
14210 */
14211 static void
14212f_reltimestr(argvars, rettv)
14213 typval_T *argvars;
14214 typval_T *rettv;
14215{
14216#ifdef FEAT_RELTIME
14217 proftime_T tm;
14218#endif
14219
14220 rettv->v_type = VAR_STRING;
14221 rettv->vval.v_string = NULL;
14222#ifdef FEAT_RELTIME
14223 if (list2proftime(&argvars[0], &tm) == OK)
14224 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14225#endif
14226}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014227
Bram Moolenaar0d660222005-01-07 21:51:51 +000014228#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14229static void make_connection __ARGS((void));
14230static int check_connection __ARGS((void));
14231
14232 static void
14233make_connection()
14234{
14235 if (X_DISPLAY == NULL
14236# ifdef FEAT_GUI
14237 && !gui.in_use
14238# endif
14239 )
14240 {
14241 x_force_connect = TRUE;
14242 setup_term_clip();
14243 x_force_connect = FALSE;
14244 }
14245}
14246
14247 static int
14248check_connection()
14249{
14250 make_connection();
14251 if (X_DISPLAY == NULL)
14252 {
14253 EMSG(_("E240: No connection to Vim server"));
14254 return FAIL;
14255 }
14256 return OK;
14257}
14258#endif
14259
14260#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014261static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014262
14263 static void
14264remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014265 typval_T *argvars;
14266 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014267 int expr;
14268{
14269 char_u *server_name;
14270 char_u *keys;
14271 char_u *r = NULL;
14272 char_u buf[NUMBUFLEN];
14273# ifdef WIN32
14274 HWND w;
14275# else
14276 Window w;
14277# endif
14278
14279 if (check_restricted() || check_secure())
14280 return;
14281
14282# ifdef FEAT_X11
14283 if (check_connection() == FAIL)
14284 return;
14285# endif
14286
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014287 server_name = get_tv_string_chk(&argvars[0]);
14288 if (server_name == NULL)
14289 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014290 keys = get_tv_string_buf(&argvars[1], buf);
14291# ifdef WIN32
14292 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14293# else
14294 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14295 < 0)
14296# endif
14297 {
14298 if (r != NULL)
14299 EMSG(r); /* sending worked but evaluation failed */
14300 else
14301 EMSG2(_("E241: Unable to send to %s"), server_name);
14302 return;
14303 }
14304
14305 rettv->vval.v_string = r;
14306
14307 if (argvars[2].v_type != VAR_UNKNOWN)
14308 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014309 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014310 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014311 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014312
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014313 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014314 v.di_tv.v_type = VAR_STRING;
14315 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014316 idvar = get_tv_string_chk(&argvars[2]);
14317 if (idvar != NULL)
14318 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014319 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014320 }
14321}
14322#endif
14323
14324/*
14325 * "remote_expr()" function
14326 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014327 static void
14328f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014329 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014330 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014331{
14332 rettv->v_type = VAR_STRING;
14333 rettv->vval.v_string = NULL;
14334#ifdef FEAT_CLIENTSERVER
14335 remote_common(argvars, rettv, TRUE);
14336#endif
14337}
14338
14339/*
14340 * "remote_foreground()" function
14341 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014342 static void
14343f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014344 typval_T *argvars UNUSED;
14345 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014346{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014347#ifdef FEAT_CLIENTSERVER
14348# ifdef WIN32
14349 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014350 {
14351 char_u *server_name = get_tv_string_chk(&argvars[0]);
14352
14353 if (server_name != NULL)
14354 serverForeground(server_name);
14355 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014356# else
14357 /* Send a foreground() expression to the server. */
14358 argvars[1].v_type = VAR_STRING;
14359 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14360 argvars[2].v_type = VAR_UNKNOWN;
14361 remote_common(argvars, rettv, TRUE);
14362 vim_free(argvars[1].vval.v_string);
14363# endif
14364#endif
14365}
14366
Bram Moolenaar0d660222005-01-07 21:51:51 +000014367 static void
14368f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014369 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014370 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014371{
14372#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014373 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014374 char_u *s = NULL;
14375# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014376 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014377# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014378 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014379
14380 if (check_restricted() || check_secure())
14381 {
14382 rettv->vval.v_number = -1;
14383 return;
14384 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014385 serverid = get_tv_string_chk(&argvars[0]);
14386 if (serverid == NULL)
14387 {
14388 rettv->vval.v_number = -1;
14389 return; /* type error; errmsg already given */
14390 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014391# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014392 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014393 if (n == 0)
14394 rettv->vval.v_number = -1;
14395 else
14396 {
14397 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14398 rettv->vval.v_number = (s != NULL);
14399 }
14400# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014401 if (check_connection() == FAIL)
14402 return;
14403
14404 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014405 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014406# endif
14407
14408 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014410 char_u *retvar;
14411
Bram Moolenaar33570922005-01-25 22:26:29 +000014412 v.di_tv.v_type = VAR_STRING;
14413 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014414 retvar = get_tv_string_chk(&argvars[1]);
14415 if (retvar != NULL)
14416 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014417 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014418 }
14419#else
14420 rettv->vval.v_number = -1;
14421#endif
14422}
14423
Bram Moolenaar0d660222005-01-07 21:51:51 +000014424 static void
14425f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014426 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014427 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014428{
14429 char_u *r = NULL;
14430
14431#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014432 char_u *serverid = get_tv_string_chk(&argvars[0]);
14433
14434 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014435 {
14436# ifdef WIN32
14437 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014438 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014439
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014440 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014441 if (n != 0)
14442 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14443 if (r == NULL)
14444# else
14445 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014446 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014447# endif
14448 EMSG(_("E277: Unable to read a server reply"));
14449 }
14450#endif
14451 rettv->v_type = VAR_STRING;
14452 rettv->vval.v_string = r;
14453}
14454
14455/*
14456 * "remote_send()" function
14457 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014458 static void
14459f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014460 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014461 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014462{
14463 rettv->v_type = VAR_STRING;
14464 rettv->vval.v_string = NULL;
14465#ifdef FEAT_CLIENTSERVER
14466 remote_common(argvars, rettv, FALSE);
14467#endif
14468}
14469
14470/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014471 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014472 */
14473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014474f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014475 typval_T *argvars;
14476 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014477{
Bram Moolenaar33570922005-01-25 22:26:29 +000014478 list_T *l;
14479 listitem_T *item, *item2;
14480 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014481 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014482 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014483 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014484 dict_T *d;
14485 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014486
Bram Moolenaar8c711452005-01-14 21:53:12 +000014487 if (argvars[0].v_type == VAR_DICT)
14488 {
14489 if (argvars[2].v_type != VAR_UNKNOWN)
14490 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014491 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014492 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014493 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014494 key = get_tv_string_chk(&argvars[1]);
14495 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014496 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014497 di = dict_find(d, key, -1);
14498 if (di == NULL)
14499 EMSG2(_(e_dictkey), key);
14500 else
14501 {
14502 *rettv = di->di_tv;
14503 init_tv(&di->di_tv);
14504 dictitem_remove(d, di);
14505 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014506 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014507 }
14508 }
14509 else if (argvars[0].v_type != VAR_LIST)
14510 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014511 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014512 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014514 int error = FALSE;
14515
14516 idx = get_tv_number_chk(&argvars[1], &error);
14517 if (error)
14518 ; /* type error: do nothing, errmsg already given */
14519 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014520 EMSGN(_(e_listidx), idx);
14521 else
14522 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014523 if (argvars[2].v_type == VAR_UNKNOWN)
14524 {
14525 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014526 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014527 *rettv = item->li_tv;
14528 vim_free(item);
14529 }
14530 else
14531 {
14532 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014533 end = get_tv_number_chk(&argvars[2], &error);
14534 if (error)
14535 ; /* type error: do nothing */
14536 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014537 EMSGN(_(e_listidx), end);
14538 else
14539 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014540 int cnt = 0;
14541
14542 for (li = item; li != NULL; li = li->li_next)
14543 {
14544 ++cnt;
14545 if (li == item2)
14546 break;
14547 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014548 if (li == NULL) /* didn't find "item2" after "item" */
14549 EMSG(_(e_invrange));
14550 else
14551 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014552 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014553 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014554 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014555 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014556 l->lv_first = item;
14557 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014558 item->li_prev = NULL;
14559 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014560 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014561 }
14562 }
14563 }
14564 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014565 }
14566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567}
14568
14569/*
14570 * "rename({from}, {to})" function
14571 */
14572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014573f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014574 typval_T *argvars;
14575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576{
14577 char_u buf[NUMBUFLEN];
14578
14579 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014580 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014582 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14583 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584}
14585
14586/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014587 * "repeat()" function
14588 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014589 static void
14590f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014591 typval_T *argvars;
14592 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014593{
14594 char_u *p;
14595 int n;
14596 int slen;
14597 int len;
14598 char_u *r;
14599 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014600
14601 n = get_tv_number(&argvars[1]);
14602 if (argvars[0].v_type == VAR_LIST)
14603 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014604 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014605 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014606 if (list_extend(rettv->vval.v_list,
14607 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014608 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014609 }
14610 else
14611 {
14612 p = get_tv_string(&argvars[0]);
14613 rettv->v_type = VAR_STRING;
14614 rettv->vval.v_string = NULL;
14615
14616 slen = (int)STRLEN(p);
14617 len = slen * n;
14618 if (len <= 0)
14619 return;
14620
14621 r = alloc(len + 1);
14622 if (r != NULL)
14623 {
14624 for (i = 0; i < n; i++)
14625 mch_memmove(r + i * slen, p, (size_t)slen);
14626 r[len] = NUL;
14627 }
14628
14629 rettv->vval.v_string = r;
14630 }
14631}
14632
14633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634 * "resolve()" function
14635 */
14636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014637f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014638 typval_T *argvars;
14639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640{
14641 char_u *p;
14642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014643 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644#ifdef FEAT_SHORTCUT
14645 {
14646 char_u *v = NULL;
14647
14648 v = mch_resolve_shortcut(p);
14649 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014650 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014651 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014652 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653 }
14654#else
14655# ifdef HAVE_READLINK
14656 {
14657 char_u buf[MAXPATHL + 1];
14658 char_u *cpy;
14659 int len;
14660 char_u *remain = NULL;
14661 char_u *q;
14662 int is_relative_to_current = FALSE;
14663 int has_trailing_pathsep = FALSE;
14664 int limit = 100;
14665
14666 p = vim_strsave(p);
14667
14668 if (p[0] == '.' && (vim_ispathsep(p[1])
14669 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14670 is_relative_to_current = TRUE;
14671
14672 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014673 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674 has_trailing_pathsep = TRUE;
14675
14676 q = getnextcomp(p);
14677 if (*q != NUL)
14678 {
14679 /* Separate the first path component in "p", and keep the
14680 * remainder (beginning with the path separator). */
14681 remain = vim_strsave(q - 1);
14682 q[-1] = NUL;
14683 }
14684
14685 for (;;)
14686 {
14687 for (;;)
14688 {
14689 len = readlink((char *)p, (char *)buf, MAXPATHL);
14690 if (len <= 0)
14691 break;
14692 buf[len] = NUL;
14693
14694 if (limit-- == 0)
14695 {
14696 vim_free(p);
14697 vim_free(remain);
14698 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014699 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700 goto fail;
14701 }
14702
14703 /* Ensure that the result will have a trailing path separator
14704 * if the argument has one. */
14705 if (remain == NULL && has_trailing_pathsep)
14706 add_pathsep(buf);
14707
14708 /* Separate the first path component in the link value and
14709 * concatenate the remainders. */
14710 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14711 if (*q != NUL)
14712 {
14713 if (remain == NULL)
14714 remain = vim_strsave(q - 1);
14715 else
14716 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014717 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014718 if (cpy != NULL)
14719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014720 vim_free(remain);
14721 remain = cpy;
14722 }
14723 }
14724 q[-1] = NUL;
14725 }
14726
14727 q = gettail(p);
14728 if (q > p && *q == NUL)
14729 {
14730 /* Ignore trailing path separator. */
14731 q[-1] = NUL;
14732 q = gettail(p);
14733 }
14734 if (q > p && !mch_isFullName(buf))
14735 {
14736 /* symlink is relative to directory of argument */
14737 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14738 if (cpy != NULL)
14739 {
14740 STRCPY(cpy, p);
14741 STRCPY(gettail(cpy), buf);
14742 vim_free(p);
14743 p = cpy;
14744 }
14745 }
14746 else
14747 {
14748 vim_free(p);
14749 p = vim_strsave(buf);
14750 }
14751 }
14752
14753 if (remain == NULL)
14754 break;
14755
14756 /* Append the first path component of "remain" to "p". */
14757 q = getnextcomp(remain + 1);
14758 len = q - remain - (*q != NUL);
14759 cpy = vim_strnsave(p, STRLEN(p) + len);
14760 if (cpy != NULL)
14761 {
14762 STRNCAT(cpy, remain, len);
14763 vim_free(p);
14764 p = cpy;
14765 }
14766 /* Shorten "remain". */
14767 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014768 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769 else
14770 {
14771 vim_free(remain);
14772 remain = NULL;
14773 }
14774 }
14775
14776 /* If the result is a relative path name, make it explicitly relative to
14777 * the current directory if and only if the argument had this form. */
14778 if (!vim_ispathsep(*p))
14779 {
14780 if (is_relative_to_current
14781 && *p != NUL
14782 && !(p[0] == '.'
14783 && (p[1] == NUL
14784 || vim_ispathsep(p[1])
14785 || (p[1] == '.'
14786 && (p[2] == NUL
14787 || vim_ispathsep(p[2]))))))
14788 {
14789 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014790 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791 if (cpy != NULL)
14792 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793 vim_free(p);
14794 p = cpy;
14795 }
14796 }
14797 else if (!is_relative_to_current)
14798 {
14799 /* Strip leading "./". */
14800 q = p;
14801 while (q[0] == '.' && vim_ispathsep(q[1]))
14802 q += 2;
14803 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014804 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014805 }
14806 }
14807
14808 /* Ensure that the result will have no trailing path separator
14809 * if the argument had none. But keep "/" or "//". */
14810 if (!has_trailing_pathsep)
14811 {
14812 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014813 if (after_pathsep(p, q))
14814 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014815 }
14816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014817 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014818 }
14819# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014820 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821# endif
14822#endif
14823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014824 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825
14826#ifdef HAVE_READLINK
14827fail:
14828#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014829 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830}
14831
14832/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014833 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 */
14835 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014836f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014837 typval_T *argvars;
14838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839{
Bram Moolenaar33570922005-01-25 22:26:29 +000014840 list_T *l;
14841 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842
Bram Moolenaar0d660222005-01-07 21:51:51 +000014843 if (argvars[0].v_type != VAR_LIST)
14844 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014845 else if ((l = argvars[0].vval.v_list) != NULL
14846 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014847 {
14848 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014849 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014850 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014851 while (li != NULL)
14852 {
14853 ni = li->li_prev;
14854 list_append(l, li);
14855 li = ni;
14856 }
14857 rettv->vval.v_list = l;
14858 rettv->v_type = VAR_LIST;
14859 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014860 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862}
14863
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014864#define SP_NOMOVE 0x01 /* don't move cursor */
14865#define SP_REPEAT 0x02 /* repeat to find outer pair */
14866#define SP_RETCOUNT 0x04 /* return matchcount */
14867#define SP_SETPCMARK 0x08 /* set previous context mark */
14868#define SP_START 0x10 /* accept match at start position */
14869#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14870#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014871
Bram Moolenaar33570922005-01-25 22:26:29 +000014872static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014873
14874/*
14875 * Get flags for a search function.
14876 * Possibly sets "p_ws".
14877 * Returns BACKWARD, FORWARD or zero (for an error).
14878 */
14879 static int
14880get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014881 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014882 int *flagsp;
14883{
14884 int dir = FORWARD;
14885 char_u *flags;
14886 char_u nbuf[NUMBUFLEN];
14887 int mask;
14888
14889 if (varp->v_type != VAR_UNKNOWN)
14890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014891 flags = get_tv_string_buf_chk(varp, nbuf);
14892 if (flags == NULL)
14893 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014894 while (*flags != NUL)
14895 {
14896 switch (*flags)
14897 {
14898 case 'b': dir = BACKWARD; break;
14899 case 'w': p_ws = TRUE; break;
14900 case 'W': p_ws = FALSE; break;
14901 default: mask = 0;
14902 if (flagsp != NULL)
14903 switch (*flags)
14904 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014905 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014906 case 'e': mask = SP_END; break;
14907 case 'm': mask = SP_RETCOUNT; break;
14908 case 'n': mask = SP_NOMOVE; break;
14909 case 'p': mask = SP_SUBPAT; break;
14910 case 'r': mask = SP_REPEAT; break;
14911 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014912 }
14913 if (mask == 0)
14914 {
14915 EMSG2(_(e_invarg2), flags);
14916 dir = 0;
14917 }
14918 else
14919 *flagsp |= mask;
14920 }
14921 if (dir == 0)
14922 break;
14923 ++flags;
14924 }
14925 }
14926 return dir;
14927}
14928
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014930 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014932 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014933search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014934 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014935 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014936 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014938 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939 char_u *pat;
14940 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014941 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942 int save_p_ws = p_ws;
14943 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014944 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014945 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014946 proftime_T tm;
14947#ifdef FEAT_RELTIME
14948 long time_limit = 0;
14949#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014950 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014951 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014953 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014954 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014955 if (dir == 0)
14956 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014957 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014958 if (flags & SP_START)
14959 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014960 if (flags & SP_END)
14961 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014962
Bram Moolenaar76929292008-01-06 19:07:36 +000014963 /* Optional arguments: line number to stop searching and timeout. */
14964 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014965 {
14966 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14967 if (lnum_stop < 0)
14968 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014969#ifdef FEAT_RELTIME
14970 if (argvars[3].v_type != VAR_UNKNOWN)
14971 {
14972 time_limit = get_tv_number_chk(&argvars[3], NULL);
14973 if (time_limit < 0)
14974 goto theend;
14975 }
14976#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014977 }
14978
Bram Moolenaar76929292008-01-06 19:07:36 +000014979#ifdef FEAT_RELTIME
14980 /* Set the time limit, if there is one. */
14981 profile_setlimit(time_limit, &tm);
14982#endif
14983
Bram Moolenaar231334e2005-07-25 20:46:57 +000014984 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014985 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014986 * Check to make sure only those flags are set.
14987 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14988 * flags cannot be set. Check for that condition also.
14989 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014990 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014991 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014992 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014993 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014994 goto theend;
14995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014996
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014997 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014998 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014999 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015000 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015002 if (flags & SP_SUBPAT)
15003 retval = subpatnum;
15004 else
15005 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015006 if (flags & SP_SETPCMARK)
15007 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015009 if (match_pos != NULL)
15010 {
15011 /* Store the match cursor position */
15012 match_pos->lnum = pos.lnum;
15013 match_pos->col = pos.col + 1;
15014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015 /* "/$" will put the cursor after the end of the line, may need to
15016 * correct that here */
15017 check_cursor();
15018 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015019
15020 /* If 'n' flag is used: restore cursor position. */
15021 if (flags & SP_NOMOVE)
15022 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015023 else
15024 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015025theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015027
15028 return retval;
15029}
15030
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015031#ifdef FEAT_FLOAT
15032/*
15033 * "round({float})" function
15034 */
15035 static void
15036f_round(argvars, rettv)
15037 typval_T *argvars;
15038 typval_T *rettv;
15039{
15040 float_T f;
15041
15042 rettv->v_type = VAR_FLOAT;
15043 if (get_float_arg(argvars, &f) == OK)
15044 /* round() is not in C90, use ceil() or floor() instead. */
15045 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15046 else
15047 rettv->vval.v_float = 0.0;
15048}
15049#endif
15050
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015051/*
15052 * "search()" function
15053 */
15054 static void
15055f_search(argvars, rettv)
15056 typval_T *argvars;
15057 typval_T *rettv;
15058{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015059 int flags = 0;
15060
15061 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015062}
15063
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015065 * "searchdecl()" function
15066 */
15067 static void
15068f_searchdecl(argvars, rettv)
15069 typval_T *argvars;
15070 typval_T *rettv;
15071{
15072 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015073 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015074 int error = FALSE;
15075 char_u *name;
15076
15077 rettv->vval.v_number = 1; /* default: FAIL */
15078
15079 name = get_tv_string_chk(&argvars[0]);
15080 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015081 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015082 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015083 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15084 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15085 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015086 if (!error && name != NULL)
15087 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015088 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015089}
15090
15091/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015092 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015094 static int
15095searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015096 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015097 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098{
15099 char_u *spat, *mpat, *epat;
15100 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015102 int dir;
15103 int flags = 0;
15104 char_u nbuf1[NUMBUFLEN];
15105 char_u nbuf2[NUMBUFLEN];
15106 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015107 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015108 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015109 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015112 spat = get_tv_string_chk(&argvars[0]);
15113 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15114 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15115 if (spat == NULL || mpat == NULL || epat == NULL)
15116 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117
Bram Moolenaar071d4272004-06-13 20:20:40 +000015118 /* Handle the optional fourth argument: flags */
15119 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015120 if (dir == 0)
15121 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015122
15123 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015124 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15125 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015126 if ((flags & (SP_END | SP_SUBPAT)) != 0
15127 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015128 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015129 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015130 goto theend;
15131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015133 /* Using 'r' implies 'W', otherwise it doesn't work. */
15134 if (flags & SP_REPEAT)
15135 p_ws = FALSE;
15136
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015137 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015138 if (argvars[3].v_type == VAR_UNKNOWN
15139 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140 skip = (char_u *)"";
15141 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015143 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015144 if (argvars[5].v_type != VAR_UNKNOWN)
15145 {
15146 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15147 if (lnum_stop < 0)
15148 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015149#ifdef FEAT_RELTIME
15150 if (argvars[6].v_type != VAR_UNKNOWN)
15151 {
15152 time_limit = get_tv_number_chk(&argvars[6], NULL);
15153 if (time_limit < 0)
15154 goto theend;
15155 }
15156#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015157 }
15158 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015159 if (skip == NULL)
15160 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015162 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015163 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015164
15165theend:
15166 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015167
15168 return retval;
15169}
15170
15171/*
15172 * "searchpair()" function
15173 */
15174 static void
15175f_searchpair(argvars, rettv)
15176 typval_T *argvars;
15177 typval_T *rettv;
15178{
15179 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15180}
15181
15182/*
15183 * "searchpairpos()" function
15184 */
15185 static void
15186f_searchpairpos(argvars, rettv)
15187 typval_T *argvars;
15188 typval_T *rettv;
15189{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015190 pos_T match_pos;
15191 int lnum = 0;
15192 int col = 0;
15193
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015194 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015195 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015196
15197 if (searchpair_cmn(argvars, &match_pos) > 0)
15198 {
15199 lnum = match_pos.lnum;
15200 col = match_pos.col;
15201 }
15202
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015203 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15204 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015205}
15206
15207/*
15208 * Search for a start/middle/end thing.
15209 * Used by searchpair(), see its documentation for the details.
15210 * Returns 0 or -1 for no match,
15211 */
15212 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015213do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15214 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015215 char_u *spat; /* start pattern */
15216 char_u *mpat; /* middle pattern */
15217 char_u *epat; /* end pattern */
15218 int dir; /* BACKWARD or FORWARD */
15219 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015220 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015221 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015222 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015223 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015224{
15225 char_u *save_cpo;
15226 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15227 long retval = 0;
15228 pos_T pos;
15229 pos_T firstpos;
15230 pos_T foundpos;
15231 pos_T save_cursor;
15232 pos_T save_pos;
15233 int n;
15234 int r;
15235 int nest = 1;
15236 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015237 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015238 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015239
15240 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15241 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015242 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015243
Bram Moolenaar76929292008-01-06 19:07:36 +000015244#ifdef FEAT_RELTIME
15245 /* Set the time limit, if there is one. */
15246 profile_setlimit(time_limit, &tm);
15247#endif
15248
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015249 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15250 * start/middle/end (pat3, for the top pair). */
15251 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15252 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15253 if (pat2 == NULL || pat3 == NULL)
15254 goto theend;
15255 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15256 if (*mpat == NUL)
15257 STRCPY(pat3, pat2);
15258 else
15259 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15260 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015261 if (flags & SP_START)
15262 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015263
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264 save_cursor = curwin->w_cursor;
15265 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015266 clearpos(&firstpos);
15267 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268 pat = pat3;
15269 for (;;)
15270 {
15271 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015272 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15274 /* didn't find it or found the first match again: FAIL */
15275 break;
15276
15277 if (firstpos.lnum == 0)
15278 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015279 if (equalpos(pos, foundpos))
15280 {
15281 /* Found the same position again. Can happen with a pattern that
15282 * has "\zs" at the end and searching backwards. Advance one
15283 * character and try again. */
15284 if (dir == BACKWARD)
15285 decl(&pos);
15286 else
15287 incl(&pos);
15288 }
15289 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015290
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015291 /* clear the start flag to avoid getting stuck here */
15292 options &= ~SEARCH_START;
15293
Bram Moolenaar071d4272004-06-13 20:20:40 +000015294 /* If the skip pattern matches, ignore this match. */
15295 if (*skip != NUL)
15296 {
15297 save_pos = curwin->w_cursor;
15298 curwin->w_cursor = pos;
15299 r = eval_to_bool(skip, &err, NULL, FALSE);
15300 curwin->w_cursor = save_pos;
15301 if (err)
15302 {
15303 /* Evaluating {skip} caused an error, break here. */
15304 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015305 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306 break;
15307 }
15308 if (r)
15309 continue;
15310 }
15311
15312 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15313 {
15314 /* Found end when searching backwards or start when searching
15315 * forward: nested pair. */
15316 ++nest;
15317 pat = pat2; /* nested, don't search for middle */
15318 }
15319 else
15320 {
15321 /* Found end when searching forward or start when searching
15322 * backward: end of (nested) pair; or found middle in outer pair. */
15323 if (--nest == 1)
15324 pat = pat3; /* outer level, search for middle */
15325 }
15326
15327 if (nest == 0)
15328 {
15329 /* Found the match: return matchcount or line number. */
15330 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015331 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015333 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015334 if (flags & SP_SETPCMARK)
15335 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336 curwin->w_cursor = pos;
15337 if (!(flags & SP_REPEAT))
15338 break;
15339 nest = 1; /* search for next unmatched */
15340 }
15341 }
15342
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015343 if (match_pos != NULL)
15344 {
15345 /* Store the match cursor position */
15346 match_pos->lnum = curwin->w_cursor.lnum;
15347 match_pos->col = curwin->w_cursor.col + 1;
15348 }
15349
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015351 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352 curwin->w_cursor = save_cursor;
15353
15354theend:
15355 vim_free(pat2);
15356 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015357 if (p_cpo == empty_option)
15358 p_cpo = save_cpo;
15359 else
15360 /* Darn, evaluating the {skip} expression changed the value. */
15361 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015362
15363 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364}
15365
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015366/*
15367 * "searchpos()" function
15368 */
15369 static void
15370f_searchpos(argvars, rettv)
15371 typval_T *argvars;
15372 typval_T *rettv;
15373{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015374 pos_T match_pos;
15375 int lnum = 0;
15376 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015377 int n;
15378 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015379
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015380 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015381 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015382
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015383 n = search_cmn(argvars, &match_pos, &flags);
15384 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015385 {
15386 lnum = match_pos.lnum;
15387 col = match_pos.col;
15388 }
15389
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015390 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15391 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015392 if (flags & SP_SUBPAT)
15393 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015394}
15395
15396
Bram Moolenaar0d660222005-01-07 21:51:51 +000015397 static void
15398f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015399 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015402#ifdef FEAT_CLIENTSERVER
15403 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015404 char_u *server = get_tv_string_chk(&argvars[0]);
15405 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406
Bram Moolenaar0d660222005-01-07 21:51:51 +000015407 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015408 if (server == NULL || reply == NULL)
15409 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015410 if (check_restricted() || check_secure())
15411 return;
15412# ifdef FEAT_X11
15413 if (check_connection() == FAIL)
15414 return;
15415# endif
15416
15417 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015419 EMSG(_("E258: Unable to send to client"));
15420 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015422 rettv->vval.v_number = 0;
15423#else
15424 rettv->vval.v_number = -1;
15425#endif
15426}
15427
Bram Moolenaar0d660222005-01-07 21:51:51 +000015428 static void
15429f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015430 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015431 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015432{
15433 char_u *r = NULL;
15434
15435#ifdef FEAT_CLIENTSERVER
15436# ifdef WIN32
15437 r = serverGetVimNames();
15438# else
15439 make_connection();
15440 if (X_DISPLAY != NULL)
15441 r = serverGetVimNames(X_DISPLAY);
15442# endif
15443#endif
15444 rettv->v_type = VAR_STRING;
15445 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446}
15447
15448/*
15449 * "setbufvar()" function
15450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015452f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015453 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015454 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455{
15456 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015459 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 char_u nbuf[NUMBUFLEN];
15461
15462 if (check_restricted() || check_secure())
15463 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015464 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15465 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015466 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467 varp = &argvars[2];
15468
15469 if (buf != NULL && varname != NULL && varp != NULL)
15470 {
15471 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473
15474 if (*varname == '&')
15475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015476 long numval;
15477 char_u *strval;
15478 int error = FALSE;
15479
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015481 numval = get_tv_number_chk(varp, &error);
15482 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015483 if (!error && strval != NULL)
15484 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485 }
15486 else
15487 {
15488 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15489 if (bufvarname != NULL)
15490 {
15491 STRCPY(bufvarname, "b:");
15492 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015493 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494 vim_free(bufvarname);
15495 }
15496 }
15497
15498 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501}
15502
15503/*
15504 * "setcmdpos()" function
15505 */
15506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015507f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015508 typval_T *argvars;
15509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015511 int pos = (int)get_tv_number(&argvars[0]) - 1;
15512
15513 if (pos >= 0)
15514 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515}
15516
15517/*
15518 * "setline()" function
15519 */
15520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015521f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015522 typval_T *argvars;
15523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524{
15525 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015526 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015527 list_T *l = NULL;
15528 listitem_T *li = NULL;
15529 long added = 0;
15530 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015532 lnum = get_tv_lnum(&argvars[0]);
15533 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015535 l = argvars[1].vval.v_list;
15536 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015538 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015539 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015540
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015541 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015542 for (;;)
15543 {
15544 if (l != NULL)
15545 {
15546 /* list argument, get next string */
15547 if (li == NULL)
15548 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015549 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015550 li = li->li_next;
15551 }
15552
15553 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015554 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015555 break;
15556 if (lnum <= curbuf->b_ml.ml_line_count)
15557 {
15558 /* existing line, replace it */
15559 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15560 {
15561 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015562 if (lnum == curwin->w_cursor.lnum)
15563 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015564 rettv->vval.v_number = 0; /* OK */
15565 }
15566 }
15567 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15568 {
15569 /* lnum is one past the last line, append the line */
15570 ++added;
15571 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15572 rettv->vval.v_number = 0; /* OK */
15573 }
15574
15575 if (l == NULL) /* only one string argument */
15576 break;
15577 ++lnum;
15578 }
15579
15580 if (added > 0)
15581 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582}
15583
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015584static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15585
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015587 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015588 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015589 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015590set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015591 win_T *wp UNUSED;
15592 typval_T *list_arg UNUSED;
15593 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015594 typval_T *rettv;
15595{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015596#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015597 char_u *act;
15598 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015599#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015600
Bram Moolenaar2641f772005-03-25 21:58:17 +000015601 rettv->vval.v_number = -1;
15602
15603#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015604 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015605 EMSG(_(e_listreq));
15606 else
15607 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015608 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015609
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015610 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015611 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015612 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015613 if (act == NULL)
15614 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015615 if (*act == 'a' || *act == 'r')
15616 action = *act;
15617 }
15618
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015619 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015620 rettv->vval.v_number = 0;
15621 }
15622#endif
15623}
15624
15625/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015626 * "setloclist()" function
15627 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015628 static void
15629f_setloclist(argvars, rettv)
15630 typval_T *argvars;
15631 typval_T *rettv;
15632{
15633 win_T *win;
15634
15635 rettv->vval.v_number = -1;
15636
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015637 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015638 if (win != NULL)
15639 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15640}
15641
15642/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015643 * "setmatches()" function
15644 */
15645 static void
15646f_setmatches(argvars, rettv)
15647 typval_T *argvars;
15648 typval_T *rettv;
15649{
15650#ifdef FEAT_SEARCH_EXTRA
15651 list_T *l;
15652 listitem_T *li;
15653 dict_T *d;
15654
15655 rettv->vval.v_number = -1;
15656 if (argvars[0].v_type != VAR_LIST)
15657 {
15658 EMSG(_(e_listreq));
15659 return;
15660 }
15661 if ((l = argvars[0].vval.v_list) != NULL)
15662 {
15663
15664 /* To some extent make sure that we are dealing with a list from
15665 * "getmatches()". */
15666 li = l->lv_first;
15667 while (li != NULL)
15668 {
15669 if (li->li_tv.v_type != VAR_DICT
15670 || (d = li->li_tv.vval.v_dict) == NULL)
15671 {
15672 EMSG(_(e_invarg));
15673 return;
15674 }
15675 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15676 && dict_find(d, (char_u *)"pattern", -1) != NULL
15677 && dict_find(d, (char_u *)"priority", -1) != NULL
15678 && dict_find(d, (char_u *)"id", -1) != NULL))
15679 {
15680 EMSG(_(e_invarg));
15681 return;
15682 }
15683 li = li->li_next;
15684 }
15685
15686 clear_matches(curwin);
15687 li = l->lv_first;
15688 while (li != NULL)
15689 {
15690 d = li->li_tv.vval.v_dict;
15691 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15692 get_dict_string(d, (char_u *)"pattern", FALSE),
15693 (int)get_dict_number(d, (char_u *)"priority"),
15694 (int)get_dict_number(d, (char_u *)"id"));
15695 li = li->li_next;
15696 }
15697 rettv->vval.v_number = 0;
15698 }
15699#endif
15700}
15701
15702/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015703 * "setpos()" function
15704 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015705 static void
15706f_setpos(argvars, rettv)
15707 typval_T *argvars;
15708 typval_T *rettv;
15709{
15710 pos_T pos;
15711 int fnum;
15712 char_u *name;
15713
Bram Moolenaar08250432008-02-13 11:42:46 +000015714 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015715 name = get_tv_string_chk(argvars);
15716 if (name != NULL)
15717 {
15718 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15719 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015720 if (--pos.col < 0)
15721 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015722 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015723 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015724 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015725 if (fnum == curbuf->b_fnum)
15726 {
15727 curwin->w_cursor = pos;
15728 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015729 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015730 }
15731 else
15732 EMSG(_(e_invarg));
15733 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015734 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15735 {
15736 /* set mark */
15737 if (setmark_pos(name[1], &pos, fnum) == OK)
15738 rettv->vval.v_number = 0;
15739 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015740 else
15741 EMSG(_(e_invarg));
15742 }
15743 }
15744}
15745
15746/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015747 * "setqflist()" function
15748 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015749 static void
15750f_setqflist(argvars, rettv)
15751 typval_T *argvars;
15752 typval_T *rettv;
15753{
15754 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15755}
15756
15757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 * "setreg()" function
15759 */
15760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015761f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015762 typval_T *argvars;
15763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764{
15765 int regname;
15766 char_u *strregname;
15767 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015768 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 int append;
15770 char_u yank_type;
15771 long block_len;
15772
15773 block_len = -1;
15774 yank_type = MAUTO;
15775 append = FALSE;
15776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015777 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015778 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015780 if (strregname == NULL)
15781 return; /* type error; errmsg already given */
15782 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783 if (regname == 0 || regname == '@')
15784 regname = '"';
15785 else if (regname == '=')
15786 return;
15787
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015788 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015790 stropt = get_tv_string_chk(&argvars[2]);
15791 if (stropt == NULL)
15792 return; /* type error */
15793 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794 switch (*stropt)
15795 {
15796 case 'a': case 'A': /* append */
15797 append = TRUE;
15798 break;
15799 case 'v': case 'c': /* character-wise selection */
15800 yank_type = MCHAR;
15801 break;
15802 case 'V': case 'l': /* line-wise selection */
15803 yank_type = MLINE;
15804 break;
15805#ifdef FEAT_VISUAL
15806 case 'b': case Ctrl_V: /* block-wise selection */
15807 yank_type = MBLOCK;
15808 if (VIM_ISDIGIT(stropt[1]))
15809 {
15810 ++stropt;
15811 block_len = getdigits(&stropt) - 1;
15812 --stropt;
15813 }
15814 break;
15815#endif
15816 }
15817 }
15818
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015819 strval = get_tv_string_chk(&argvars[1]);
15820 if (strval != NULL)
15821 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015823 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824}
15825
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015826/*
15827 * "settabwinvar()" function
15828 */
15829 static void
15830f_settabwinvar(argvars, rettv)
15831 typval_T *argvars;
15832 typval_T *rettv;
15833{
15834 setwinvar(argvars, rettv, 1);
15835}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015836
15837/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015838 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015841f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015842 typval_T *argvars;
15843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015845 setwinvar(argvars, rettv, 0);
15846}
15847
15848/*
15849 * "setwinvar()" and "settabwinvar()" functions
15850 */
15851 static void
15852setwinvar(argvars, rettv, off)
15853 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015854 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015855 int off;
15856{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 win_T *win;
15858#ifdef FEAT_WINDOWS
15859 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015860 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015861#endif
15862 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015863 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015865 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866
15867 if (check_restricted() || check_secure())
15868 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015869
15870#ifdef FEAT_WINDOWS
15871 if (off == 1)
15872 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15873 else
15874 tp = curtab;
15875#endif
15876 win = find_win_by_nr(&argvars[off], tp);
15877 varname = get_tv_string_chk(&argvars[off + 1]);
15878 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015879
15880 if (win != NULL && varname != NULL && varp != NULL)
15881 {
15882#ifdef FEAT_WINDOWS
15883 /* set curwin to be our win, temporarily */
15884 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015885 save_curtab = curtab;
15886 goto_tabpage_tp(tp);
15887 if (!win_valid(win))
15888 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015889 curwin = win;
15890 curbuf = curwin->w_buffer;
15891#endif
15892
15893 if (*varname == '&')
15894 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015895 long numval;
15896 char_u *strval;
15897 int error = FALSE;
15898
Bram Moolenaar071d4272004-06-13 20:20:40 +000015899 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015900 numval = get_tv_number_chk(varp, &error);
15901 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015902 if (!error && strval != NULL)
15903 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904 }
15905 else
15906 {
15907 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15908 if (winvarname != NULL)
15909 {
15910 STRCPY(winvarname, "w:");
15911 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015912 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015913 vim_free(winvarname);
15914 }
15915 }
15916
15917#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015918 /* Restore current tabpage and window, if still valid (autocomands can
15919 * make them invalid). */
15920 if (valid_tabpage(save_curtab))
15921 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922 if (win_valid(save_curwin))
15923 {
15924 curwin = save_curwin;
15925 curbuf = curwin->w_buffer;
15926 }
15927#endif
15928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929}
15930
15931/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015932 * "shellescape({string})" function
15933 */
15934 static void
15935f_shellescape(argvars, rettv)
15936 typval_T *argvars;
15937 typval_T *rettv;
15938{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015939 rettv->vval.v_string = vim_strsave_shellescape(
15940 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015941 rettv->v_type = VAR_STRING;
15942}
15943
15944/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015945 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015946 */
15947 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015948f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015949 typval_T *argvars;
15950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015951{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015952 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015953
Bram Moolenaar0d660222005-01-07 21:51:51 +000015954 p = get_tv_string(&argvars[0]);
15955 rettv->vval.v_string = vim_strsave(p);
15956 simplify_filename(rettv->vval.v_string); /* simplify in place */
15957 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015958}
15959
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015960#ifdef FEAT_FLOAT
15961/*
15962 * "sin()" function
15963 */
15964 static void
15965f_sin(argvars, rettv)
15966 typval_T *argvars;
15967 typval_T *rettv;
15968{
15969 float_T f;
15970
15971 rettv->v_type = VAR_FLOAT;
15972 if (get_float_arg(argvars, &f) == OK)
15973 rettv->vval.v_float = sin(f);
15974 else
15975 rettv->vval.v_float = 0.0;
15976}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015977
15978/*
15979 * "sinh()" function
15980 */
15981 static void
15982f_sinh(argvars, rettv)
15983 typval_T *argvars;
15984 typval_T *rettv;
15985{
15986 float_T f;
15987
15988 rettv->v_type = VAR_FLOAT;
15989 if (get_float_arg(argvars, &f) == OK)
15990 rettv->vval.v_float = sinh(f);
15991 else
15992 rettv->vval.v_float = 0.0;
15993}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015994#endif
15995
Bram Moolenaar0d660222005-01-07 21:51:51 +000015996static int
15997#ifdef __BORLANDC__
15998 _RTLENTRYF
15999#endif
16000 item_compare __ARGS((const void *s1, const void *s2));
16001static int
16002#ifdef __BORLANDC__
16003 _RTLENTRYF
16004#endif
16005 item_compare2 __ARGS((const void *s1, const void *s2));
16006
16007static int item_compare_ic;
16008static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016009static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016010#define ITEM_COMPARE_FAIL 999
16011
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016013 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016014 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016015 static int
16016#ifdef __BORLANDC__
16017_RTLENTRYF
16018#endif
16019item_compare(s1, s2)
16020 const void *s1;
16021 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016023 char_u *p1, *p2;
16024 char_u *tofree1, *tofree2;
16025 int res;
16026 char_u numbuf1[NUMBUFLEN];
16027 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016029 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16030 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016031 if (p1 == NULL)
16032 p1 = (char_u *)"";
16033 if (p2 == NULL)
16034 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016035 if (item_compare_ic)
16036 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016038 res = STRCMP(p1, p2);
16039 vim_free(tofree1);
16040 vim_free(tofree2);
16041 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042}
16043
16044 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016045#ifdef __BORLANDC__
16046_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016048item_compare2(s1, s2)
16049 const void *s1;
16050 const void *s2;
16051{
16052 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016053 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016054 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016055 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016057 /* shortcut after failure in previous call; compare all items equal */
16058 if (item_compare_func_err)
16059 return 0;
16060
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016061 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16062 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016063 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16064 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016065
16066 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016067 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016068 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016069 clear_tv(&argv[0]);
16070 clear_tv(&argv[1]);
16071
16072 if (res == FAIL)
16073 res = ITEM_COMPARE_FAIL;
16074 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016075 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16076 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016077 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016078 clear_tv(&rettv);
16079 return res;
16080}
16081
16082/*
16083 * "sort({list})" function
16084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016086f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016087 typval_T *argvars;
16088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089{
Bram Moolenaar33570922005-01-25 22:26:29 +000016090 list_T *l;
16091 listitem_T *li;
16092 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016093 long len;
16094 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095
Bram Moolenaar0d660222005-01-07 21:51:51 +000016096 if (argvars[0].v_type != VAR_LIST)
16097 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098 else
16099 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016100 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016101 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016102 return;
16103 rettv->vval.v_list = l;
16104 rettv->v_type = VAR_LIST;
16105 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106
Bram Moolenaar0d660222005-01-07 21:51:51 +000016107 len = list_len(l);
16108 if (len <= 1)
16109 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016110
Bram Moolenaar0d660222005-01-07 21:51:51 +000016111 item_compare_ic = FALSE;
16112 item_compare_func = NULL;
16113 if (argvars[1].v_type != VAR_UNKNOWN)
16114 {
16115 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016116 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016117 else
16118 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016119 int error = FALSE;
16120
16121 i = get_tv_number_chk(&argvars[1], &error);
16122 if (error)
16123 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016124 if (i == 1)
16125 item_compare_ic = TRUE;
16126 else
16127 item_compare_func = get_tv_string(&argvars[1]);
16128 }
16129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130
Bram Moolenaar0d660222005-01-07 21:51:51 +000016131 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016132 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016133 if (ptrs == NULL)
16134 return;
16135 i = 0;
16136 for (li = l->lv_first; li != NULL; li = li->li_next)
16137 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016139 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016140 /* test the compare function */
16141 if (item_compare_func != NULL
16142 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16143 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016144 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016146 {
16147 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016148 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016149 item_compare_func == NULL ? item_compare : item_compare2);
16150
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016151 if (!item_compare_func_err)
16152 {
16153 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016154 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016155 l->lv_len = 0;
16156 for (i = 0; i < len; ++i)
16157 list_append(l, ptrs[i]);
16158 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016159 }
16160
16161 vim_free(ptrs);
16162 }
16163}
16164
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016165/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016166 * "soundfold({word})" function
16167 */
16168 static void
16169f_soundfold(argvars, rettv)
16170 typval_T *argvars;
16171 typval_T *rettv;
16172{
16173 char_u *s;
16174
16175 rettv->v_type = VAR_STRING;
16176 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016177#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016178 rettv->vval.v_string = eval_soundfold(s);
16179#else
16180 rettv->vval.v_string = vim_strsave(s);
16181#endif
16182}
16183
16184/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016185 * "spellbadword()" function
16186 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016187 static void
16188f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016189 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016190 typval_T *rettv;
16191{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016192 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016193 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016194 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016195
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016196 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016197 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016198
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016199#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016200 if (argvars[0].v_type == VAR_UNKNOWN)
16201 {
16202 /* Find the start and length of the badly spelled word. */
16203 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16204 if (len != 0)
16205 word = ml_get_cursor();
16206 }
16207 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16208 {
16209 char_u *str = get_tv_string_chk(&argvars[0]);
16210 int capcol = -1;
16211
16212 if (str != NULL)
16213 {
16214 /* Check the argument for spelling. */
16215 while (*str != NUL)
16216 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016217 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016218 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016219 {
16220 word = str;
16221 break;
16222 }
16223 str += len;
16224 }
16225 }
16226 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016227#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016228
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016229 list_append_string(rettv->vval.v_list, word, len);
16230 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016231 attr == HLF_SPB ? "bad" :
16232 attr == HLF_SPR ? "rare" :
16233 attr == HLF_SPL ? "local" :
16234 attr == HLF_SPC ? "caps" :
16235 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016236}
16237
16238/*
16239 * "spellsuggest()" function
16240 */
16241 static void
16242f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016243 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016244 typval_T *rettv;
16245{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016246#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016247 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016248 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016249 int maxcount;
16250 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016251 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016252 listitem_T *li;
16253 int need_capital = FALSE;
16254#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016255
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016256 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016257 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016258
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016259#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016260 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16261 {
16262 str = get_tv_string(&argvars[0]);
16263 if (argvars[1].v_type != VAR_UNKNOWN)
16264 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016265 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016266 if (maxcount <= 0)
16267 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016268 if (argvars[2].v_type != VAR_UNKNOWN)
16269 {
16270 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16271 if (typeerr)
16272 return;
16273 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016274 }
16275 else
16276 maxcount = 25;
16277
Bram Moolenaar4770d092006-01-12 23:22:24 +000016278 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016279
16280 for (i = 0; i < ga.ga_len; ++i)
16281 {
16282 str = ((char_u **)ga.ga_data)[i];
16283
16284 li = listitem_alloc();
16285 if (li == NULL)
16286 vim_free(str);
16287 else
16288 {
16289 li->li_tv.v_type = VAR_STRING;
16290 li->li_tv.v_lock = 0;
16291 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016292 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016293 }
16294 }
16295 ga_clear(&ga);
16296 }
16297#endif
16298}
16299
Bram Moolenaar0d660222005-01-07 21:51:51 +000016300 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016301f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016302 typval_T *argvars;
16303 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016304{
16305 char_u *str;
16306 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016307 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016308 regmatch_T regmatch;
16309 char_u patbuf[NUMBUFLEN];
16310 char_u *save_cpo;
16311 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016312 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016313 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016314 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016315
16316 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16317 save_cpo = p_cpo;
16318 p_cpo = (char_u *)"";
16319
16320 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016321 if (argvars[1].v_type != VAR_UNKNOWN)
16322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016323 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16324 if (pat == NULL)
16325 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016326 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016327 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016328 }
16329 if (pat == NULL || *pat == NUL)
16330 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016331
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016332 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016333 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016334 if (typeerr)
16335 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336
Bram Moolenaar0d660222005-01-07 21:51:51 +000016337 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16338 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016340 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016341 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016342 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016343 if (*str == NUL)
16344 match = FALSE; /* empty item at the end */
16345 else
16346 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016347 if (match)
16348 end = regmatch.startp[0];
16349 else
16350 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016351 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16352 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016353 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016354 if (list_append_string(rettv->vval.v_list, str,
16355 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016356 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016357 }
16358 if (!match)
16359 break;
16360 /* Advance to just after the match. */
16361 if (regmatch.endp[0] > str)
16362 col = 0;
16363 else
16364 {
16365 /* Don't get stuck at the same match. */
16366#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016367 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016368#else
16369 col = 1;
16370#endif
16371 }
16372 str = regmatch.endp[0];
16373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374
Bram Moolenaar0d660222005-01-07 21:51:51 +000016375 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016377
Bram Moolenaar0d660222005-01-07 21:51:51 +000016378 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379}
16380
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016381#ifdef FEAT_FLOAT
16382/*
16383 * "sqrt()" function
16384 */
16385 static void
16386f_sqrt(argvars, rettv)
16387 typval_T *argvars;
16388 typval_T *rettv;
16389{
16390 float_T f;
16391
16392 rettv->v_type = VAR_FLOAT;
16393 if (get_float_arg(argvars, &f) == OK)
16394 rettv->vval.v_float = sqrt(f);
16395 else
16396 rettv->vval.v_float = 0.0;
16397}
16398
16399/*
16400 * "str2float()" function
16401 */
16402 static void
16403f_str2float(argvars, rettv)
16404 typval_T *argvars;
16405 typval_T *rettv;
16406{
16407 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16408
16409 if (*p == '+')
16410 p = skipwhite(p + 1);
16411 (void)string2float(p, &rettv->vval.v_float);
16412 rettv->v_type = VAR_FLOAT;
16413}
16414#endif
16415
Bram Moolenaar2c932302006-03-18 21:42:09 +000016416/*
16417 * "str2nr()" function
16418 */
16419 static void
16420f_str2nr(argvars, rettv)
16421 typval_T *argvars;
16422 typval_T *rettv;
16423{
16424 int base = 10;
16425 char_u *p;
16426 long n;
16427
16428 if (argvars[1].v_type != VAR_UNKNOWN)
16429 {
16430 base = get_tv_number(&argvars[1]);
16431 if (base != 8 && base != 10 && base != 16)
16432 {
16433 EMSG(_(e_invarg));
16434 return;
16435 }
16436 }
16437
16438 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016439 if (*p == '+')
16440 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016441 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16442 rettv->vval.v_number = n;
16443}
16444
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445#ifdef HAVE_STRFTIME
16446/*
16447 * "strftime({format}[, {time}])" function
16448 */
16449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016450f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016451 typval_T *argvars;
16452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453{
16454 char_u result_buf[256];
16455 struct tm *curtime;
16456 time_t seconds;
16457 char_u *p;
16458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016459 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016461 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016462 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463 seconds = time(NULL);
16464 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016465 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466 curtime = localtime(&seconds);
16467 /* MSVC returns NULL for an invalid value of seconds. */
16468 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016469 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470 else
16471 {
16472# ifdef FEAT_MBYTE
16473 vimconv_T conv;
16474 char_u *enc;
16475
16476 conv.vc_type = CONV_NONE;
16477 enc = enc_locale();
16478 convert_setup(&conv, p_enc, enc);
16479 if (conv.vc_type != CONV_NONE)
16480 p = string_convert(&conv, p, NULL);
16481# endif
16482 if (p != NULL)
16483 (void)strftime((char *)result_buf, sizeof(result_buf),
16484 (char *)p, curtime);
16485 else
16486 result_buf[0] = NUL;
16487
16488# ifdef FEAT_MBYTE
16489 if (conv.vc_type != CONV_NONE)
16490 vim_free(p);
16491 convert_setup(&conv, enc, p_enc);
16492 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016493 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 else
16495# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016496 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016497
16498# ifdef FEAT_MBYTE
16499 /* Release conversion descriptors */
16500 convert_setup(&conv, NULL, NULL);
16501 vim_free(enc);
16502# endif
16503 }
16504}
16505#endif
16506
16507/*
16508 * "stridx()" function
16509 */
16510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016511f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016512 typval_T *argvars;
16513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016514{
16515 char_u buf[NUMBUFLEN];
16516 char_u *needle;
16517 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016518 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016520 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016522 needle = get_tv_string_chk(&argvars[1]);
16523 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016524 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016525 if (needle == NULL || haystack == NULL)
16526 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016527
Bram Moolenaar33570922005-01-25 22:26:29 +000016528 if (argvars[2].v_type != VAR_UNKNOWN)
16529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016530 int error = FALSE;
16531
16532 start_idx = get_tv_number_chk(&argvars[2], &error);
16533 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016534 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016535 if (start_idx >= 0)
16536 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016537 }
16538
16539 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16540 if (pos != NULL)
16541 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542}
16543
16544/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016545 * "string()" function
16546 */
16547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016548f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016549 typval_T *argvars;
16550 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016551{
16552 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016553 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016555 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016556 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016557 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016558 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016559 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560}
16561
16562/*
16563 * "strlen()" function
16564 */
16565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016566f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016567 typval_T *argvars;
16568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016570 rettv->vval.v_number = (varnumber_T)(STRLEN(
16571 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572}
16573
16574/*
16575 * "strpart()" function
16576 */
16577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016578f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016579 typval_T *argvars;
16580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581{
16582 char_u *p;
16583 int n;
16584 int len;
16585 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016586 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016588 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589 slen = (int)STRLEN(p);
16590
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016591 n = get_tv_number_chk(&argvars[1], &error);
16592 if (error)
16593 len = 0;
16594 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016595 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016596 else
16597 len = slen - n; /* default len: all bytes that are available. */
16598
16599 /*
16600 * Only return the overlap between the specified part and the actual
16601 * string.
16602 */
16603 if (n < 0)
16604 {
16605 len += n;
16606 n = 0;
16607 }
16608 else if (n > slen)
16609 n = slen;
16610 if (len < 0)
16611 len = 0;
16612 else if (n + len > slen)
16613 len = slen - n;
16614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016615 rettv->v_type = VAR_STRING;
16616 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016617}
16618
16619/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016620 * "strridx()" function
16621 */
16622 static void
16623f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016624 typval_T *argvars;
16625 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016626{
16627 char_u buf[NUMBUFLEN];
16628 char_u *needle;
16629 char_u *haystack;
16630 char_u *rest;
16631 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016632 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016634 needle = get_tv_string_chk(&argvars[1]);
16635 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016636
16637 rettv->vval.v_number = -1;
16638 if (needle == NULL || haystack == NULL)
16639 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016640
16641 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016642 if (argvars[2].v_type != VAR_UNKNOWN)
16643 {
16644 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016645 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016646 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016647 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016648 }
16649 else
16650 end_idx = haystack_len;
16651
Bram Moolenaar0d660222005-01-07 21:51:51 +000016652 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016653 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016654 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016655 lastmatch = haystack + end_idx;
16656 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016657 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016658 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016659 for (rest = haystack; *rest != '\0'; ++rest)
16660 {
16661 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016662 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016663 break;
16664 lastmatch = rest;
16665 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016666 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016667
16668 if (lastmatch == NULL)
16669 rettv->vval.v_number = -1;
16670 else
16671 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16672}
16673
16674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675 * "strtrans()" function
16676 */
16677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016678f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016679 typval_T *argvars;
16680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016681{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016682 rettv->v_type = VAR_STRING;
16683 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684}
16685
16686/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016687 * "submatch()" function
16688 */
16689 static void
16690f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016691 typval_T *argvars;
16692 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016693{
16694 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016695 rettv->vval.v_string =
16696 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016697}
16698
16699/*
16700 * "substitute()" function
16701 */
16702 static void
16703f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016704 typval_T *argvars;
16705 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016706{
16707 char_u patbuf[NUMBUFLEN];
16708 char_u subbuf[NUMBUFLEN];
16709 char_u flagsbuf[NUMBUFLEN];
16710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016711 char_u *str = get_tv_string_chk(&argvars[0]);
16712 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16713 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16714 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16715
Bram Moolenaar0d660222005-01-07 21:51:51 +000016716 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016717 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16718 rettv->vval.v_string = NULL;
16719 else
16720 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016721}
16722
16723/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016724 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016727f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016728 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730{
16731 int id = 0;
16732#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016733 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734 long col;
16735 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016736 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016738 lnum = get_tv_lnum(argvars); /* -1 on type error */
16739 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16740 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016742 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016743 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016744 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745#endif
16746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016747 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748}
16749
16750/*
16751 * "synIDattr(id, what [, mode])" function
16752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016754f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016755 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016757{
16758 char_u *p = NULL;
16759#ifdef FEAT_SYN_HL
16760 int id;
16761 char_u *what;
16762 char_u *mode;
16763 char_u modebuf[NUMBUFLEN];
16764 int modec;
16765
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016766 id = get_tv_number(&argvars[0]);
16767 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016768 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016770 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771 modec = TOLOWER_ASC(mode[0]);
16772 if (modec != 't' && modec != 'c'
16773#ifdef FEAT_GUI
16774 && modec != 'g'
16775#endif
16776 )
16777 modec = 0; /* replace invalid with current */
16778 }
16779 else
16780 {
16781#ifdef FEAT_GUI
16782 if (gui.in_use)
16783 modec = 'g';
16784 else
16785#endif
16786 if (t_colors > 1)
16787 modec = 'c';
16788 else
16789 modec = 't';
16790 }
16791
16792
16793 switch (TOLOWER_ASC(what[0]))
16794 {
16795 case 'b':
16796 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16797 p = highlight_color(id, what, modec);
16798 else /* bold */
16799 p = highlight_has_attr(id, HL_BOLD, modec);
16800 break;
16801
Bram Moolenaar12682fd2010-03-10 13:43:49 +010016802 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803 p = highlight_color(id, what, modec);
16804 break;
16805
16806 case 'i':
16807 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16808 p = highlight_has_attr(id, HL_INVERSE, modec);
16809 else /* italic */
16810 p = highlight_has_attr(id, HL_ITALIC, modec);
16811 break;
16812
16813 case 'n': /* name */
16814 p = get_highlight_name(NULL, id - 1);
16815 break;
16816
16817 case 'r': /* reverse */
16818 p = highlight_has_attr(id, HL_INVERSE, modec);
16819 break;
16820
Bram Moolenaar6f507d62008-11-28 10:16:05 +000016821 case 's':
16822 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
16823 p = highlight_color(id, what, modec);
16824 else /* standout */
16825 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826 break;
16827
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016828 case 'u':
16829 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16830 /* underline */
16831 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16832 else
16833 /* undercurl */
16834 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835 break;
16836 }
16837
16838 if (p != NULL)
16839 p = vim_strsave(p);
16840#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016841 rettv->v_type = VAR_STRING;
16842 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843}
16844
16845/*
16846 * "synIDtrans(id)" function
16847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016849f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016850 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852{
16853 int id;
16854
16855#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016856 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857
16858 if (id > 0)
16859 id = syn_get_final_id(id);
16860 else
16861#endif
16862 id = 0;
16863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016864 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865}
16866
16867/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016868 * "synstack(lnum, col)" function
16869 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016870 static void
16871f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016872 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016873 typval_T *rettv;
16874{
16875#ifdef FEAT_SYN_HL
16876 long lnum;
16877 long col;
16878 int i;
16879 int id;
16880#endif
16881
16882 rettv->v_type = VAR_LIST;
16883 rettv->vval.v_list = NULL;
16884
16885#ifdef FEAT_SYN_HL
16886 lnum = get_tv_lnum(argvars); /* -1 on type error */
16887 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16888
16889 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar6cad8bd2008-09-10 13:39:10 +000016890 && col >= 0 && (col == 0 || col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016891 && rettv_list_alloc(rettv) != FAIL)
16892 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016893 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016894 for (i = 0; ; ++i)
16895 {
16896 id = syn_get_stack_item(i);
16897 if (id < 0)
16898 break;
16899 if (list_append_number(rettv->vval.v_list, id) == FAIL)
16900 break;
16901 }
16902 }
16903#endif
16904}
16905
16906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 * "system()" function
16908 */
16909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016910f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016911 typval_T *argvars;
16912 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016914 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016916 char_u *infile = NULL;
16917 char_u buf[NUMBUFLEN];
16918 int err = FALSE;
16919 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016921 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016922 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016923
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016924 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016925 {
16926 /*
16927 * Write the string to a temp file, to be used for input of the shell
16928 * command.
16929 */
16930 if ((infile = vim_tempname('i')) == NULL)
16931 {
16932 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016933 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016934 }
16935
16936 fd = mch_fopen((char *)infile, WRITEBIN);
16937 if (fd == NULL)
16938 {
16939 EMSG2(_(e_notopen), infile);
16940 goto done;
16941 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016942 p = get_tv_string_buf_chk(&argvars[1], buf);
16943 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016944 {
16945 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016946 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016947 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016948 if (fwrite(p, STRLEN(p), 1, fd) != 1)
16949 err = TRUE;
16950 if (fclose(fd) != 0)
16951 err = TRUE;
16952 if (err)
16953 {
16954 EMSG(_("E677: Error writing temp file"));
16955 goto done;
16956 }
16957 }
16958
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016959 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
16960 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016961
Bram Moolenaar071d4272004-06-13 20:20:40 +000016962#ifdef USE_CR
16963 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016964 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016965 {
16966 char_u *s;
16967
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016968 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969 {
16970 if (*s == CAR)
16971 *s = NL;
16972 }
16973 }
16974#else
16975# ifdef USE_CRNL
16976 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016977 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978 {
16979 char_u *s, *d;
16980
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016981 d = res;
16982 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983 {
16984 if (s[0] == CAR && s[1] == NL)
16985 ++s;
16986 *d++ = *s;
16987 }
16988 *d = NUL;
16989 }
16990# endif
16991#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016992
16993done:
16994 if (infile != NULL)
16995 {
16996 mch_remove(infile);
16997 vim_free(infile);
16998 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016999 rettv->v_type = VAR_STRING;
17000 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001}
17002
17003/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017004 * "tabpagebuflist()" function
17005 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017006 static void
17007f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017008 typval_T *argvars UNUSED;
17009 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017010{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017011#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017012 tabpage_T *tp;
17013 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017014
17015 if (argvars[0].v_type == VAR_UNKNOWN)
17016 wp = firstwin;
17017 else
17018 {
17019 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17020 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017021 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017022 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017023 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017024 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017025 for (; wp != NULL; wp = wp->w_next)
17026 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017027 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017028 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017029 }
17030#endif
17031}
17032
17033
17034/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017035 * "tabpagenr()" function
17036 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017037 static void
17038f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017039 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017040 typval_T *rettv;
17041{
17042 int nr = 1;
17043#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017044 char_u *arg;
17045
17046 if (argvars[0].v_type != VAR_UNKNOWN)
17047 {
17048 arg = get_tv_string_chk(&argvars[0]);
17049 nr = 0;
17050 if (arg != NULL)
17051 {
17052 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017053 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017054 else
17055 EMSG2(_(e_invexpr2), arg);
17056 }
17057 }
17058 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017059 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017060#endif
17061 rettv->vval.v_number = nr;
17062}
17063
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017064
17065#ifdef FEAT_WINDOWS
17066static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17067
17068/*
17069 * Common code for tabpagewinnr() and winnr().
17070 */
17071 static int
17072get_winnr(tp, argvar)
17073 tabpage_T *tp;
17074 typval_T *argvar;
17075{
17076 win_T *twin;
17077 int nr = 1;
17078 win_T *wp;
17079 char_u *arg;
17080
17081 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17082 if (argvar->v_type != VAR_UNKNOWN)
17083 {
17084 arg = get_tv_string_chk(argvar);
17085 if (arg == NULL)
17086 nr = 0; /* type error; errmsg already given */
17087 else if (STRCMP(arg, "$") == 0)
17088 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17089 else if (STRCMP(arg, "#") == 0)
17090 {
17091 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17092 if (twin == NULL)
17093 nr = 0;
17094 }
17095 else
17096 {
17097 EMSG2(_(e_invexpr2), arg);
17098 nr = 0;
17099 }
17100 }
17101
17102 if (nr > 0)
17103 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17104 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017105 {
17106 if (wp == NULL)
17107 {
17108 /* didn't find it in this tabpage */
17109 nr = 0;
17110 break;
17111 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017112 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017113 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017114 return nr;
17115}
17116#endif
17117
17118/*
17119 * "tabpagewinnr()" function
17120 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017121 static void
17122f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017123 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017124 typval_T *rettv;
17125{
17126 int nr = 1;
17127#ifdef FEAT_WINDOWS
17128 tabpage_T *tp;
17129
17130 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17131 if (tp == NULL)
17132 nr = 0;
17133 else
17134 nr = get_winnr(tp, &argvars[1]);
17135#endif
17136 rettv->vval.v_number = nr;
17137}
17138
17139
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017140/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017141 * "tagfiles()" function
17142 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017143 static void
17144f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017145 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017146 typval_T *rettv;
17147{
17148 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017149 tagname_T tn;
17150 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017151
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017152 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017153 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017154
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017155 for (first = TRUE; ; first = FALSE)
17156 if (get_tagfname(&tn, first, fname) == FAIL
17157 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017158 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017159 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017160}
17161
17162/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017163 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017164 */
17165 static void
17166f_taglist(argvars, rettv)
17167 typval_T *argvars;
17168 typval_T *rettv;
17169{
17170 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017171
17172 tag_pattern = get_tv_string(&argvars[0]);
17173
17174 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017175 if (*tag_pattern == NUL)
17176 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017177
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017178 if (rettv_list_alloc(rettv) == OK)
17179 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017180}
17181
17182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183 * "tempname()" function
17184 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017186f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017187 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017188 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189{
17190 static int x = 'A';
17191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017192 rettv->v_type = VAR_STRING;
17193 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194
17195 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17196 * names. Skip 'I' and 'O', they are used for shell redirection. */
17197 do
17198 {
17199 if (x == 'Z')
17200 x = '0';
17201 else if (x == '9')
17202 x = 'A';
17203 else
17204 {
17205#ifdef EBCDIC
17206 if (x == 'I')
17207 x = 'J';
17208 else if (x == 'R')
17209 x = 'S';
17210 else
17211#endif
17212 ++x;
17213 }
17214 } while (x == 'I' || x == 'O');
17215}
17216
17217/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017218 * "test(list)" function: Just checking the walls...
17219 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017220 static void
17221f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017222 typval_T *argvars UNUSED;
17223 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017224{
17225 /* Used for unit testing. Change the code below to your liking. */
17226#if 0
17227 listitem_T *li;
17228 list_T *l;
17229 char_u *bad, *good;
17230
17231 if (argvars[0].v_type != VAR_LIST)
17232 return;
17233 l = argvars[0].vval.v_list;
17234 if (l == NULL)
17235 return;
17236 li = l->lv_first;
17237 if (li == NULL)
17238 return;
17239 bad = get_tv_string(&li->li_tv);
17240 li = li->li_next;
17241 if (li == NULL)
17242 return;
17243 good = get_tv_string(&li->li_tv);
17244 rettv->vval.v_number = test_edit_score(bad, good);
17245#endif
17246}
17247
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017248#ifdef FEAT_FLOAT
17249/*
17250 * "tan()" function
17251 */
17252 static void
17253f_tan(argvars, rettv)
17254 typval_T *argvars;
17255 typval_T *rettv;
17256{
17257 float_T f;
17258
17259 rettv->v_type = VAR_FLOAT;
17260 if (get_float_arg(argvars, &f) == OK)
17261 rettv->vval.v_float = tan(f);
17262 else
17263 rettv->vval.v_float = 0.0;
17264}
17265
17266/*
17267 * "tanh()" function
17268 */
17269 static void
17270f_tanh(argvars, rettv)
17271 typval_T *argvars;
17272 typval_T *rettv;
17273{
17274 float_T f;
17275
17276 rettv->v_type = VAR_FLOAT;
17277 if (get_float_arg(argvars, &f) == OK)
17278 rettv->vval.v_float = tanh(f);
17279 else
17280 rettv->vval.v_float = 0.0;
17281}
17282#endif
17283
Bram Moolenaard52d9742005-08-21 22:20:28 +000017284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285 * "tolower(string)" function
17286 */
17287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017288f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017289 typval_T *argvars;
17290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017291{
17292 char_u *p;
17293
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017294 p = vim_strsave(get_tv_string(&argvars[0]));
17295 rettv->v_type = VAR_STRING;
17296 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017297
17298 if (p != NULL)
17299 while (*p != NUL)
17300 {
17301#ifdef FEAT_MBYTE
17302 int l;
17303
17304 if (enc_utf8)
17305 {
17306 int c, lc;
17307
17308 c = utf_ptr2char(p);
17309 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017310 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311 /* TODO: reallocate string when byte count changes. */
17312 if (utf_char2len(lc) == l)
17313 utf_char2bytes(lc, p);
17314 p += l;
17315 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017316 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 p += l; /* skip multi-byte character */
17318 else
17319#endif
17320 {
17321 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17322 ++p;
17323 }
17324 }
17325}
17326
17327/*
17328 * "toupper(string)" function
17329 */
17330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017331f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017332 typval_T *argvars;
17333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017335 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017336 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337}
17338
17339/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017340 * "tr(string, fromstr, tostr)" function
17341 */
17342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017343f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017344 typval_T *argvars;
17345 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017346{
17347 char_u *instr;
17348 char_u *fromstr;
17349 char_u *tostr;
17350 char_u *p;
17351#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017352 int inlen;
17353 int fromlen;
17354 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017355 int idx;
17356 char_u *cpstr;
17357 int cplen;
17358 int first = TRUE;
17359#endif
17360 char_u buf[NUMBUFLEN];
17361 char_u buf2[NUMBUFLEN];
17362 garray_T ga;
17363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017364 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017365 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17366 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017367
17368 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017369 rettv->v_type = VAR_STRING;
17370 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017371 if (fromstr == NULL || tostr == NULL)
17372 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017373 ga_init2(&ga, (int)sizeof(char), 80);
17374
17375#ifdef FEAT_MBYTE
17376 if (!has_mbyte)
17377#endif
17378 /* not multi-byte: fromstr and tostr must be the same length */
17379 if (STRLEN(fromstr) != STRLEN(tostr))
17380 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017381#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017382error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017383#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017384 EMSG2(_(e_invarg2), fromstr);
17385 ga_clear(&ga);
17386 return;
17387 }
17388
17389 /* fromstr and tostr have to contain the same number of chars */
17390 while (*instr != NUL)
17391 {
17392#ifdef FEAT_MBYTE
17393 if (has_mbyte)
17394 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017395 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017396 cpstr = instr;
17397 cplen = inlen;
17398 idx = 0;
17399 for (p = fromstr; *p != NUL; p += fromlen)
17400 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017401 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017402 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17403 {
17404 for (p = tostr; *p != NUL; p += tolen)
17405 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017406 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017407 if (idx-- == 0)
17408 {
17409 cplen = tolen;
17410 cpstr = p;
17411 break;
17412 }
17413 }
17414 if (*p == NUL) /* tostr is shorter than fromstr */
17415 goto error;
17416 break;
17417 }
17418 ++idx;
17419 }
17420
17421 if (first && cpstr == instr)
17422 {
17423 /* Check that fromstr and tostr have the same number of
17424 * (multi-byte) characters. Done only once when a character
17425 * of instr doesn't appear in fromstr. */
17426 first = FALSE;
17427 for (p = tostr; *p != NUL; p += tolen)
17428 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017429 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017430 --idx;
17431 }
17432 if (idx != 0)
17433 goto error;
17434 }
17435
17436 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017437 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017438 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017439
17440 instr += inlen;
17441 }
17442 else
17443#endif
17444 {
17445 /* When not using multi-byte chars we can do it faster. */
17446 p = vim_strchr(fromstr, *instr);
17447 if (p != NULL)
17448 ga_append(&ga, tostr[p - fromstr]);
17449 else
17450 ga_append(&ga, *instr);
17451 ++instr;
17452 }
17453 }
17454
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017455 /* add a terminating NUL */
17456 ga_grow(&ga, 1);
17457 ga_append(&ga, NUL);
17458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017459 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017460}
17461
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017462#ifdef FEAT_FLOAT
17463/*
17464 * "trunc({float})" function
17465 */
17466 static void
17467f_trunc(argvars, rettv)
17468 typval_T *argvars;
17469 typval_T *rettv;
17470{
17471 float_T f;
17472
17473 rettv->v_type = VAR_FLOAT;
17474 if (get_float_arg(argvars, &f) == OK)
17475 /* trunc() is not in C90, use floor() or ceil() instead. */
17476 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17477 else
17478 rettv->vval.v_float = 0.0;
17479}
17480#endif
17481
Bram Moolenaar8299df92004-07-10 09:47:34 +000017482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483 * "type(expr)" function
17484 */
17485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017486f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017487 typval_T *argvars;
17488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017490 int n;
17491
17492 switch (argvars[0].v_type)
17493 {
17494 case VAR_NUMBER: n = 0; break;
17495 case VAR_STRING: n = 1; break;
17496 case VAR_FUNC: n = 2; break;
17497 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017498 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017499#ifdef FEAT_FLOAT
17500 case VAR_FLOAT: n = 5; break;
17501#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017502 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17503 }
17504 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505}
17506
17507/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017508 * "values(dict)" function
17509 */
17510 static void
17511f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017512 typval_T *argvars;
17513 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017514{
17515 dict_list(argvars, rettv, 1);
17516}
17517
17518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519 * "virtcol(string)" function
17520 */
17521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017522f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017523 typval_T *argvars;
17524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525{
17526 colnr_T vcol = 0;
17527 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017528 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017530 fp = var2fpos(&argvars[0], FALSE, &fnum);
17531 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17532 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533 {
17534 getvvcol(curwin, fp, NULL, NULL, &vcol);
17535 ++vcol;
17536 }
17537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017538 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539}
17540
17541/*
17542 * "visualmode()" function
17543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017545f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017546 typval_T *argvars UNUSED;
17547 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548{
17549#ifdef FEAT_VISUAL
17550 char_u str[2];
17551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017552 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553 str[0] = curbuf->b_visual_mode_eval;
17554 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017555 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556
17557 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017558 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560#endif
17561}
17562
17563/*
17564 * "winbufnr(nr)" function
17565 */
17566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017567f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017568 typval_T *argvars;
17569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570{
17571 win_T *wp;
17572
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017573 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017575 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017577 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578}
17579
17580/*
17581 * "wincol()" function
17582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017584f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017585 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587{
17588 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017589 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590}
17591
17592/*
17593 * "winheight(nr)" function
17594 */
17595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017596f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017597 typval_T *argvars;
17598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599{
17600 win_T *wp;
17601
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017602 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017604 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017606 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017607}
17608
17609/*
17610 * "winline()" function
17611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017613f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017614 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616{
17617 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017618 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619}
17620
17621/*
17622 * "winnr()" function
17623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017625f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017626 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628{
17629 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017630
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017632 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017634 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635}
17636
17637/*
17638 * "winrestcmd()" function
17639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017641f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017642 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644{
17645#ifdef FEAT_WINDOWS
17646 win_T *wp;
17647 int winnr = 1;
17648 garray_T ga;
17649 char_u buf[50];
17650
17651 ga_init2(&ga, (int)sizeof(char), 70);
17652 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17653 {
17654 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17655 ga_concat(&ga, buf);
17656# ifdef FEAT_VERTSPLIT
17657 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17658 ga_concat(&ga, buf);
17659# endif
17660 ++winnr;
17661 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017662 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017664 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017666 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017668 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669}
17670
17671/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017672 * "winrestview()" function
17673 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017674 static void
17675f_winrestview(argvars, rettv)
17676 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017677 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017678{
17679 dict_T *dict;
17680
17681 if (argvars[0].v_type != VAR_DICT
17682 || (dict = argvars[0].vval.v_dict) == NULL)
17683 EMSG(_(e_invarg));
17684 else
17685 {
17686 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17687 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17688#ifdef FEAT_VIRTUALEDIT
17689 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17690#endif
17691 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017692 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017693
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017694 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017695#ifdef FEAT_DIFF
17696 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17697#endif
17698 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17699 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17700
17701 check_cursor();
17702 changed_cline_bef_curs();
17703 invalidate_botline();
17704 redraw_later(VALID);
17705
17706 if (curwin->w_topline == 0)
17707 curwin->w_topline = 1;
17708 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17709 curwin->w_topline = curbuf->b_ml.ml_line_count;
17710#ifdef FEAT_DIFF
17711 check_topfill(curwin, TRUE);
17712#endif
17713 }
17714}
17715
17716/*
17717 * "winsaveview()" function
17718 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017719 static void
17720f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017721 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017722 typval_T *rettv;
17723{
17724 dict_T *dict;
17725
17726 dict = dict_alloc();
17727 if (dict == NULL)
17728 return;
17729 rettv->v_type = VAR_DICT;
17730 rettv->vval.v_dict = dict;
17731 ++dict->dv_refcount;
17732
17733 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17734 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17735#ifdef FEAT_VIRTUALEDIT
17736 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17737#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017738 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017739 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17740
17741 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17742#ifdef FEAT_DIFF
17743 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17744#endif
17745 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17746 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17747}
17748
17749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750 * "winwidth(nr)" function
17751 */
17752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017753f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017754 typval_T *argvars;
17755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756{
17757 win_T *wp;
17758
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017759 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017761 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762 else
17763#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017764 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017766 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767#endif
17768}
17769
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017771 * "writefile()" function
17772 */
17773 static void
17774f_writefile(argvars, rettv)
17775 typval_T *argvars;
17776 typval_T *rettv;
17777{
17778 int binary = FALSE;
17779 char_u *fname;
17780 FILE *fd;
17781 listitem_T *li;
17782 char_u *s;
17783 int ret = 0;
17784 int c;
17785
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017786 if (check_restricted() || check_secure())
17787 return;
17788
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017789 if (argvars[0].v_type != VAR_LIST)
17790 {
17791 EMSG2(_(e_listarg), "writefile()");
17792 return;
17793 }
17794 if (argvars[0].vval.v_list == NULL)
17795 return;
17796
17797 if (argvars[2].v_type != VAR_UNKNOWN
17798 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17799 binary = TRUE;
17800
17801 /* Always open the file in binary mode, library functions have a mind of
17802 * their own about CR-LF conversion. */
17803 fname = get_tv_string(&argvars[1]);
17804 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17805 {
17806 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17807 ret = -1;
17808 }
17809 else
17810 {
17811 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17812 li = li->li_next)
17813 {
17814 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17815 {
17816 if (*s == '\n')
17817 c = putc(NUL, fd);
17818 else
17819 c = putc(*s, fd);
17820 if (c == EOF)
17821 {
17822 ret = -1;
17823 break;
17824 }
17825 }
17826 if (!binary || li->li_next != NULL)
17827 if (putc('\n', fd) == EOF)
17828 {
17829 ret = -1;
17830 break;
17831 }
17832 if (ret < 0)
17833 {
17834 EMSG(_(e_write));
17835 break;
17836 }
17837 }
17838 fclose(fd);
17839 }
17840
17841 rettv->vval.v_number = ret;
17842}
17843
17844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017846 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847 */
17848 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017849var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017850 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017851 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017852 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017854 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017856 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857
Bram Moolenaara5525202006-03-02 22:52:09 +000017858 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017859 if (varp->v_type == VAR_LIST)
17860 {
17861 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017862 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000017863 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017864 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017865
17866 l = varp->vval.v_list;
17867 if (l == NULL)
17868 return NULL;
17869
17870 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017871 pos.lnum = list_find_nr(l, 0L, &error);
17872 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017873 return NULL; /* invalid line number */
17874
17875 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017876 pos.col = list_find_nr(l, 1L, &error);
17877 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017878 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017879 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000017880
17881 /* We accept "$" for the column number: last column. */
17882 li = list_find(l, 1L);
17883 if (li != NULL && li->li_tv.v_type == VAR_STRING
17884 && li->li_tv.vval.v_string != NULL
17885 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
17886 pos.col = len + 1;
17887
Bram Moolenaara5525202006-03-02 22:52:09 +000017888 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000017889 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017890 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017891 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017892
Bram Moolenaara5525202006-03-02 22:52:09 +000017893#ifdef FEAT_VIRTUALEDIT
17894 /* Get the virtual offset. Defaults to zero. */
17895 pos.coladd = list_find_nr(l, 2L, &error);
17896 if (error)
17897 pos.coladd = 0;
17898#endif
17899
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017900 return &pos;
17901 }
17902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017903 name = get_tv_string_chk(varp);
17904 if (name == NULL)
17905 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017906 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017907 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017908#ifdef FEAT_VISUAL
17909 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
17910 {
17911 if (VIsual_active)
17912 return &VIsual;
17913 return &curwin->w_cursor;
17914 }
17915#endif
17916 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017918 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
17920 return NULL;
17921 return pp;
17922 }
Bram Moolenaara5525202006-03-02 22:52:09 +000017923
17924#ifdef FEAT_VIRTUALEDIT
17925 pos.coladd = 0;
17926#endif
17927
Bram Moolenaar477933c2007-07-17 14:32:23 +000017928 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017929 {
17930 pos.col = 0;
17931 if (name[1] == '0') /* "w0": first visible line */
17932 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017933 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017934 pos.lnum = curwin->w_topline;
17935 return &pos;
17936 }
17937 else if (name[1] == '$') /* "w$": last visible line */
17938 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017939 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017940 pos.lnum = curwin->w_botline - 1;
17941 return &pos;
17942 }
17943 }
17944 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000017946 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947 {
17948 pos.lnum = curbuf->b_ml.ml_line_count;
17949 pos.col = 0;
17950 }
17951 else
17952 {
17953 pos.lnum = curwin->w_cursor.lnum;
17954 pos.col = (colnr_T)STRLEN(ml_get_curline());
17955 }
17956 return &pos;
17957 }
17958 return NULL;
17959}
17960
17961/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017962 * Convert list in "arg" into a position and optional file number.
17963 * When "fnump" is NULL there is no file number, only 3 items.
17964 * Note that the column is passed on as-is, the caller may want to decrement
17965 * it to use 1 for the first column.
17966 * Return FAIL when conversion is not possible, doesn't check the position for
17967 * validity.
17968 */
17969 static int
17970list2fpos(arg, posp, fnump)
17971 typval_T *arg;
17972 pos_T *posp;
17973 int *fnump;
17974{
17975 list_T *l = arg->vval.v_list;
17976 long i = 0;
17977 long n;
17978
Bram Moolenaarbde35262006-07-23 20:12:24 +000017979 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17980 * when "fnump" isn't NULL and "coladd" is optional. */
17981 if (arg->v_type != VAR_LIST
17982 || l == NULL
17983 || l->lv_len < (fnump == NULL ? 2 : 3)
17984 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017985 return FAIL;
17986
17987 if (fnump != NULL)
17988 {
17989 n = list_find_nr(l, i++, NULL); /* fnum */
17990 if (n < 0)
17991 return FAIL;
17992 if (n == 0)
17993 n = curbuf->b_fnum; /* current buffer */
17994 *fnump = n;
17995 }
17996
17997 n = list_find_nr(l, i++, NULL); /* lnum */
17998 if (n < 0)
17999 return FAIL;
18000 posp->lnum = n;
18001
18002 n = list_find_nr(l, i++, NULL); /* col */
18003 if (n < 0)
18004 return FAIL;
18005 posp->col = n;
18006
18007#ifdef FEAT_VIRTUALEDIT
18008 n = list_find_nr(l, i, NULL);
18009 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018010 posp->coladd = 0;
18011 else
18012 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018013#endif
18014
18015 return OK;
18016}
18017
18018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019 * Get the length of an environment variable name.
18020 * Advance "arg" to the first character after the name.
18021 * Return 0 for error.
18022 */
18023 static int
18024get_env_len(arg)
18025 char_u **arg;
18026{
18027 char_u *p;
18028 int len;
18029
18030 for (p = *arg; vim_isIDc(*p); ++p)
18031 ;
18032 if (p == *arg) /* no name found */
18033 return 0;
18034
18035 len = (int)(p - *arg);
18036 *arg = p;
18037 return len;
18038}
18039
18040/*
18041 * Get the length of the name of a function or internal variable.
18042 * "arg" is advanced to the first non-white character after the name.
18043 * Return 0 if something is wrong.
18044 */
18045 static int
18046get_id_len(arg)
18047 char_u **arg;
18048{
18049 char_u *p;
18050 int len;
18051
18052 /* Find the end of the name. */
18053 for (p = *arg; eval_isnamec(*p); ++p)
18054 ;
18055 if (p == *arg) /* no name found */
18056 return 0;
18057
18058 len = (int)(p - *arg);
18059 *arg = skipwhite(p);
18060
18061 return len;
18062}
18063
18064/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018065 * Get the length of the name of a variable or function.
18066 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018068 * Return -1 if curly braces expansion failed.
18069 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 * If the name contains 'magic' {}'s, expand them and return the
18071 * expanded name in an allocated string via 'alias' - caller must free.
18072 */
18073 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018074get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 char_u **arg;
18076 char_u **alias;
18077 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018078 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079{
18080 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081 char_u *p;
18082 char_u *expr_start;
18083 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084
18085 *alias = NULL; /* default to no alias */
18086
18087 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18088 && (*arg)[2] == (int)KE_SNR)
18089 {
18090 /* hard coded <SNR>, already translated */
18091 *arg += 3;
18092 return get_id_len(arg) + 3;
18093 }
18094 len = eval_fname_script(*arg);
18095 if (len > 0)
18096 {
18097 /* literal "<SID>", "s:" or "<SNR>" */
18098 *arg += len;
18099 }
18100
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018102 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018104 p = find_name_end(*arg, &expr_start, &expr_end,
18105 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 if (expr_start != NULL)
18107 {
18108 char_u *temp_string;
18109
18110 if (!evaluate)
18111 {
18112 len += (int)(p - *arg);
18113 *arg = skipwhite(p);
18114 return len;
18115 }
18116
18117 /*
18118 * Include any <SID> etc in the expanded string:
18119 * Thus the -len here.
18120 */
18121 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18122 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018123 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018124 *alias = temp_string;
18125 *arg = skipwhite(p);
18126 return (int)STRLEN(temp_string);
18127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128
18129 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018130 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 EMSG2(_(e_invexpr2), *arg);
18132
18133 return len;
18134}
18135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018136/*
18137 * Find the end of a variable or function name, taking care of magic braces.
18138 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18139 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018140 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018141 * Return a pointer to just after the name. Equal to "arg" if there is no
18142 * valid name.
18143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018145find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146 char_u *arg;
18147 char_u **expr_start;
18148 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018149 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018151 int mb_nest = 0;
18152 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153 char_u *p;
18154
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018155 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018157 *expr_start = NULL;
18158 *expr_end = NULL;
18159 }
18160
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018161 /* Quick check for valid starting character. */
18162 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18163 return arg;
18164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018165 for (p = arg; *p != NUL
18166 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018167 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018168 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018169 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018170 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018171 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018172 if (*p == '\'')
18173 {
18174 /* skip over 'string' to avoid counting [ and ] inside it. */
18175 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18176 ;
18177 if (*p == NUL)
18178 break;
18179 }
18180 else if (*p == '"')
18181 {
18182 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18183 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18184 if (*p == '\\' && p[1] != NUL)
18185 ++p;
18186 if (*p == NUL)
18187 break;
18188 }
18189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018190 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018192 if (*p == '[')
18193 ++br_nest;
18194 else if (*p == ']')
18195 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018198 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018200 if (*p == '{')
18201 {
18202 mb_nest++;
18203 if (expr_start != NULL && *expr_start == NULL)
18204 *expr_start = p;
18205 }
18206 else if (*p == '}')
18207 {
18208 mb_nest--;
18209 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18210 *expr_end = p;
18211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213 }
18214
18215 return p;
18216}
18217
18218/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018219 * Expands out the 'magic' {}'s in a variable/function name.
18220 * Note that this can call itself recursively, to deal with
18221 * constructs like foo{bar}{baz}{bam}
18222 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18223 * "in_start" ^
18224 * "expr_start" ^
18225 * "expr_end" ^
18226 * "in_end" ^
18227 *
18228 * Returns a new allocated string, which the caller must free.
18229 * Returns NULL for failure.
18230 */
18231 static char_u *
18232make_expanded_name(in_start, expr_start, expr_end, in_end)
18233 char_u *in_start;
18234 char_u *expr_start;
18235 char_u *expr_end;
18236 char_u *in_end;
18237{
18238 char_u c1;
18239 char_u *retval = NULL;
18240 char_u *temp_result;
18241 char_u *nextcmd = NULL;
18242
18243 if (expr_end == NULL || in_end == NULL)
18244 return NULL;
18245 *expr_start = NUL;
18246 *expr_end = NUL;
18247 c1 = *in_end;
18248 *in_end = NUL;
18249
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018250 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018251 if (temp_result != NULL && nextcmd == NULL)
18252 {
18253 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18254 + (in_end - expr_end) + 1));
18255 if (retval != NULL)
18256 {
18257 STRCPY(retval, in_start);
18258 STRCAT(retval, temp_result);
18259 STRCAT(retval, expr_end + 1);
18260 }
18261 }
18262 vim_free(temp_result);
18263
18264 *in_end = c1; /* put char back for error messages */
18265 *expr_start = '{';
18266 *expr_end = '}';
18267
18268 if (retval != NULL)
18269 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018270 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018271 if (expr_start != NULL)
18272 {
18273 /* Further expansion! */
18274 temp_result = make_expanded_name(retval, expr_start,
18275 expr_end, temp_result);
18276 vim_free(retval);
18277 retval = temp_result;
18278 }
18279 }
18280
18281 return retval;
18282}
18283
18284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018285 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018286 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287 */
18288 static int
18289eval_isnamec(c)
18290 int c;
18291{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018292 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18293}
18294
18295/*
18296 * Return TRUE if character "c" can be used as the first character in a
18297 * variable or function name (excluding '{' and '}').
18298 */
18299 static int
18300eval_isnamec1(c)
18301 int c;
18302{
18303 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304}
18305
18306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307 * Set number v: variable to "val".
18308 */
18309 void
18310set_vim_var_nr(idx, val)
18311 int idx;
18312 long val;
18313{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018314 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315}
18316
18317/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018318 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319 */
18320 long
18321get_vim_var_nr(idx)
18322 int idx;
18323{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018324 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325}
18326
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018327/*
18328 * Get string v: variable value. Uses a static buffer, can only be used once.
18329 */
18330 char_u *
18331get_vim_var_str(idx)
18332 int idx;
18333{
18334 return get_tv_string(&vimvars[idx].vv_tv);
18335}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018336
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018338 * Get List v: variable value. Caller must take care of reference count when
18339 * needed.
18340 */
18341 list_T *
18342get_vim_var_list(idx)
18343 int idx;
18344{
18345 return vimvars[idx].vv_list;
18346}
18347
18348/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018349 * Set v:char to character "c".
18350 */
18351 void
18352set_vim_var_char(c)
18353 int c;
18354{
18355#ifdef FEAT_MBYTE
18356 char_u buf[MB_MAXBYTES];
18357#else
18358 char_u buf[2];
18359#endif
18360
18361#ifdef FEAT_MBYTE
18362 if (has_mbyte)
18363 buf[(*mb_char2bytes)(c, buf)] = NUL;
18364 else
18365#endif
18366 {
18367 buf[0] = c;
18368 buf[1] = NUL;
18369 }
18370 set_vim_var_string(VV_CHAR, buf, -1);
18371}
18372
18373/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018374 * Set v:count to "count" and v:count1 to "count1".
18375 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018376 */
18377 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018378set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379 long count;
18380 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018381 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018383 if (set_prevcount)
18384 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018385 vimvars[VV_COUNT].vv_nr = count;
18386 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387}
18388
18389/*
18390 * Set string v: variable to a copy of "val".
18391 */
18392 void
18393set_vim_var_string(idx, val, len)
18394 int idx;
18395 char_u *val;
18396 int len; /* length of "val" to use or -1 (whole string) */
18397{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018398 /* Need to do this (at least) once, since we can't initialize a union.
18399 * Will always be invoked when "v:progname" is set. */
18400 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18401
Bram Moolenaare9a41262005-01-15 22:18:47 +000018402 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018404 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018405 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018406 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018408 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409}
18410
18411/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018412 * Set List v: variable to "val".
18413 */
18414 void
18415set_vim_var_list(idx, val)
18416 int idx;
18417 list_T *val;
18418{
18419 list_unref(vimvars[idx].vv_list);
18420 vimvars[idx].vv_list = val;
18421 if (val != NULL)
18422 ++val->lv_refcount;
18423}
18424
18425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018426 * Set v:register if needed.
18427 */
18428 void
18429set_reg_var(c)
18430 int c;
18431{
18432 char_u regname;
18433
18434 if (c == 0 || c == ' ')
18435 regname = '"';
18436 else
18437 regname = c;
18438 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018439 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440 set_vim_var_string(VV_REG, &regname, 1);
18441}
18442
18443/*
18444 * Get or set v:exception. If "oldval" == NULL, return the current value.
18445 * Otherwise, restore the value to "oldval" and return NULL.
18446 * Must always be called in pairs to save and restore v:exception! Does not
18447 * take care of memory allocations.
18448 */
18449 char_u *
18450v_exception(oldval)
18451 char_u *oldval;
18452{
18453 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018454 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455
Bram Moolenaare9a41262005-01-15 22:18:47 +000018456 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457 return NULL;
18458}
18459
18460/*
18461 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18462 * Otherwise, restore the value to "oldval" and return NULL.
18463 * Must always be called in pairs to save and restore v:throwpoint! Does not
18464 * take care of memory allocations.
18465 */
18466 char_u *
18467v_throwpoint(oldval)
18468 char_u *oldval;
18469{
18470 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018471 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472
Bram Moolenaare9a41262005-01-15 22:18:47 +000018473 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474 return NULL;
18475}
18476
18477#if defined(FEAT_AUTOCMD) || defined(PROTO)
18478/*
18479 * Set v:cmdarg.
18480 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18481 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18482 * Must always be called in pairs!
18483 */
18484 char_u *
18485set_cmdarg(eap, oldarg)
18486 exarg_T *eap;
18487 char_u *oldarg;
18488{
18489 char_u *oldval;
18490 char_u *newval;
18491 unsigned len;
18492
Bram Moolenaare9a41262005-01-15 22:18:47 +000018493 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018494 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018496 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018497 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018498 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499 }
18500
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018501 if (eap->force_bin == FORCE_BIN)
18502 len = 6;
18503 else if (eap->force_bin == FORCE_NOBIN)
18504 len = 8;
18505 else
18506 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018507
18508 if (eap->read_edit)
18509 len += 7;
18510
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018511 if (eap->force_ff != 0)
18512 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18513# ifdef FEAT_MBYTE
18514 if (eap->force_enc != 0)
18515 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018516 if (eap->bad_char != 0)
18517 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018518# endif
18519
18520 newval = alloc(len + 1);
18521 if (newval == NULL)
18522 return NULL;
18523
18524 if (eap->force_bin == FORCE_BIN)
18525 sprintf((char *)newval, " ++bin");
18526 else if (eap->force_bin == FORCE_NOBIN)
18527 sprintf((char *)newval, " ++nobin");
18528 else
18529 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018530
18531 if (eap->read_edit)
18532 STRCAT(newval, " ++edit");
18533
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018534 if (eap->force_ff != 0)
18535 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18536 eap->cmd + eap->force_ff);
18537# ifdef FEAT_MBYTE
18538 if (eap->force_enc != 0)
18539 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18540 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018541 if (eap->bad_char == BAD_KEEP)
18542 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18543 else if (eap->bad_char == BAD_DROP)
18544 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18545 else if (eap->bad_char != 0)
18546 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018547# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018548 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018549 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550}
18551#endif
18552
18553/*
18554 * Get the value of internal variable "name".
18555 * Return OK or FAIL.
18556 */
18557 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018558get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 char_u *name;
18560 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018561 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018562 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563{
18564 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018565 typval_T *tv = NULL;
18566 typval_T atv;
18567 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569
18570 /* truncate the name, so that we can use strcmp() */
18571 cc = name[len];
18572 name[len] = NUL;
18573
18574 /*
18575 * Check for "b:changedtick".
18576 */
18577 if (STRCMP(name, "b:changedtick") == 0)
18578 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018579 atv.v_type = VAR_NUMBER;
18580 atv.vval.v_number = curbuf->b_changedtick;
18581 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 }
18583
18584 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585 * Check for user-defined variables.
18586 */
18587 else
18588 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018589 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018591 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592 }
18593
Bram Moolenaare9a41262005-01-15 22:18:47 +000018594 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018596 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018597 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 ret = FAIL;
18599 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018600 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018601 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602
18603 name[len] = cc;
18604
18605 return ret;
18606}
18607
18608/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018609 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18610 * Also handle function call with Funcref variable: func(expr)
18611 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18612 */
18613 static int
18614handle_subscript(arg, rettv, evaluate, verbose)
18615 char_u **arg;
18616 typval_T *rettv;
18617 int evaluate; /* do more than finding the end */
18618 int verbose; /* give error messages */
18619{
18620 int ret = OK;
18621 dict_T *selfdict = NULL;
18622 char_u *s;
18623 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018624 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018625
18626 while (ret == OK
18627 && (**arg == '['
18628 || (**arg == '.' && rettv->v_type == VAR_DICT)
18629 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18630 && !vim_iswhite(*(*arg - 1)))
18631 {
18632 if (**arg == '(')
18633 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018634 /* need to copy the funcref so that we can clear rettv */
18635 functv = *rettv;
18636 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018637
18638 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018639 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018640 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018641 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18642 &len, evaluate, selfdict);
18643
18644 /* Clear the funcref afterwards, so that deleting it while
18645 * evaluating the arguments is possible (see test55). */
18646 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018647
18648 /* Stop the expression evaluation when immediately aborting on
18649 * error, or when an interrupt occurred or an exception was thrown
18650 * but not caught. */
18651 if (aborting())
18652 {
18653 if (ret == OK)
18654 clear_tv(rettv);
18655 ret = FAIL;
18656 }
18657 dict_unref(selfdict);
18658 selfdict = NULL;
18659 }
18660 else /* **arg == '[' || **arg == '.' */
18661 {
18662 dict_unref(selfdict);
18663 if (rettv->v_type == VAR_DICT)
18664 {
18665 selfdict = rettv->vval.v_dict;
18666 if (selfdict != NULL)
18667 ++selfdict->dv_refcount;
18668 }
18669 else
18670 selfdict = NULL;
18671 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18672 {
18673 clear_tv(rettv);
18674 ret = FAIL;
18675 }
18676 }
18677 }
18678 dict_unref(selfdict);
18679 return ret;
18680}
18681
18682/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018683 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018684 * value).
18685 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018686 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018687alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018688{
Bram Moolenaar33570922005-01-25 22:26:29 +000018689 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018690}
18691
18692/*
18693 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694 * The string "s" must have been allocated, it is consumed.
18695 * Return NULL for out of memory, the variable otherwise.
18696 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018697 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018698alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699 char_u *s;
18700{
Bram Moolenaar33570922005-01-25 22:26:29 +000018701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018703 rettv = alloc_tv();
18704 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018706 rettv->v_type = VAR_STRING;
18707 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708 }
18709 else
18710 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018711 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712}
18713
18714/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018715 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018717 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018718free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018719 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720{
18721 if (varp != NULL)
18722 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018723 switch (varp->v_type)
18724 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018725 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018726 func_unref(varp->vval.v_string);
18727 /*FALLTHROUGH*/
18728 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018729 vim_free(varp->vval.v_string);
18730 break;
18731 case VAR_LIST:
18732 list_unref(varp->vval.v_list);
18733 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018734 case VAR_DICT:
18735 dict_unref(varp->vval.v_dict);
18736 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018737 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018738#ifdef FEAT_FLOAT
18739 case VAR_FLOAT:
18740#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018741 case VAR_UNKNOWN:
18742 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018743 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018744 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018745 break;
18746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747 vim_free(varp);
18748 }
18749}
18750
18751/*
18752 * Free the memory for a variable value and set the value to NULL or 0.
18753 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018754 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018755clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018756 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757{
18758 if (varp != NULL)
18759 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018760 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018762 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018763 func_unref(varp->vval.v_string);
18764 /*FALLTHROUGH*/
18765 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018766 vim_free(varp->vval.v_string);
18767 varp->vval.v_string = NULL;
18768 break;
18769 case VAR_LIST:
18770 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018771 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018772 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018773 case VAR_DICT:
18774 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018775 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018776 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018777 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018778 varp->vval.v_number = 0;
18779 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018780#ifdef FEAT_FLOAT
18781 case VAR_FLOAT:
18782 varp->vval.v_float = 0.0;
18783 break;
18784#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018785 case VAR_UNKNOWN:
18786 break;
18787 default:
18788 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018790 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 }
18792}
18793
18794/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018795 * Set the value of a variable to NULL without freeing items.
18796 */
18797 static void
18798init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018799 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018800{
18801 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018802 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018803}
18804
18805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806 * Get the number value of a variable.
18807 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018808 * For incompatible types, return 0.
18809 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18810 * caller of incompatible types: it sets *denote to TRUE if "denote"
18811 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 */
18813 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018814get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018815 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018817 int error = FALSE;
18818
18819 return get_tv_number_chk(varp, &error); /* return 0L on error */
18820}
18821
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018822 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018823get_tv_number_chk(varp, denote)
18824 typval_T *varp;
18825 int *denote;
18826{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018827 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018829 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018831 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018832 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018833#ifdef FEAT_FLOAT
18834 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018835 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018836 break;
18837#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018838 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018839 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018840 break;
18841 case VAR_STRING:
18842 if (varp->vval.v_string != NULL)
18843 vim_str2nr(varp->vval.v_string, NULL, NULL,
18844 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018845 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018846 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018847 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018848 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018849 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018850 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018851 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018852 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018853 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018854 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018856 if (denote == NULL) /* useful for values that must be unsigned */
18857 n = -1;
18858 else
18859 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018860 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861}
18862
18863/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018864 * Get the lnum from the first argument.
18865 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018866 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867 */
18868 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018869get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000018870 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871{
Bram Moolenaar33570922005-01-25 22:26:29 +000018872 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018873 linenr_T lnum;
18874
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018875 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876 if (lnum == 0) /* no valid number, try using line() */
18877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018878 rettv.v_type = VAR_NUMBER;
18879 f_line(argvars, &rettv);
18880 lnum = rettv.vval.v_number;
18881 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018882 }
18883 return lnum;
18884}
18885
18886/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018887 * Get the lnum from the first argument.
18888 * Also accepts "$", then "buf" is used.
18889 * Returns 0 on error.
18890 */
18891 static linenr_T
18892get_tv_lnum_buf(argvars, buf)
18893 typval_T *argvars;
18894 buf_T *buf;
18895{
18896 if (argvars[0].v_type == VAR_STRING
18897 && argvars[0].vval.v_string != NULL
18898 && argvars[0].vval.v_string[0] == '$'
18899 && buf != NULL)
18900 return buf->b_ml.ml_line_count;
18901 return get_tv_number_chk(&argvars[0], NULL);
18902}
18903
18904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905 * Get the string value of a variable.
18906 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000018907 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18908 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909 * If the String variable has never been set, return an empty string.
18910 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018911 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18912 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913 */
18914 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018915get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018916 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917{
18918 static char_u mybuf[NUMBUFLEN];
18919
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018920 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921}
18922
18923 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018924get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000018925 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018926 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018928 char_u *res = get_tv_string_buf_chk(varp, buf);
18929
18930 return res != NULL ? res : (char_u *)"";
18931}
18932
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018933 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018934get_tv_string_chk(varp)
18935 typval_T *varp;
18936{
18937 static char_u mybuf[NUMBUFLEN];
18938
18939 return get_tv_string_buf_chk(varp, mybuf);
18940}
18941
18942 static char_u *
18943get_tv_string_buf_chk(varp, buf)
18944 typval_T *varp;
18945 char_u *buf;
18946{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018947 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018949 case VAR_NUMBER:
18950 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
18951 return buf;
18952 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018953 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018954 break;
18955 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018956 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000018957 break;
18958 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018959 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018960 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018961#ifdef FEAT_FLOAT
18962 case VAR_FLOAT:
18963 EMSG(_("E806: using Float as a String"));
18964 break;
18965#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018966 case VAR_STRING:
18967 if (varp->vval.v_string != NULL)
18968 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018969 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018970 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018971 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018972 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018974 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975}
18976
18977/*
18978 * Find variable "name" in the list of variables.
18979 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018980 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018981 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000018982 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018984 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018985find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018987 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018990 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991
Bram Moolenaara7043832005-01-21 11:56:39 +000018992 ht = find_var_ht(name, &varname);
18993 if (htp != NULL)
18994 *htp = ht;
18995 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018997 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998}
18999
19000/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019001 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019002 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019004 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019005find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019006 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019007 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019008 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019009{
Bram Moolenaar33570922005-01-25 22:26:29 +000019010 hashitem_T *hi;
19011
19012 if (*varname == NUL)
19013 {
19014 /* Must be something like "s:", otherwise "ht" would be NULL. */
19015 switch (varname[-2])
19016 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019017 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019018 case 'g': return &globvars_var;
19019 case 'v': return &vimvars_var;
19020 case 'b': return &curbuf->b_bufvar;
19021 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019022#ifdef FEAT_WINDOWS
19023 case 't': return &curtab->tp_winvar;
19024#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019025 case 'l': return current_funccal == NULL
19026 ? NULL : &current_funccal->l_vars_var;
19027 case 'a': return current_funccal == NULL
19028 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019029 }
19030 return NULL;
19031 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019032
19033 hi = hash_find(ht, varname);
19034 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019035 {
19036 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019037 * worked find the variable again. Don't auto-load a script if it was
19038 * loaded already, otherwise it would be loaded every time when
19039 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019040 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019041 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019042 hi = hash_find(ht, varname);
19043 if (HASHITEM_EMPTY(hi))
19044 return NULL;
19045 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019046 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019047}
19048
19049/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019050 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019051 * Set "varname" to the start of name without ':'.
19052 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019053 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019054find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 char_u *name;
19056 char_u **varname;
19057{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019058 hashitem_T *hi;
19059
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060 if (name[1] != ':')
19061 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019062 /* The name must not start with a colon or #. */
19063 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064 return NULL;
19065 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019066
19067 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019068 hi = hash_find(&compat_hashtab, name);
19069 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019070 return &compat_hashtab;
19071
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019073 return &globvarht; /* global variable */
19074 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075 }
19076 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019077 if (*name == 'g') /* global variable */
19078 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019079 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19080 */
19081 if (vim_strchr(name + 2, ':') != NULL
19082 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019083 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019085 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019087 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019088#ifdef FEAT_WINDOWS
19089 if (*name == 't') /* tab page variable */
19090 return &curtab->tp_vars.dv_hashtab;
19091#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019092 if (*name == 'v') /* v: variable */
19093 return &vimvarht;
19094 if (*name == 'a' && current_funccal != NULL) /* function argument */
19095 return &current_funccal->l_avars.dv_hashtab;
19096 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19097 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 if (*name == 's' /* script variable */
19099 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19100 return &SCRIPT_VARS(current_SID);
19101 return NULL;
19102}
19103
19104/*
19105 * Get the string value of a (global/local) variable.
19106 * Returns NULL when it doesn't exist.
19107 */
19108 char_u *
19109get_var_value(name)
19110 char_u *name;
19111{
Bram Moolenaar33570922005-01-25 22:26:29 +000019112 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113
Bram Moolenaara7043832005-01-21 11:56:39 +000019114 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 if (v == NULL)
19116 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019117 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118}
19119
19120/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019121 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 * sourcing this script and when executing functions defined in the script.
19123 */
19124 void
19125new_script_vars(id)
19126 scid_T id;
19127{
Bram Moolenaara7043832005-01-21 11:56:39 +000019128 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019129 hashtab_T *ht;
19130 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019131
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19133 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019134 /* Re-allocating ga_data means that an ht_array pointing to
19135 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019136 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019137 for (i = 1; i <= ga_scripts.ga_len; ++i)
19138 {
19139 ht = &SCRIPT_VARS(i);
19140 if (ht->ht_mask == HT_INIT_SIZE - 1)
19141 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019142 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019143 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019144 }
19145
Bram Moolenaar071d4272004-06-13 20:20:40 +000019146 while (ga_scripts.ga_len < id)
19147 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019148 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
19149 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019150 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019151 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152 }
19153 }
19154}
19155
19156/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019157 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19158 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159 */
19160 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019161init_var_dict(dict, dict_var)
19162 dict_T *dict;
19163 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164{
Bram Moolenaar33570922005-01-25 22:26:29 +000019165 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019166 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019167 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019168 dict_var->di_tv.vval.v_dict = dict;
19169 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019170 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019171 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19172 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173}
19174
19175/*
19176 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019177 * Frees all allocated variables and the value they contain.
19178 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179 */
19180 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019181vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019182 hashtab_T *ht;
19183{
19184 vars_clear_ext(ht, TRUE);
19185}
19186
19187/*
19188 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19189 */
19190 static void
19191vars_clear_ext(ht, free_val)
19192 hashtab_T *ht;
19193 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019194{
Bram Moolenaara7043832005-01-21 11:56:39 +000019195 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019196 hashitem_T *hi;
19197 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019198
Bram Moolenaar33570922005-01-25 22:26:29 +000019199 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019200 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019201 for (hi = ht->ht_array; todo > 0; ++hi)
19202 {
19203 if (!HASHITEM_EMPTY(hi))
19204 {
19205 --todo;
19206
Bram Moolenaar33570922005-01-25 22:26:29 +000019207 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019208 * ht_array might change then. hash_clear() takes care of it
19209 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019210 v = HI2DI(hi);
19211 if (free_val)
19212 clear_tv(&v->di_tv);
19213 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19214 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019215 }
19216 }
19217 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019218 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219}
19220
Bram Moolenaara7043832005-01-21 11:56:39 +000019221/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019222 * Delete a variable from hashtab "ht" at item "hi".
19223 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019226delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019227 hashtab_T *ht;
19228 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229{
Bram Moolenaar33570922005-01-25 22:26:29 +000019230 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019231
19232 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019233 clear_tv(&di->di_tv);
19234 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235}
19236
19237/*
19238 * List the value of one internal variable.
19239 */
19240 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019241list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019242 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019243 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019244 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019246 char_u *tofree;
19247 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019248 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019249
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019250 current_copyID += COPYID_INC;
19251 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019252 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019253 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019254 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255}
19256
Bram Moolenaar071d4272004-06-13 20:20:40 +000019257 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019258list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259 char_u *prefix;
19260 char_u *name;
19261 int type;
19262 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019263 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264{
Bram Moolenaar31859182007-08-14 20:41:13 +000019265 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19266 msg_start();
19267 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019268 if (name != NULL) /* "a:" vars don't have a name stored */
19269 msg_puts(name);
19270 msg_putchar(' ');
19271 msg_advance(22);
19272 if (type == VAR_NUMBER)
19273 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019274 else if (type == VAR_FUNC)
19275 msg_putchar('*');
19276 else if (type == VAR_LIST)
19277 {
19278 msg_putchar('[');
19279 if (*string == '[')
19280 ++string;
19281 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019282 else if (type == VAR_DICT)
19283 {
19284 msg_putchar('{');
19285 if (*string == '{')
19286 ++string;
19287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288 else
19289 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019290
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019292
19293 if (type == VAR_FUNC)
19294 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019295 if (*first)
19296 {
19297 msg_clr_eos();
19298 *first = FALSE;
19299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019300}
19301
19302/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019303 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 * If the variable already exists, the value is updated.
19305 * Otherwise the variable is created.
19306 */
19307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019308set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019310 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019311 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019312{
Bram Moolenaar33570922005-01-25 22:26:29 +000019313 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019315 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019316 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019318 ht = find_var_ht(name, &varname);
19319 if (ht == NULL || *varname == NUL)
19320 {
19321 EMSG2(_(e_illvar), name);
19322 return;
19323 }
19324 v = find_var_in_ht(ht, varname, TRUE);
19325
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019326 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019327 {
19328 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19329 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19330 ? name[2] : name[0]))
19331 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019332 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019333 return;
19334 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019335 /* Don't allow hiding a function. When "v" is not NULL we migth be
19336 * assigning another function to the same var, the type is checked
19337 * below. */
19338 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019339 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019340 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019341 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019342 return;
19343 }
19344 }
19345
Bram Moolenaar33570922005-01-25 22:26:29 +000019346 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019348 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019349 if (var_check_ro(v->di_flags, name)
19350 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019351 return;
19352 if (v->di_tv.v_type != tv->v_type
19353 && !((v->di_tv.v_type == VAR_STRING
19354 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019355 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019356 || tv->v_type == VAR_NUMBER))
19357#ifdef FEAT_FLOAT
19358 && !((v->di_tv.v_type == VAR_NUMBER
19359 || v->di_tv.v_type == VAR_FLOAT)
19360 && (tv->v_type == VAR_NUMBER
19361 || tv->v_type == VAR_FLOAT))
19362#endif
19363 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019364 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019365 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019366 return;
19367 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019368
19369 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019370 * Handle setting internal v: variables separately: we don't change
19371 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019372 */
19373 if (ht == &vimvarht)
19374 {
19375 if (v->di_tv.v_type == VAR_STRING)
19376 {
19377 vim_free(v->di_tv.vval.v_string);
19378 if (copy || tv->v_type != VAR_STRING)
19379 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19380 else
19381 {
19382 /* Take over the string to avoid an extra alloc/free. */
19383 v->di_tv.vval.v_string = tv->vval.v_string;
19384 tv->vval.v_string = NULL;
19385 }
19386 }
19387 else if (v->di_tv.v_type != VAR_NUMBER)
19388 EMSG2(_(e_intern2), "set_var()");
19389 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019390 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019391 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019392 if (STRCMP(varname, "searchforward") == 0)
19393 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19394 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019395 return;
19396 }
19397
19398 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399 }
19400 else /* add a new variable */
19401 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019402 /* Can't add "v:" variable. */
19403 if (ht == &vimvarht)
19404 {
19405 EMSG2(_(e_illvar), name);
19406 return;
19407 }
19408
Bram Moolenaar92124a32005-06-17 22:03:40 +000019409 /* Make sure the variable name is valid. */
19410 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019411 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19412 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019413 {
19414 EMSG2(_(e_illvar), varname);
19415 return;
19416 }
19417
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019418 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19419 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019420 if (v == NULL)
19421 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019422 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019423 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019425 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019426 return;
19427 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019428 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019430
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019431 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019432 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019433 else
19434 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019435 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019436 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019437 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439}
19440
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019441/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019442 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019443 * Also give an error message.
19444 */
19445 static int
19446var_check_ro(flags, name)
19447 int flags;
19448 char_u *name;
19449{
19450 if (flags & DI_FLAGS_RO)
19451 {
19452 EMSG2(_(e_readonlyvar), name);
19453 return TRUE;
19454 }
19455 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19456 {
19457 EMSG2(_(e_readonlysbx), name);
19458 return TRUE;
19459 }
19460 return FALSE;
19461}
19462
19463/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019464 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19465 * Also give an error message.
19466 */
19467 static int
19468var_check_fixed(flags, name)
19469 int flags;
19470 char_u *name;
19471{
19472 if (flags & DI_FLAGS_FIX)
19473 {
19474 EMSG2(_("E795: Cannot delete variable %s"), name);
19475 return TRUE;
19476 }
19477 return FALSE;
19478}
19479
19480/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019481 * Return TRUE if typeval "tv" is set to be locked (immutable).
19482 * Also give an error message, using "name".
19483 */
19484 static int
19485tv_check_lock(lock, name)
19486 int lock;
19487 char_u *name;
19488{
19489 if (lock & VAR_LOCKED)
19490 {
19491 EMSG2(_("E741: Value is locked: %s"),
19492 name == NULL ? (char_u *)_("Unknown") : name);
19493 return TRUE;
19494 }
19495 if (lock & VAR_FIXED)
19496 {
19497 EMSG2(_("E742: Cannot change value of %s"),
19498 name == NULL ? (char_u *)_("Unknown") : name);
19499 return TRUE;
19500 }
19501 return FALSE;
19502}
19503
19504/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019505 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019506 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019507 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019508 * It is OK for "from" and "to" to point to the same item. This is used to
19509 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019510 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019511 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019512copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019513 typval_T *from;
19514 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019516 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019517 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019518 switch (from->v_type)
19519 {
19520 case VAR_NUMBER:
19521 to->vval.v_number = from->vval.v_number;
19522 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019523#ifdef FEAT_FLOAT
19524 case VAR_FLOAT:
19525 to->vval.v_float = from->vval.v_float;
19526 break;
19527#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019528 case VAR_STRING:
19529 case VAR_FUNC:
19530 if (from->vval.v_string == NULL)
19531 to->vval.v_string = NULL;
19532 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019533 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019534 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019535 if (from->v_type == VAR_FUNC)
19536 func_ref(to->vval.v_string);
19537 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019538 break;
19539 case VAR_LIST:
19540 if (from->vval.v_list == NULL)
19541 to->vval.v_list = NULL;
19542 else
19543 {
19544 to->vval.v_list = from->vval.v_list;
19545 ++to->vval.v_list->lv_refcount;
19546 }
19547 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019548 case VAR_DICT:
19549 if (from->vval.v_dict == NULL)
19550 to->vval.v_dict = NULL;
19551 else
19552 {
19553 to->vval.v_dict = from->vval.v_dict;
19554 ++to->vval.v_dict->dv_refcount;
19555 }
19556 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019557 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019558 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019559 break;
19560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561}
19562
19563/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019564 * Make a copy of an item.
19565 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019566 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19567 * reference to an already copied list/dict can be used.
19568 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019569 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019570 static int
19571item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019572 typval_T *from;
19573 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019574 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019575 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019576{
19577 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019578 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019579
Bram Moolenaar33570922005-01-25 22:26:29 +000019580 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019581 {
19582 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019583 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019584 }
19585 ++recurse;
19586
19587 switch (from->v_type)
19588 {
19589 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019590#ifdef FEAT_FLOAT
19591 case VAR_FLOAT:
19592#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019593 case VAR_STRING:
19594 case VAR_FUNC:
19595 copy_tv(from, to);
19596 break;
19597 case VAR_LIST:
19598 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019599 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019600 if (from->vval.v_list == NULL)
19601 to->vval.v_list = NULL;
19602 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19603 {
19604 /* use the copy made earlier */
19605 to->vval.v_list = from->vval.v_list->lv_copylist;
19606 ++to->vval.v_list->lv_refcount;
19607 }
19608 else
19609 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19610 if (to->vval.v_list == NULL)
19611 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019612 break;
19613 case VAR_DICT:
19614 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019615 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019616 if (from->vval.v_dict == NULL)
19617 to->vval.v_dict = NULL;
19618 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19619 {
19620 /* use the copy made earlier */
19621 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19622 ++to->vval.v_dict->dv_refcount;
19623 }
19624 else
19625 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19626 if (to->vval.v_dict == NULL)
19627 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019628 break;
19629 default:
19630 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019631 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019632 }
19633 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019634 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019635}
19636
19637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638 * ":echo expr1 ..." print each argument separated with a space, add a
19639 * newline at the end.
19640 * ":echon expr1 ..." print each argument plain.
19641 */
19642 void
19643ex_echo(eap)
19644 exarg_T *eap;
19645{
19646 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019647 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019648 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649 char_u *p;
19650 int needclr = TRUE;
19651 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019652 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653
19654 if (eap->skip)
19655 ++emsg_skip;
19656 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19657 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019658 /* If eval1() causes an error message the text from the command may
19659 * still need to be cleared. E.g., "echo 22,44". */
19660 need_clr_eos = needclr;
19661
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019663 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664 {
19665 /*
19666 * Report the invalid expression unless the expression evaluation
19667 * has been cancelled due to an aborting error, an interrupt, or an
19668 * exception.
19669 */
19670 if (!aborting())
19671 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019672 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673 break;
19674 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019675 need_clr_eos = FALSE;
19676
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677 if (!eap->skip)
19678 {
19679 if (atstart)
19680 {
19681 atstart = FALSE;
19682 /* Call msg_start() after eval1(), evaluating the expression
19683 * may cause a message to appear. */
19684 if (eap->cmdidx == CMD_echo)
19685 msg_start();
19686 }
19687 else if (eap->cmdidx == CMD_echo)
19688 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019689 current_copyID += COPYID_INC;
19690 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019691 if (p != NULL)
19692 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019694 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019696 if (*p != TAB && needclr)
19697 {
19698 /* remove any text still there from the command */
19699 msg_clr_eos();
19700 needclr = FALSE;
19701 }
19702 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703 }
19704 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019705 {
19706#ifdef FEAT_MBYTE
19707 if (has_mbyte)
19708 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019709 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019710
19711 (void)msg_outtrans_len_attr(p, i, echo_attr);
19712 p += i - 1;
19713 }
19714 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019716 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019719 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019721 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722 arg = skipwhite(arg);
19723 }
19724 eap->nextcmd = check_nextcmd(arg);
19725
19726 if (eap->skip)
19727 --emsg_skip;
19728 else
19729 {
19730 /* remove text that may still be there from the command */
19731 if (needclr)
19732 msg_clr_eos();
19733 if (eap->cmdidx == CMD_echo)
19734 msg_end();
19735 }
19736}
19737
19738/*
19739 * ":echohl {name}".
19740 */
19741 void
19742ex_echohl(eap)
19743 exarg_T *eap;
19744{
19745 int id;
19746
19747 id = syn_name2id(eap->arg);
19748 if (id == 0)
19749 echo_attr = 0;
19750 else
19751 echo_attr = syn_id2attr(id);
19752}
19753
19754/*
19755 * ":execute expr1 ..." execute the result of an expression.
19756 * ":echomsg expr1 ..." Print a message
19757 * ":echoerr expr1 ..." Print an error
19758 * Each gets spaces around each argument and a newline at the end for
19759 * echo commands
19760 */
19761 void
19762ex_execute(eap)
19763 exarg_T *eap;
19764{
19765 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019766 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 int ret = OK;
19768 char_u *p;
19769 garray_T ga;
19770 int len;
19771 int save_did_emsg;
19772
19773 ga_init2(&ga, 1, 80);
19774
19775 if (eap->skip)
19776 ++emsg_skip;
19777 while (*arg != NUL && *arg != '|' && *arg != '\n')
19778 {
19779 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019780 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781 {
19782 /*
19783 * Report the invalid expression unless the expression evaluation
19784 * has been cancelled due to an aborting error, an interrupt, or an
19785 * exception.
19786 */
19787 if (!aborting())
19788 EMSG2(_(e_invexpr2), p);
19789 ret = FAIL;
19790 break;
19791 }
19792
19793 if (!eap->skip)
19794 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019795 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796 len = (int)STRLEN(p);
19797 if (ga_grow(&ga, len + 2) == FAIL)
19798 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019799 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800 ret = FAIL;
19801 break;
19802 }
19803 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806 ga.ga_len += len;
19807 }
19808
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019809 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 arg = skipwhite(arg);
19811 }
19812
19813 if (ret != FAIL && ga.ga_data != NULL)
19814 {
19815 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019816 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019818 out_flush();
19819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 else if (eap->cmdidx == CMD_echoerr)
19821 {
19822 /* We don't want to abort following commands, restore did_emsg. */
19823 save_did_emsg = did_emsg;
19824 EMSG((char_u *)ga.ga_data);
19825 if (!force_abort)
19826 did_emsg = save_did_emsg;
19827 }
19828 else if (eap->cmdidx == CMD_execute)
19829 do_cmdline((char_u *)ga.ga_data,
19830 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19831 }
19832
19833 ga_clear(&ga);
19834
19835 if (eap->skip)
19836 --emsg_skip;
19837
19838 eap->nextcmd = check_nextcmd(arg);
19839}
19840
19841/*
19842 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19843 * "arg" points to the "&" or '+' when called, to "option" when returning.
19844 * Returns NULL when no option name found. Otherwise pointer to the char
19845 * after the option name.
19846 */
19847 static char_u *
19848find_option_end(arg, opt_flags)
19849 char_u **arg;
19850 int *opt_flags;
19851{
19852 char_u *p = *arg;
19853
19854 ++p;
19855 if (*p == 'g' && p[1] == ':')
19856 {
19857 *opt_flags = OPT_GLOBAL;
19858 p += 2;
19859 }
19860 else if (*p == 'l' && p[1] == ':')
19861 {
19862 *opt_flags = OPT_LOCAL;
19863 p += 2;
19864 }
19865 else
19866 *opt_flags = 0;
19867
19868 if (!ASCII_ISALPHA(*p))
19869 return NULL;
19870 *arg = p;
19871
19872 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
19873 p += 4; /* termcap option */
19874 else
19875 while (ASCII_ISALPHA(*p))
19876 ++p;
19877 return p;
19878}
19879
19880/*
19881 * ":function"
19882 */
19883 void
19884ex_function(eap)
19885 exarg_T *eap;
19886{
19887 char_u *theline;
19888 int j;
19889 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891 char_u *name = NULL;
19892 char_u *p;
19893 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019894 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895 garray_T newargs;
19896 garray_T newlines;
19897 int varargs = FALSE;
19898 int mustend = FALSE;
19899 int flags = 0;
19900 ufunc_T *fp;
19901 int indent;
19902 int nesting;
19903 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019904 dictitem_T *v;
19905 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019906 static int func_nr = 0; /* number for nameless function */
19907 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019908 hashtab_T *ht;
19909 int todo;
19910 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019911 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019912
19913 /*
19914 * ":function" without argument: list functions.
19915 */
19916 if (ends_excmd(*eap->arg))
19917 {
19918 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019919 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019920 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000019921 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019922 {
19923 if (!HASHITEM_EMPTY(hi))
19924 {
19925 --todo;
19926 fp = HI2UF(hi);
19927 if (!isdigit(*fp->uf_name))
19928 list_func_head(fp, FALSE);
19929 }
19930 }
19931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932 eap->nextcmd = check_nextcmd(eap->arg);
19933 return;
19934 }
19935
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019936 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019937 * ":function /pat": list functions matching pattern.
19938 */
19939 if (*eap->arg == '/')
19940 {
19941 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
19942 if (!eap->skip)
19943 {
19944 regmatch_T regmatch;
19945
19946 c = *p;
19947 *p = NUL;
19948 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
19949 *p = c;
19950 if (regmatch.regprog != NULL)
19951 {
19952 regmatch.rm_ic = p_ic;
19953
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019954 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019955 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
19956 {
19957 if (!HASHITEM_EMPTY(hi))
19958 {
19959 --todo;
19960 fp = HI2UF(hi);
19961 if (!isdigit(*fp->uf_name)
19962 && vim_regexec(&regmatch, fp->uf_name, 0))
19963 list_func_head(fp, FALSE);
19964 }
19965 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000019966 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019967 }
19968 }
19969 if (*p == '/')
19970 ++p;
19971 eap->nextcmd = check_nextcmd(p);
19972 return;
19973 }
19974
19975 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019976 * Get the function name. There are these situations:
19977 * func normal function name
19978 * "name" == func, "fudi.fd_dict" == NULL
19979 * dict.func new dictionary entry
19980 * "name" == NULL, "fudi.fd_dict" set,
19981 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19982 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019983 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019984 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19985 * dict.func existing dict entry that's not a Funcref
19986 * "name" == NULL, "fudi.fd_dict" set,
19987 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019990 name = trans_function_name(&p, eap->skip, 0, &fudi);
19991 paren = (vim_strchr(p, '(') != NULL);
19992 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993 {
19994 /*
19995 * Return on an invalid expression in braces, unless the expression
19996 * evaluation has been cancelled due to an aborting error, an
19997 * interrupt, or an exception.
19998 */
19999 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020000 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020001 if (!eap->skip && fudi.fd_newkey != NULL)
20002 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020003 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 else
20007 eap->skip = TRUE;
20008 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020009
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010 /* An error in a function call during evaluation of an expression in magic
20011 * braces should not cause the function not to be defined. */
20012 saved_did_emsg = did_emsg;
20013 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014
20015 /*
20016 * ":function func" with only function name: list function.
20017 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020018 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 {
20020 if (!ends_excmd(*skipwhite(p)))
20021 {
20022 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020023 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024 }
20025 eap->nextcmd = check_nextcmd(p);
20026 if (eap->nextcmd != NULL)
20027 *p = NUL;
20028 if (!eap->skip && !got_int)
20029 {
20030 fp = find_func(name);
20031 if (fp != NULL)
20032 {
20033 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020034 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020036 if (FUNCLINE(fp, j) == NULL)
20037 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038 msg_putchar('\n');
20039 msg_outnum((long)(j + 1));
20040 if (j < 9)
20041 msg_putchar(' ');
20042 if (j < 99)
20043 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020044 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 out_flush(); /* show a line at a time */
20046 ui_breakcheck();
20047 }
20048 if (!got_int)
20049 {
20050 msg_putchar('\n');
20051 msg_puts((char_u *)" endfunction");
20052 }
20053 }
20054 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020055 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020057 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 }
20059
20060 /*
20061 * ":function name(arg1, arg2)" Define function.
20062 */
20063 p = skipwhite(p);
20064 if (*p != '(')
20065 {
20066 if (!eap->skip)
20067 {
20068 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020069 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070 }
20071 /* attempt to continue by skipping some text */
20072 if (vim_strchr(p, '(') != NULL)
20073 p = vim_strchr(p, '(');
20074 }
20075 p = skipwhite(p + 1);
20076
20077 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20078 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20079
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020080 if (!eap->skip)
20081 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020082 /* Check the name of the function. Unless it's a dictionary function
20083 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020084 if (name != NULL)
20085 arg = name;
20086 else
20087 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020088 if (arg != NULL && (fudi.fd_di == NULL
20089 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020090 {
20091 if (*arg == K_SPECIAL)
20092 j = 3;
20093 else
20094 j = 0;
20095 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20096 : eval_isnamec(arg[j])))
20097 ++j;
20098 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020099 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020100 }
20101 }
20102
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103 /*
20104 * Isolate the arguments: "arg1, arg2, ...)"
20105 */
20106 while (*p != ')')
20107 {
20108 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20109 {
20110 varargs = TRUE;
20111 p += 3;
20112 mustend = TRUE;
20113 }
20114 else
20115 {
20116 arg = p;
20117 while (ASCII_ISALNUM(*p) || *p == '_')
20118 ++p;
20119 if (arg == p || isdigit(*arg)
20120 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20121 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20122 {
20123 if (!eap->skip)
20124 EMSG2(_("E125: Illegal argument: %s"), arg);
20125 break;
20126 }
20127 if (ga_grow(&newargs, 1) == FAIL)
20128 goto erret;
20129 c = *p;
20130 *p = NUL;
20131 arg = vim_strsave(arg);
20132 if (arg == NULL)
20133 goto erret;
20134 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20135 *p = c;
20136 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 if (*p == ',')
20138 ++p;
20139 else
20140 mustend = TRUE;
20141 }
20142 p = skipwhite(p);
20143 if (mustend && *p != ')')
20144 {
20145 if (!eap->skip)
20146 EMSG2(_(e_invarg2), eap->arg);
20147 break;
20148 }
20149 }
20150 ++p; /* skip the ')' */
20151
Bram Moolenaare9a41262005-01-15 22:18:47 +000020152 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153 for (;;)
20154 {
20155 p = skipwhite(p);
20156 if (STRNCMP(p, "range", 5) == 0)
20157 {
20158 flags |= FC_RANGE;
20159 p += 5;
20160 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020161 else if (STRNCMP(p, "dict", 4) == 0)
20162 {
20163 flags |= FC_DICT;
20164 p += 4;
20165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166 else if (STRNCMP(p, "abort", 5) == 0)
20167 {
20168 flags |= FC_ABORT;
20169 p += 5;
20170 }
20171 else
20172 break;
20173 }
20174
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020175 /* When there is a line break use what follows for the function body.
20176 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20177 if (*p == '\n')
20178 line_arg = p + 1;
20179 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180 EMSG(_(e_trailing));
20181
20182 /*
20183 * Read the body of the function, until ":endfunction" is found.
20184 */
20185 if (KeyTyped)
20186 {
20187 /* Check if the function already exists, don't let the user type the
20188 * whole function before telling him it doesn't work! For a script we
20189 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020190 if (!eap->skip && !eap->forceit)
20191 {
20192 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20193 EMSG(_(e_funcdict));
20194 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020195 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020198 if (!eap->skip && did_emsg)
20199 goto erret;
20200
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 msg_putchar('\n'); /* don't overwrite the function name */
20202 cmdline_row = msg_row;
20203 }
20204
20205 indent = 2;
20206 nesting = 0;
20207 for (;;)
20208 {
20209 msg_scroll = TRUE;
20210 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020211 sourcing_lnum_off = sourcing_lnum;
20212
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020213 if (line_arg != NULL)
20214 {
20215 /* Use eap->arg, split up in parts by line breaks. */
20216 theline = line_arg;
20217 p = vim_strchr(theline, '\n');
20218 if (p == NULL)
20219 line_arg += STRLEN(line_arg);
20220 else
20221 {
20222 *p = NUL;
20223 line_arg = p + 1;
20224 }
20225 }
20226 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 theline = getcmdline(':', 0L, indent);
20228 else
20229 theline = eap->getline(':', eap->cookie, indent);
20230 if (KeyTyped)
20231 lines_left = Rows - 1;
20232 if (theline == NULL)
20233 {
20234 EMSG(_("E126: Missing :endfunction"));
20235 goto erret;
20236 }
20237
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020238 /* Detect line continuation: sourcing_lnum increased more than one. */
20239 if (sourcing_lnum > sourcing_lnum_off + 1)
20240 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20241 else
20242 sourcing_lnum_off = 0;
20243
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244 if (skip_until != NULL)
20245 {
20246 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20247 * don't check for ":endfunc". */
20248 if (STRCMP(theline, skip_until) == 0)
20249 {
20250 vim_free(skip_until);
20251 skip_until = NULL;
20252 }
20253 }
20254 else
20255 {
20256 /* skip ':' and blanks*/
20257 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20258 ;
20259
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020260 /* Check for "endfunction". */
20261 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020263 if (line_arg == NULL)
20264 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265 break;
20266 }
20267
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020268 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269 * at "end". */
20270 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20271 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020272 else if (STRNCMP(p, "if", 2) == 0
20273 || STRNCMP(p, "wh", 2) == 0
20274 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 || STRNCMP(p, "try", 3) == 0)
20276 indent += 2;
20277
20278 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020279 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020281 if (*p == '!')
20282 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 p += eval_fname_script(p);
20284 if (ASCII_ISALPHA(*p))
20285 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020286 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287 if (*skipwhite(p) == '(')
20288 {
20289 ++nesting;
20290 indent += 2;
20291 }
20292 }
20293 }
20294
20295 /* Check for ":append" or ":insert". */
20296 p = skip_range(p, NULL);
20297 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20298 || (p[0] == 'i'
20299 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20300 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20301 skip_until = vim_strsave((char_u *)".");
20302
20303 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20304 arg = skipwhite(skiptowhite(p));
20305 if (arg[0] == '<' && arg[1] =='<'
20306 && ((p[0] == 'p' && p[1] == 'y'
20307 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20308 || (p[0] == 'p' && p[1] == 'e'
20309 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20310 || (p[0] == 't' && p[1] == 'c'
20311 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20312 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20313 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020314 || (p[0] == 'm' && p[1] == 'z'
20315 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 ))
20317 {
20318 /* ":python <<" continues until a dot, like ":append" */
20319 p = skipwhite(arg + 2);
20320 if (*p == NUL)
20321 skip_until = vim_strsave((char_u *)".");
20322 else
20323 skip_until = vim_strsave(p);
20324 }
20325 }
20326
20327 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020328 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020329 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020330 if (line_arg == NULL)
20331 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020333 }
20334
20335 /* Copy the line to newly allocated memory. get_one_sourceline()
20336 * allocates 250 bytes per line, this saves 80% on average. The cost
20337 * is an extra alloc/free. */
20338 p = vim_strsave(theline);
20339 if (p != NULL)
20340 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020341 if (line_arg == NULL)
20342 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020343 theline = p;
20344 }
20345
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020346 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20347
20348 /* Add NULL lines for continuation lines, so that the line count is
20349 * equal to the index in the growarray. */
20350 while (sourcing_lnum_off-- > 0)
20351 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020352
20353 /* Check for end of eap->arg. */
20354 if (line_arg != NULL && *line_arg == NUL)
20355 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 }
20357
20358 /* Don't define the function when skipping commands or when an error was
20359 * detected. */
20360 if (eap->skip || did_emsg)
20361 goto erret;
20362
20363 /*
20364 * If there are no errors, add the function
20365 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020366 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020367 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020368 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020369 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020370 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020371 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020372 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020373 goto erret;
20374 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020375
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020376 fp = find_func(name);
20377 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020379 if (!eap->forceit)
20380 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020381 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020382 goto erret;
20383 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020384 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020385 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020386 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020387 name);
20388 goto erret;
20389 }
20390 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020391 ga_clear_strings(&(fp->uf_args));
20392 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020393 vim_free(name);
20394 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 }
20397 else
20398 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020399 char numbuf[20];
20400
20401 fp = NULL;
20402 if (fudi.fd_newkey == NULL && !eap->forceit)
20403 {
20404 EMSG(_(e_funcdict));
20405 goto erret;
20406 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020407 if (fudi.fd_di == NULL)
20408 {
20409 /* Can't add a function to a locked dictionary */
20410 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20411 goto erret;
20412 }
20413 /* Can't change an existing function if it is locked */
20414 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20415 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020416
20417 /* Give the function a sequential number. Can only be used with a
20418 * Funcref! */
20419 vim_free(name);
20420 sprintf(numbuf, "%d", ++func_nr);
20421 name = vim_strsave((char_u *)numbuf);
20422 if (name == NULL)
20423 goto erret;
20424 }
20425
20426 if (fp == NULL)
20427 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020428 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020429 {
20430 int slen, plen;
20431 char_u *scriptname;
20432
20433 /* Check that the autoload name matches the script name. */
20434 j = FAIL;
20435 if (sourcing_name != NULL)
20436 {
20437 scriptname = autoload_name(name);
20438 if (scriptname != NULL)
20439 {
20440 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020441 plen = (int)STRLEN(p);
20442 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020443 if (slen > plen && fnamecmp(p,
20444 sourcing_name + slen - plen) == 0)
20445 j = OK;
20446 vim_free(scriptname);
20447 }
20448 }
20449 if (j == FAIL)
20450 {
20451 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20452 goto erret;
20453 }
20454 }
20455
20456 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 if (fp == NULL)
20458 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020459
20460 if (fudi.fd_dict != NULL)
20461 {
20462 if (fudi.fd_di == NULL)
20463 {
20464 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020465 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020466 if (fudi.fd_di == NULL)
20467 {
20468 vim_free(fp);
20469 goto erret;
20470 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020471 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20472 {
20473 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020474 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020475 goto erret;
20476 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020477 }
20478 else
20479 /* overwrite existing dict entry */
20480 clear_tv(&fudi.fd_di->di_tv);
20481 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020482 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020483 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020484 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020485
20486 /* behave like "dict" was used */
20487 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020488 }
20489
Bram Moolenaar071d4272004-06-13 20:20:40 +000020490 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020491 STRCPY(fp->uf_name, name);
20492 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020494 fp->uf_args = newargs;
20495 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020496#ifdef FEAT_PROFILE
20497 fp->uf_tml_count = NULL;
20498 fp->uf_tml_total = NULL;
20499 fp->uf_tml_self = NULL;
20500 fp->uf_profiling = FALSE;
20501 if (prof_def_func())
20502 func_do_profile(fp);
20503#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020504 fp->uf_varargs = varargs;
20505 fp->uf_flags = flags;
20506 fp->uf_calls = 0;
20507 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020508 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509
20510erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511 ga_clear_strings(&newargs);
20512 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020513ret_free:
20514 vim_free(skip_until);
20515 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020516 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518}
20519
20520/*
20521 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020522 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020523 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020524 * flags:
20525 * TFN_INT: internal function name OK
20526 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527 * Advances "pp" to just after the function name (if no error).
20528 */
20529 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020530trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531 char_u **pp;
20532 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020533 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020534 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020536 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537 char_u *start;
20538 char_u *end;
20539 int lead;
20540 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020542 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020543
20544 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020545 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020547
20548 /* Check for hard coded <SNR>: already translated function ID (from a user
20549 * command). */
20550 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20551 && (*pp)[2] == (int)KE_SNR)
20552 {
20553 *pp += 3;
20554 len = get_id_len(pp) + 3;
20555 return vim_strnsave(start, len);
20556 }
20557
20558 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20559 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020561 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020563
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020564 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20565 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020566 if (end == start)
20567 {
20568 if (!skip)
20569 EMSG(_("E129: Function name required"));
20570 goto theend;
20571 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020572 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020573 {
20574 /*
20575 * Report an invalid expression in braces, unless the expression
20576 * evaluation has been cancelled due to an aborting error, an
20577 * interrupt, or an exception.
20578 */
20579 if (!aborting())
20580 {
20581 if (end != NULL)
20582 EMSG2(_(e_invarg2), start);
20583 }
20584 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020585 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020586 goto theend;
20587 }
20588
20589 if (lv.ll_tv != NULL)
20590 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020591 if (fdp != NULL)
20592 {
20593 fdp->fd_dict = lv.ll_dict;
20594 fdp->fd_newkey = lv.ll_newkey;
20595 lv.ll_newkey = NULL;
20596 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020597 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020598 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20599 {
20600 name = vim_strsave(lv.ll_tv->vval.v_string);
20601 *pp = end;
20602 }
20603 else
20604 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020605 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20606 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020607 EMSG(_(e_funcref));
20608 else
20609 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020610 name = NULL;
20611 }
20612 goto theend;
20613 }
20614
20615 if (lv.ll_name == NULL)
20616 {
20617 /* Error found, but continue after the function name. */
20618 *pp = end;
20619 goto theend;
20620 }
20621
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020622 /* Check if the name is a Funcref. If so, use the value. */
20623 if (lv.ll_exp_name != NULL)
20624 {
20625 len = (int)STRLEN(lv.ll_exp_name);
20626 name = deref_func_name(lv.ll_exp_name, &len);
20627 if (name == lv.ll_exp_name)
20628 name = NULL;
20629 }
20630 else
20631 {
20632 len = (int)(end - *pp);
20633 name = deref_func_name(*pp, &len);
20634 if (name == *pp)
20635 name = NULL;
20636 }
20637 if (name != NULL)
20638 {
20639 name = vim_strsave(name);
20640 *pp = end;
20641 goto theend;
20642 }
20643
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020644 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020645 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020646 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020647 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20648 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20649 {
20650 /* When there was "s:" already or the name expanded to get a
20651 * leading "s:" then remove it. */
20652 lv.ll_name += 2;
20653 len -= 2;
20654 lead = 2;
20655 }
20656 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020657 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020658 {
20659 if (lead == 2) /* skip over "s:" */
20660 lv.ll_name += 2;
20661 len = (int)(end - lv.ll_name);
20662 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020663
20664 /*
20665 * Copy the function name to allocated memory.
20666 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20667 * Accept <SNR>123_name() outside a script.
20668 */
20669 if (skip)
20670 lead = 0; /* do nothing */
20671 else if (lead > 0)
20672 {
20673 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020674 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20675 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020676 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020677 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020678 if (current_SID <= 0)
20679 {
20680 EMSG(_(e_usingsid));
20681 goto theend;
20682 }
20683 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20684 lead += (int)STRLEN(sid_buf);
20685 }
20686 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020687 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020688 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020689 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020690 goto theend;
20691 }
20692 name = alloc((unsigned)(len + lead + 1));
20693 if (name != NULL)
20694 {
20695 if (lead > 0)
20696 {
20697 name[0] = K_SPECIAL;
20698 name[1] = KS_EXTRA;
20699 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020700 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020701 STRCPY(name + 3, sid_buf);
20702 }
20703 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20704 name[len + lead] = NUL;
20705 }
20706 *pp = end;
20707
20708theend:
20709 clear_lval(&lv);
20710 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711}
20712
20713/*
20714 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20715 * Return 2 if "p" starts with "s:".
20716 * Return 0 otherwise.
20717 */
20718 static int
20719eval_fname_script(p)
20720 char_u *p;
20721{
20722 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20723 || STRNICMP(p + 1, "SNR>", 4) == 0))
20724 return 5;
20725 if (p[0] == 's' && p[1] == ':')
20726 return 2;
20727 return 0;
20728}
20729
20730/*
20731 * Return TRUE if "p" starts with "<SID>" or "s:".
20732 * Only works if eval_fname_script() returned non-zero for "p"!
20733 */
20734 static int
20735eval_fname_sid(p)
20736 char_u *p;
20737{
20738 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20739}
20740
20741/*
20742 * List the head of the function: "name(arg1, arg2)".
20743 */
20744 static void
20745list_func_head(fp, indent)
20746 ufunc_T *fp;
20747 int indent;
20748{
20749 int j;
20750
20751 msg_start();
20752 if (indent)
20753 MSG_PUTS(" ");
20754 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020755 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756 {
20757 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020758 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020759 }
20760 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020761 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020763 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020764 {
20765 if (j)
20766 MSG_PUTS(", ");
20767 msg_puts(FUNCARG(fp, j));
20768 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020769 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770 {
20771 if (j)
20772 MSG_PUTS(", ");
20773 MSG_PUTS("...");
20774 }
20775 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020776 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020777 if (p_verbose > 0)
20778 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779}
20780
20781/*
20782 * Find a function by name, return pointer to it in ufuncs.
20783 * Return NULL for unknown function.
20784 */
20785 static ufunc_T *
20786find_func(name)
20787 char_u *name;
20788{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020789 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020791 hi = hash_find(&func_hashtab, name);
20792 if (!HASHITEM_EMPTY(hi))
20793 return HI2UF(hi);
20794 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795}
20796
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020797#if defined(EXITFREE) || defined(PROTO)
20798 void
20799free_all_functions()
20800{
20801 hashitem_T *hi;
20802
20803 /* Need to start all over every time, because func_free() may change the
20804 * hash table. */
20805 while (func_hashtab.ht_used > 0)
20806 for (hi = func_hashtab.ht_array; ; ++hi)
20807 if (!HASHITEM_EMPTY(hi))
20808 {
20809 func_free(HI2UF(hi));
20810 break;
20811 }
20812}
20813#endif
20814
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020815/*
20816 * Return TRUE if a function "name" exists.
20817 */
20818 static int
20819function_exists(name)
20820 char_u *name;
20821{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020822 char_u *nm = name;
20823 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020824 int n = FALSE;
20825
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020826 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020827 nm = skipwhite(nm);
20828
20829 /* Only accept "funcname", "funcname ", "funcname (..." and
20830 * "funcname(...", not "funcname!...". */
20831 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020832 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020833 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020834 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020835 else
20836 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020837 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020838 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020839 return n;
20840}
20841
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020842/*
20843 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020844 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020845 */
20846 static int
20847builtin_function(name)
20848 char_u *name;
20849{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020850 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20851 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020852}
20853
Bram Moolenaar05159a02005-02-26 23:04:13 +000020854#if defined(FEAT_PROFILE) || defined(PROTO)
20855/*
20856 * Start profiling function "fp".
20857 */
20858 static void
20859func_do_profile(fp)
20860 ufunc_T *fp;
20861{
20862 fp->uf_tm_count = 0;
20863 profile_zero(&fp->uf_tm_self);
20864 profile_zero(&fp->uf_tm_total);
20865 if (fp->uf_tml_count == NULL)
20866 fp->uf_tml_count = (int *)alloc_clear((unsigned)
20867 (sizeof(int) * fp->uf_lines.ga_len));
20868 if (fp->uf_tml_total == NULL)
20869 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
20870 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20871 if (fp->uf_tml_self == NULL)
20872 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
20873 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20874 fp->uf_tml_idx = -1;
20875 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
20876 || fp->uf_tml_self == NULL)
20877 return; /* out of memory */
20878
20879 fp->uf_profiling = TRUE;
20880}
20881
20882/*
20883 * Dump the profiling results for all functions in file "fd".
20884 */
20885 void
20886func_dump_profile(fd)
20887 FILE *fd;
20888{
20889 hashitem_T *hi;
20890 int todo;
20891 ufunc_T *fp;
20892 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000020893 ufunc_T **sorttab;
20894 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020895
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020896 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020897 if (todo == 0)
20898 return; /* nothing to dump */
20899
Bram Moolenaar73830342005-02-28 22:48:19 +000020900 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
20901
Bram Moolenaar05159a02005-02-26 23:04:13 +000020902 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
20903 {
20904 if (!HASHITEM_EMPTY(hi))
20905 {
20906 --todo;
20907 fp = HI2UF(hi);
20908 if (fp->uf_profiling)
20909 {
Bram Moolenaar73830342005-02-28 22:48:19 +000020910 if (sorttab != NULL)
20911 sorttab[st_len++] = fp;
20912
Bram Moolenaar05159a02005-02-26 23:04:13 +000020913 if (fp->uf_name[0] == K_SPECIAL)
20914 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
20915 else
20916 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
20917 if (fp->uf_tm_count == 1)
20918 fprintf(fd, "Called 1 time\n");
20919 else
20920 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
20921 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
20922 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
20923 fprintf(fd, "\n");
20924 fprintf(fd, "count total (s) self (s)\n");
20925
20926 for (i = 0; i < fp->uf_lines.ga_len; ++i)
20927 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020928 if (FUNCLINE(fp, i) == NULL)
20929 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000020930 prof_func_line(fd, fp->uf_tml_count[i],
20931 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020932 fprintf(fd, "%s\n", FUNCLINE(fp, i));
20933 }
20934 fprintf(fd, "\n");
20935 }
20936 }
20937 }
Bram Moolenaar73830342005-02-28 22:48:19 +000020938
20939 if (sorttab != NULL && st_len > 0)
20940 {
20941 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20942 prof_total_cmp);
20943 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
20944 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20945 prof_self_cmp);
20946 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
20947 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020948
20949 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020950}
Bram Moolenaar73830342005-02-28 22:48:19 +000020951
20952 static void
20953prof_sort_list(fd, sorttab, st_len, title, prefer_self)
20954 FILE *fd;
20955 ufunc_T **sorttab;
20956 int st_len;
20957 char *title;
20958 int prefer_self; /* when equal print only self time */
20959{
20960 int i;
20961 ufunc_T *fp;
20962
20963 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
20964 fprintf(fd, "count total (s) self (s) function\n");
20965 for (i = 0; i < 20 && i < st_len; ++i)
20966 {
20967 fp = sorttab[i];
20968 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
20969 prefer_self);
20970 if (fp->uf_name[0] == K_SPECIAL)
20971 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
20972 else
20973 fprintf(fd, " %s()\n", fp->uf_name);
20974 }
20975 fprintf(fd, "\n");
20976}
20977
20978/*
20979 * Print the count and times for one function or function line.
20980 */
20981 static void
20982prof_func_line(fd, count, total, self, prefer_self)
20983 FILE *fd;
20984 int count;
20985 proftime_T *total;
20986 proftime_T *self;
20987 int prefer_self; /* when equal print only self time */
20988{
20989 if (count > 0)
20990 {
20991 fprintf(fd, "%5d ", count);
20992 if (prefer_self && profile_equal(total, self))
20993 fprintf(fd, " ");
20994 else
20995 fprintf(fd, "%s ", profile_msg(total));
20996 if (!prefer_self && profile_equal(total, self))
20997 fprintf(fd, " ");
20998 else
20999 fprintf(fd, "%s ", profile_msg(self));
21000 }
21001 else
21002 fprintf(fd, " ");
21003}
21004
21005/*
21006 * Compare function for total time sorting.
21007 */
21008 static int
21009#ifdef __BORLANDC__
21010_RTLENTRYF
21011#endif
21012prof_total_cmp(s1, s2)
21013 const void *s1;
21014 const void *s2;
21015{
21016 ufunc_T *p1, *p2;
21017
21018 p1 = *(ufunc_T **)s1;
21019 p2 = *(ufunc_T **)s2;
21020 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21021}
21022
21023/*
21024 * Compare function for self time sorting.
21025 */
21026 static int
21027#ifdef __BORLANDC__
21028_RTLENTRYF
21029#endif
21030prof_self_cmp(s1, s2)
21031 const void *s1;
21032 const void *s2;
21033{
21034 ufunc_T *p1, *p2;
21035
21036 p1 = *(ufunc_T **)s1;
21037 p2 = *(ufunc_T **)s2;
21038 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21039}
21040
Bram Moolenaar05159a02005-02-26 23:04:13 +000021041#endif
21042
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021043/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021044 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021045 * Return TRUE if a package was loaded.
21046 */
21047 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021048script_autoload(name, reload)
21049 char_u *name;
21050 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021051{
21052 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021053 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021054 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021055 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021056
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021057 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021058 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021059 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021060 return FALSE;
21061
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021062 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021063
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021064 /* Find the name in the list of previously loaded package names. Skip
21065 * "autoload/", it's always the same. */
21066 for (i = 0; i < ga_loaded.ga_len; ++i)
21067 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21068 break;
21069 if (!reload && i < ga_loaded.ga_len)
21070 ret = FALSE; /* was loaded already */
21071 else
21072 {
21073 /* Remember the name if it wasn't loaded already. */
21074 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21075 {
21076 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21077 tofree = NULL;
21078 }
21079
21080 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021081 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021082 ret = TRUE;
21083 }
21084
21085 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021086 return ret;
21087}
21088
21089/*
21090 * Return the autoload script name for a function or variable name.
21091 * Returns NULL when out of memory.
21092 */
21093 static char_u *
21094autoload_name(name)
21095 char_u *name;
21096{
21097 char_u *p;
21098 char_u *scriptname;
21099
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021100 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021101 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21102 if (scriptname == NULL)
21103 return FALSE;
21104 STRCPY(scriptname, "autoload/");
21105 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021106 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021107 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021108 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021109 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021110 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021111}
21112
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21114
21115/*
21116 * Function given to ExpandGeneric() to obtain the list of user defined
21117 * function names.
21118 */
21119 char_u *
21120get_user_func_name(xp, idx)
21121 expand_T *xp;
21122 int idx;
21123{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021124 static long_u done;
21125 static hashitem_T *hi;
21126 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127
21128 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021130 done = 0;
21131 hi = func_hashtab.ht_array;
21132 }
21133 if (done < func_hashtab.ht_used)
21134 {
21135 if (done++ > 0)
21136 ++hi;
21137 while (HASHITEM_EMPTY(hi))
21138 ++hi;
21139 fp = HI2UF(hi);
21140
21141 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21142 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143
21144 cat_func_name(IObuff, fp);
21145 if (xp->xp_context != EXPAND_USER_FUNC)
21146 {
21147 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021148 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 STRCAT(IObuff, ")");
21150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151 return IObuff;
21152 }
21153 return NULL;
21154}
21155
21156#endif /* FEAT_CMDL_COMPL */
21157
21158/*
21159 * Copy the function name of "fp" to buffer "buf".
21160 * "buf" must be able to hold the function name plus three bytes.
21161 * Takes care of script-local function names.
21162 */
21163 static void
21164cat_func_name(buf, fp)
21165 char_u *buf;
21166 ufunc_T *fp;
21167{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021168 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169 {
21170 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021171 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172 }
21173 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021174 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175}
21176
21177/*
21178 * ":delfunction {name}"
21179 */
21180 void
21181ex_delfunction(eap)
21182 exarg_T *eap;
21183{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021184 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 char_u *p;
21186 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021187 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188
21189 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021190 name = trans_function_name(&p, eap->skip, 0, &fudi);
21191 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021193 {
21194 if (fudi.fd_dict != NULL && !eap->skip)
21195 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198 if (!ends_excmd(*skipwhite(p)))
21199 {
21200 vim_free(name);
21201 EMSG(_(e_trailing));
21202 return;
21203 }
21204 eap->nextcmd = check_nextcmd(p);
21205 if (eap->nextcmd != NULL)
21206 *p = NUL;
21207
21208 if (!eap->skip)
21209 fp = find_func(name);
21210 vim_free(name);
21211
21212 if (!eap->skip)
21213 {
21214 if (fp == NULL)
21215 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021216 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021217 return;
21218 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021219 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021220 {
21221 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21222 return;
21223 }
21224
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021225 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021227 /* Delete the dict item that refers to the function, it will
21228 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021229 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021231 else
21232 func_free(fp);
21233 }
21234}
21235
21236/*
21237 * Free a function and remove it from the list of functions.
21238 */
21239 static void
21240func_free(fp)
21241 ufunc_T *fp;
21242{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021243 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021244
21245 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021246 ga_clear_strings(&(fp->uf_args));
21247 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021248#ifdef FEAT_PROFILE
21249 vim_free(fp->uf_tml_count);
21250 vim_free(fp->uf_tml_total);
21251 vim_free(fp->uf_tml_self);
21252#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021253
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021254 /* remove the function from the function hashtable */
21255 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21256 if (HASHITEM_EMPTY(hi))
21257 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021258 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021259 hash_remove(&func_hashtab, hi);
21260
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021261 vim_free(fp);
21262}
21263
21264/*
21265 * Unreference a Function: decrement the reference count and free it when it
21266 * becomes zero. Only for numbered functions.
21267 */
21268 static void
21269func_unref(name)
21270 char_u *name;
21271{
21272 ufunc_T *fp;
21273
21274 if (name != NULL && isdigit(*name))
21275 {
21276 fp = find_func(name);
21277 if (fp == NULL)
21278 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021279 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021280 {
21281 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021282 * when "uf_calls" becomes zero. */
21283 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021284 func_free(fp);
21285 }
21286 }
21287}
21288
21289/*
21290 * Count a reference to a Function.
21291 */
21292 static void
21293func_ref(name)
21294 char_u *name;
21295{
21296 ufunc_T *fp;
21297
21298 if (name != NULL && isdigit(*name))
21299 {
21300 fp = find_func(name);
21301 if (fp == NULL)
21302 EMSG2(_(e_intern2), "func_ref()");
21303 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021304 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305 }
21306}
21307
21308/*
21309 * Call a user function.
21310 */
21311 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021312call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313 ufunc_T *fp; /* pointer to function */
21314 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021315 typval_T *argvars; /* arguments */
21316 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317 linenr_T firstline; /* first line of range */
21318 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021319 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021320{
Bram Moolenaar33570922005-01-25 22:26:29 +000021321 char_u *save_sourcing_name;
21322 linenr_T save_sourcing_lnum;
21323 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021324 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021325 int save_did_emsg;
21326 static int depth = 0;
21327 dictitem_T *v;
21328 int fixvar_idx = 0; /* index in fixvar[] */
21329 int i;
21330 int ai;
21331 char_u numbuf[NUMBUFLEN];
21332 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021333#ifdef FEAT_PROFILE
21334 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021335 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337
21338 /* If depth of calling is getting too high, don't execute the function */
21339 if (depth >= p_mfd)
21340 {
21341 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021342 rettv->v_type = VAR_NUMBER;
21343 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021344 return;
21345 }
21346 ++depth;
21347
21348 line_breakcheck(); /* check for CTRL-C hit */
21349
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021350 fc = (funccall_T *)alloc(sizeof(funccall_T));
21351 fc->caller = current_funccal;
21352 current_funccal = fc;
21353 fc->func = fp;
21354 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021355 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021356 fc->linenr = 0;
21357 fc->returned = FALSE;
21358 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021360 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21361 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362
Bram Moolenaar33570922005-01-25 22:26:29 +000021363 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021364 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021365 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21366 * each argument variable and saves a lot of time.
21367 */
21368 /*
21369 * Init l: variables.
21370 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021371 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021372 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021373 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021374 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21375 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021376 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021377 name = v->di_key;
21378 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021379 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021380 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021381 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021382 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021383 v->di_tv.vval.v_dict = selfdict;
21384 ++selfdict->dv_refcount;
21385 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021386
Bram Moolenaar33570922005-01-25 22:26:29 +000021387 /*
21388 * Init a: variables.
21389 * Set a:0 to "argcount".
21390 * Set a:000 to a list with room for the "..." arguments.
21391 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021392 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21393 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021394 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021395 /* Use "name" to avoid a warning from some compiler that checks the
21396 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021397 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021398 name = v->di_key;
21399 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021400 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021401 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021402 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021403 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021404 v->di_tv.vval.v_list = &fc->l_varlist;
21405 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21406 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21407 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021408
21409 /*
21410 * Set a:firstline to "firstline" and a:lastline to "lastline".
21411 * Set a:name to named arguments.
21412 * Set a:N to the "..." arguments.
21413 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021414 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021415 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021416 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021417 (varnumber_T)lastline);
21418 for (i = 0; i < argcount; ++i)
21419 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021420 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021421 if (ai < 0)
21422 /* named argument a:name */
21423 name = FUNCARG(fp, i);
21424 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021425 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021426 /* "..." argument a:1, a:2, etc. */
21427 sprintf((char *)numbuf, "%d", ai + 1);
21428 name = numbuf;
21429 }
21430 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21431 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021432 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021433 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21434 }
21435 else
21436 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021437 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21438 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021439 if (v == NULL)
21440 break;
21441 v->di_flags = DI_FLAGS_RO;
21442 }
21443 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021444 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021445
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021446 /* Note: the values are copied directly to avoid alloc/free.
21447 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021448 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021449 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021450
21451 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21452 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021453 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21454 fc->l_listitems[ai].li_tv = argvars[i];
21455 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021456 }
21457 }
21458
Bram Moolenaar071d4272004-06-13 20:20:40 +000021459 /* Don't redraw while executing the function. */
21460 ++RedrawingDisabled;
21461 save_sourcing_name = sourcing_name;
21462 save_sourcing_lnum = sourcing_lnum;
21463 sourcing_lnum = 1;
21464 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021465 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021466 if (sourcing_name != NULL)
21467 {
21468 if (save_sourcing_name != NULL
21469 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21470 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21471 else
21472 STRCPY(sourcing_name, "function ");
21473 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21474
21475 if (p_verbose >= 12)
21476 {
21477 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021478 verbose_enter_scroll();
21479
Bram Moolenaar555b2802005-05-19 21:08:39 +000021480 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 if (p_verbose >= 14)
21482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021484 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021485 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021486 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021487
21488 msg_puts((char_u *)"(");
21489 for (i = 0; i < argcount; ++i)
21490 {
21491 if (i > 0)
21492 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021493 if (argvars[i].v_type == VAR_NUMBER)
21494 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495 else
21496 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021497 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21498 if (s != NULL)
21499 {
21500 trunc_string(s, buf, MSG_BUF_CLEN);
21501 msg_puts(buf);
21502 vim_free(tofree);
21503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504 }
21505 }
21506 msg_puts((char_u *)")");
21507 }
21508 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021509
21510 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021511 --no_wait_return;
21512 }
21513 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021514#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021515 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021516 {
21517 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21518 func_do_profile(fp);
21519 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021520 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021521 {
21522 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021523 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021524 profile_zero(&fp->uf_tm_children);
21525 }
21526 script_prof_save(&wait_start);
21527 }
21528#endif
21529
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021531 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 save_did_emsg = did_emsg;
21533 did_emsg = FALSE;
21534
21535 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021536 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21538
21539 --RedrawingDisabled;
21540
21541 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021542 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021544 clear_tv(rettv);
21545 rettv->v_type = VAR_NUMBER;
21546 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547 }
21548
Bram Moolenaar05159a02005-02-26 23:04:13 +000021549#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021550 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021551 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021552 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021553 profile_end(&call_start);
21554 profile_sub_wait(&wait_start, &call_start);
21555 profile_add(&fp->uf_tm_total, &call_start);
21556 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021557 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021558 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021559 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21560 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021561 }
21562 }
21563#endif
21564
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565 /* when being verbose, mention the return value */
21566 if (p_verbose >= 12)
21567 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021569 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570
Bram Moolenaar071d4272004-06-13 20:20:40 +000021571 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021572 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021573 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021574 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021575 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021576 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021578 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021579 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021580 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021581 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021582
Bram Moolenaar555b2802005-05-19 21:08:39 +000021583 /* The value may be very long. Skip the middle part, so that we
21584 * have some idea how it starts and ends. smsg() would always
21585 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021586 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021587 if (s != NULL)
21588 {
21589 trunc_string(s, buf, MSG_BUF_CLEN);
21590 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21591 vim_free(tofree);
21592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 }
21594 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021595
21596 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597 --no_wait_return;
21598 }
21599
21600 vim_free(sourcing_name);
21601 sourcing_name = save_sourcing_name;
21602 sourcing_lnum = save_sourcing_lnum;
21603 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021604#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021605 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021606 script_prof_restore(&wait_start);
21607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608
21609 if (p_verbose >= 12 && sourcing_name != NULL)
21610 {
21611 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021612 verbose_enter_scroll();
21613
Bram Moolenaar555b2802005-05-19 21:08:39 +000021614 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021616
21617 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 --no_wait_return;
21619 }
21620
21621 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021622 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021624
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021625 /* If the a:000 list and the l: and a: dicts are not referenced we can
21626 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021627 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21628 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21629 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21630 {
21631 free_funccal(fc, FALSE);
21632 }
21633 else
21634 {
21635 hashitem_T *hi;
21636 listitem_T *li;
21637 int todo;
21638
21639 /* "fc" is still in use. This can happen when returning "a:000" or
21640 * assigning "l:" to a global variable.
21641 * Link "fc" in the list for garbage collection later. */
21642 fc->caller = previous_funccal;
21643 previous_funccal = fc;
21644
21645 /* Make a copy of the a: variables, since we didn't do that above. */
21646 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21647 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21648 {
21649 if (!HASHITEM_EMPTY(hi))
21650 {
21651 --todo;
21652 v = HI2DI(hi);
21653 copy_tv(&v->di_tv, &v->di_tv);
21654 }
21655 }
21656
21657 /* Make a copy of the a:000 items, since we didn't do that above. */
21658 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21659 copy_tv(&li->li_tv, &li->li_tv);
21660 }
21661}
21662
21663/*
21664 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021665 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021666 */
21667 static int
21668can_free_funccal(fc, copyID)
21669 funccall_T *fc;
21670 int copyID;
21671{
21672 return (fc->l_varlist.lv_copyID != copyID
21673 && fc->l_vars.dv_copyID != copyID
21674 && fc->l_avars.dv_copyID != copyID);
21675}
21676
21677/*
21678 * Free "fc" and what it contains.
21679 */
21680 static void
21681free_funccal(fc, free_val)
21682 funccall_T *fc;
21683 int free_val; /* a: vars were allocated */
21684{
21685 listitem_T *li;
21686
21687 /* The a: variables typevals may not have been allocated, only free the
21688 * allocated variables. */
21689 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21690
21691 /* free all l: variables */
21692 vars_clear(&fc->l_vars.dv_hashtab);
21693
21694 /* Free the a:000 variables if they were allocated. */
21695 if (free_val)
21696 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21697 clear_tv(&li->li_tv);
21698
21699 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700}
21701
21702/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021703 * Add a number variable "name" to dict "dp" with value "nr".
21704 */
21705 static void
21706add_nr_var(dp, v, name, nr)
21707 dict_T *dp;
21708 dictitem_T *v;
21709 char *name;
21710 varnumber_T nr;
21711{
21712 STRCPY(v->di_key, name);
21713 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21714 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21715 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021716 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021717 v->di_tv.vval.v_number = nr;
21718}
21719
21720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021721 * ":return [expr]"
21722 */
21723 void
21724ex_return(eap)
21725 exarg_T *eap;
21726{
21727 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021728 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 int returning = FALSE;
21730
21731 if (current_funccal == NULL)
21732 {
21733 EMSG(_("E133: :return not inside a function"));
21734 return;
21735 }
21736
21737 if (eap->skip)
21738 ++emsg_skip;
21739
21740 eap->nextcmd = NULL;
21741 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021742 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743 {
21744 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021745 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021747 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748 }
21749 /* It's safer to return also on error. */
21750 else if (!eap->skip)
21751 {
21752 /*
21753 * Return unless the expression evaluation has been cancelled due to an
21754 * aborting error, an interrupt, or an exception.
21755 */
21756 if (!aborting())
21757 returning = do_return(eap, FALSE, TRUE, NULL);
21758 }
21759
21760 /* When skipping or the return gets pending, advance to the next command
21761 * in this line (!returning). Otherwise, ignore the rest of the line.
21762 * Following lines will be ignored by get_func_line(). */
21763 if (returning)
21764 eap->nextcmd = NULL;
21765 else if (eap->nextcmd == NULL) /* no argument */
21766 eap->nextcmd = check_nextcmd(arg);
21767
21768 if (eap->skip)
21769 --emsg_skip;
21770}
21771
21772/*
21773 * Return from a function. Possibly makes the return pending. Also called
21774 * for a pending return at the ":endtry" or after returning from an extra
21775 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021776 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021777 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021778 * FALSE when the return gets pending.
21779 */
21780 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021781do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782 exarg_T *eap;
21783 int reanimate;
21784 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021785 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786{
21787 int idx;
21788 struct condstack *cstack = eap->cstack;
21789
21790 if (reanimate)
21791 /* Undo the return. */
21792 current_funccal->returned = FALSE;
21793
21794 /*
21795 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21796 * not in its finally clause (which then is to be executed next) is found.
21797 * In this case, make the ":return" pending for execution at the ":endtry".
21798 * Otherwise, return normally.
21799 */
21800 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21801 if (idx >= 0)
21802 {
21803 cstack->cs_pending[idx] = CSTP_RETURN;
21804
21805 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021806 /* A pending return again gets pending. "rettv" points to an
21807 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021809 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021810 else
21811 {
21812 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021813 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021815 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021817 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818 {
21819 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021820 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021821 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021822 else
21823 EMSG(_(e_outofmem));
21824 }
21825 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021826 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827
21828 if (reanimate)
21829 {
21830 /* The pending return value could be overwritten by a ":return"
21831 * without argument in a finally clause; reset the default
21832 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021833 current_funccal->rettv->v_type = VAR_NUMBER;
21834 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835 }
21836 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021837 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838 }
21839 else
21840 {
21841 current_funccal->returned = TRUE;
21842
21843 /* If the return is carried out now, store the return value. For
21844 * a return immediately after reanimation, the value is already
21845 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021846 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021848 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021849 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021851 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852 }
21853 }
21854
21855 return idx < 0;
21856}
21857
21858/*
21859 * Free the variable with a pending return value.
21860 */
21861 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021862discard_pending_return(rettv)
21863 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864{
Bram Moolenaar33570922005-01-25 22:26:29 +000021865 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866}
21867
21868/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021869 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870 * is an allocated string. Used by report_pending() for verbose messages.
21871 */
21872 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021873get_return_cmd(rettv)
21874 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021875{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021876 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021877 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021878 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021879
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021880 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021881 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021882 if (s == NULL)
21883 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021884
21885 STRCPY(IObuff, ":return ");
21886 STRNCPY(IObuff + 8, s, IOSIZE - 8);
21887 if (STRLEN(s) + 8 >= IOSIZE)
21888 STRCPY(IObuff + IOSIZE - 4, "...");
21889 vim_free(tofree);
21890 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891}
21892
21893/*
21894 * Get next function line.
21895 * Called by do_cmdline() to get the next line.
21896 * Returns allocated string, or NULL for end of function.
21897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898 char_u *
21899get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000021900 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000021902 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021903{
Bram Moolenaar33570922005-01-25 22:26:29 +000021904 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021905 ufunc_T *fp = fcp->func;
21906 char_u *retval;
21907 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908
21909 /* If breakpoints have been added/deleted need to check for it. */
21910 if (fcp->dbg_tick != debug_tick)
21911 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021912 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913 sourcing_lnum);
21914 fcp->dbg_tick = debug_tick;
21915 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021916#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021917 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021918 func_line_end(cookie);
21919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920
Bram Moolenaar05159a02005-02-26 23:04:13 +000021921 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021922 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
21923 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924 retval = NULL;
21925 else
21926 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021927 /* Skip NULL lines (continuation lines). */
21928 while (fcp->linenr < gap->ga_len
21929 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
21930 ++fcp->linenr;
21931 if (fcp->linenr >= gap->ga_len)
21932 retval = NULL;
21933 else
21934 {
21935 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
21936 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021937#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021938 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021939 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021940#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021942 }
21943
21944 /* Did we encounter a breakpoint? */
21945 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
21946 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021947 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021949 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950 sourcing_lnum);
21951 fcp->dbg_tick = debug_tick;
21952 }
21953
21954 return retval;
21955}
21956
Bram Moolenaar05159a02005-02-26 23:04:13 +000021957#if defined(FEAT_PROFILE) || defined(PROTO)
21958/*
21959 * Called when starting to read a function line.
21960 * "sourcing_lnum" must be correct!
21961 * When skipping lines it may not actually be executed, but we won't find out
21962 * until later and we need to store the time now.
21963 */
21964 void
21965func_line_start(cookie)
21966 void *cookie;
21967{
21968 funccall_T *fcp = (funccall_T *)cookie;
21969 ufunc_T *fp = fcp->func;
21970
21971 if (fp->uf_profiling && sourcing_lnum >= 1
21972 && sourcing_lnum <= fp->uf_lines.ga_len)
21973 {
21974 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021975 /* Skip continuation lines. */
21976 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
21977 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021978 fp->uf_tml_execed = FALSE;
21979 profile_start(&fp->uf_tml_start);
21980 profile_zero(&fp->uf_tml_children);
21981 profile_get_wait(&fp->uf_tml_wait);
21982 }
21983}
21984
21985/*
21986 * Called when actually executing a function line.
21987 */
21988 void
21989func_line_exec(cookie)
21990 void *cookie;
21991{
21992 funccall_T *fcp = (funccall_T *)cookie;
21993 ufunc_T *fp = fcp->func;
21994
21995 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21996 fp->uf_tml_execed = TRUE;
21997}
21998
21999/*
22000 * Called when done with a function line.
22001 */
22002 void
22003func_line_end(cookie)
22004 void *cookie;
22005{
22006 funccall_T *fcp = (funccall_T *)cookie;
22007 ufunc_T *fp = fcp->func;
22008
22009 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22010 {
22011 if (fp->uf_tml_execed)
22012 {
22013 ++fp->uf_tml_count[fp->uf_tml_idx];
22014 profile_end(&fp->uf_tml_start);
22015 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022016 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022017 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22018 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022019 }
22020 fp->uf_tml_idx = -1;
22021 }
22022}
22023#endif
22024
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025/*
22026 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022027 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028 */
22029 int
22030func_has_ended(cookie)
22031 void *cookie;
22032{
Bram Moolenaar33570922005-01-25 22:26:29 +000022033 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022034
22035 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22036 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022037 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038 || fcp->returned);
22039}
22040
22041/*
22042 * return TRUE if cookie indicates a function which "abort"s on errors.
22043 */
22044 int
22045func_has_abort(cookie)
22046 void *cookie;
22047{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022048 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049}
22050
22051#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22052typedef enum
22053{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022054 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22055 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22056 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057} var_flavour_T;
22058
22059static var_flavour_T var_flavour __ARGS((char_u *varname));
22060
22061 static var_flavour_T
22062var_flavour(varname)
22063 char_u *varname;
22064{
22065 char_u *p = varname;
22066
22067 if (ASCII_ISUPPER(*p))
22068 {
22069 while (*(++p))
22070 if (ASCII_ISLOWER(*p))
22071 return VAR_FLAVOUR_SESSION;
22072 return VAR_FLAVOUR_VIMINFO;
22073 }
22074 else
22075 return VAR_FLAVOUR_DEFAULT;
22076}
22077#endif
22078
22079#if defined(FEAT_VIMINFO) || defined(PROTO)
22080/*
22081 * Restore global vars that start with a capital from the viminfo file
22082 */
22083 int
22084read_viminfo_varlist(virp, writing)
22085 vir_T *virp;
22086 int writing;
22087{
22088 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022089 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022090 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091
22092 if (!writing && (find_viminfo_parameter('!') != NULL))
22093 {
22094 tab = vim_strchr(virp->vir_line + 1, '\t');
22095 if (tab != NULL)
22096 {
22097 *tab++ = '\0'; /* isolate the variable name */
22098 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022099 type = VAR_STRING;
22100#ifdef FEAT_FLOAT
22101 else if (*tab == 'F')
22102 type = VAR_FLOAT;
22103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022104
22105 tab = vim_strchr(tab, '\t');
22106 if (tab != NULL)
22107 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022108 tv.v_type = type;
22109 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022110 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022112#ifdef FEAT_FLOAT
22113 else if (type == VAR_FLOAT)
22114 (void)string2float(tab + 1, &tv.vval.v_float);
22115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022117 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022118 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022119 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022120 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022121 }
22122 }
22123 }
22124
22125 return viminfo_readline(virp);
22126}
22127
22128/*
22129 * Write global vars that start with a capital to the viminfo file
22130 */
22131 void
22132write_viminfo_varlist(fp)
22133 FILE *fp;
22134{
Bram Moolenaar33570922005-01-25 22:26:29 +000022135 hashitem_T *hi;
22136 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022137 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022138 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022139 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022140 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022141 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022142
22143 if (find_viminfo_parameter('!') == NULL)
22144 return;
22145
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022146 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022147
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022148 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022149 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022151 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022153 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022154 this_var = HI2DI(hi);
22155 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022156 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022157 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022158 {
22159 case VAR_STRING: s = "STR"; break;
22160 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022161#ifdef FEAT_FLOAT
22162 case VAR_FLOAT: s = "FLO"; break;
22163#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022164 default: continue;
22165 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022166 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022167 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022168 if (p != NULL)
22169 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022170 vim_free(tofree);
22171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172 }
22173 }
22174}
22175#endif
22176
22177#if defined(FEAT_SESSION) || defined(PROTO)
22178 int
22179store_session_globals(fd)
22180 FILE *fd;
22181{
Bram Moolenaar33570922005-01-25 22:26:29 +000022182 hashitem_T *hi;
22183 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022184 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185 char_u *p, *t;
22186
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022187 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022188 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022190 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022192 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022193 this_var = HI2DI(hi);
22194 if ((this_var->di_tv.v_type == VAR_NUMBER
22195 || this_var->di_tv.v_type == VAR_STRING)
22196 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022197 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022198 /* Escape special characters with a backslash. Turn a LF and
22199 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022200 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022201 (char_u *)"\\\"\n\r");
22202 if (p == NULL) /* out of memory */
22203 break;
22204 for (t = p; *t != NUL; ++t)
22205 if (*t == '\n')
22206 *t = 'n';
22207 else if (*t == '\r')
22208 *t = 'r';
22209 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022210 this_var->di_key,
22211 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22212 : ' ',
22213 p,
22214 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22215 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022216 || put_eol(fd) == FAIL)
22217 {
22218 vim_free(p);
22219 return FAIL;
22220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221 vim_free(p);
22222 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022223#ifdef FEAT_FLOAT
22224 else if (this_var->di_tv.v_type == VAR_FLOAT
22225 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22226 {
22227 float_T f = this_var->di_tv.vval.v_float;
22228 int sign = ' ';
22229
22230 if (f < 0)
22231 {
22232 f = -f;
22233 sign = '-';
22234 }
22235 if ((fprintf(fd, "let %s = %c&%f",
22236 this_var->di_key, sign, f) < 0)
22237 || put_eol(fd) == FAIL)
22238 return FAIL;
22239 }
22240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241 }
22242 }
22243 return OK;
22244}
22245#endif
22246
Bram Moolenaar661b1822005-07-28 22:36:45 +000022247/*
22248 * Display script name where an item was last set.
22249 * Should only be invoked when 'verbose' is non-zero.
22250 */
22251 void
22252last_set_msg(scriptID)
22253 scid_T scriptID;
22254{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022255 char_u *p;
22256
Bram Moolenaar661b1822005-07-28 22:36:45 +000022257 if (scriptID != 0)
22258 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022259 p = home_replace_save(NULL, get_scriptname(scriptID));
22260 if (p != NULL)
22261 {
22262 verbose_enter();
22263 MSG_PUTS(_("\n\tLast set from "));
22264 MSG_PUTS(p);
22265 vim_free(p);
22266 verbose_leave();
22267 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022268 }
22269}
22270
Bram Moolenaard812df62008-11-09 12:46:09 +000022271/*
22272 * List v:oldfiles in a nice way.
22273 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022274 void
22275ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022276 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022277{
22278 list_T *l = vimvars[VV_OLDFILES].vv_list;
22279 listitem_T *li;
22280 int nr = 0;
22281
22282 if (l == NULL)
22283 msg((char_u *)_("No old files"));
22284 else
22285 {
22286 msg_start();
22287 msg_scroll = TRUE;
22288 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22289 {
22290 msg_outnum((long)++nr);
22291 MSG_PUTS(": ");
22292 msg_outtrans(get_tv_string(&li->li_tv));
22293 msg_putchar('\n');
22294 out_flush(); /* output one line at a time */
22295 ui_breakcheck();
22296 }
22297 /* Assume "got_int" was set to truncate the listing. */
22298 got_int = FALSE;
22299
22300#ifdef FEAT_BROWSE_CMD
22301 if (cmdmod.browse)
22302 {
22303 quit_more = FALSE;
22304 nr = prompt_for_number(FALSE);
22305 msg_starthere();
22306 if (nr > 0)
22307 {
22308 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22309 (long)nr);
22310
22311 if (p != NULL)
22312 {
22313 p = expand_env_save(p);
22314 eap->arg = p;
22315 eap->cmdidx = CMD_edit;
22316 cmdmod.browse = FALSE;
22317 do_exedit(eap, NULL);
22318 vim_free(p);
22319 }
22320 }
22321 }
22322#endif
22323 }
22324}
22325
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326#endif /* FEAT_EVAL */
22327
Bram Moolenaar071d4272004-06-13 20:20:40 +000022328
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022329#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330
22331#ifdef WIN3264
22332/*
22333 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22334 */
22335static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22336static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22337static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22338
22339/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022340 * Get the short path (8.3) for the filename in "fnamep".
22341 * Only works for a valid file name.
22342 * When the path gets longer "fnamep" is changed and the allocated buffer
22343 * is put in "bufp".
22344 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22345 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346 */
22347 static int
22348get_short_pathname(fnamep, bufp, fnamelen)
22349 char_u **fnamep;
22350 char_u **bufp;
22351 int *fnamelen;
22352{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022353 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354 char_u *newbuf;
22355
22356 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022357 l = GetShortPathName(*fnamep, *fnamep, len);
22358 if (l > len - 1)
22359 {
22360 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022361 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362 newbuf = vim_strnsave(*fnamep, l);
22363 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022364 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365
22366 vim_free(*bufp);
22367 *fnamep = *bufp = newbuf;
22368
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022369 /* Really should always succeed, as the buffer is big enough. */
22370 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022371 }
22372
22373 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022374 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022375}
22376
22377/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022378 * Get the short path (8.3) for the filename in "fname". The converted
22379 * path is returned in "bufp".
22380 *
22381 * Some of the directories specified in "fname" may not exist. This function
22382 * will shorten the existing directories at the beginning of the path and then
22383 * append the remaining non-existing path.
22384 *
22385 * fname - Pointer to the filename to shorten. On return, contains the
22386 * pointer to the shortened pathname
22387 * bufp - Pointer to an allocated buffer for the filename.
22388 * fnamelen - Length of the filename pointed to by fname
22389 *
22390 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391 */
22392 static int
22393shortpath_for_invalid_fname(fname, bufp, fnamelen)
22394 char_u **fname;
22395 char_u **bufp;
22396 int *fnamelen;
22397{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022398 char_u *short_fname, *save_fname, *pbuf_unused;
22399 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022401 int old_len, len;
22402 int new_len, sfx_len;
22403 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022404
22405 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022406 old_len = *fnamelen;
22407 save_fname = vim_strnsave(*fname, old_len);
22408 pbuf_unused = NULL;
22409 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022411 endp = save_fname + old_len - 1; /* Find the end of the copy */
22412 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022414 /*
22415 * Try shortening the supplied path till it succeeds by removing one
22416 * directory at a time from the tail of the path.
22417 */
22418 len = 0;
22419 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022420 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022421 /* go back one path-separator */
22422 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22423 --endp;
22424 if (endp <= save_fname)
22425 break; /* processed the complete path */
22426
22427 /*
22428 * Replace the path separator with a NUL and try to shorten the
22429 * resulting path.
22430 */
22431 ch = *endp;
22432 *endp = 0;
22433 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022434 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022435 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22436 {
22437 retval = FAIL;
22438 goto theend;
22439 }
22440 *endp = ch; /* preserve the string */
22441
22442 if (len > 0)
22443 break; /* successfully shortened the path */
22444
22445 /* failed to shorten the path. Skip the path separator */
22446 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447 }
22448
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022449 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022451 /*
22452 * Succeeded in shortening the path. Now concatenate the shortened
22453 * path with the remaining path at the tail.
22454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022456 /* Compute the length of the new path. */
22457 sfx_len = (int)(save_endp - endp) + 1;
22458 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022459
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022460 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022461 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022462 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022464 /* There is not enough space in the currently allocated string,
22465 * copy it to a buffer big enough. */
22466 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022468 {
22469 retval = FAIL;
22470 goto theend;
22471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472 }
22473 else
22474 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022475 /* Transfer short_fname to the main buffer (it's big enough),
22476 * unless get_short_pathname() did its work in-place. */
22477 *fname = *bufp = save_fname;
22478 if (short_fname != save_fname)
22479 vim_strncpy(save_fname, short_fname, len);
22480 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022481 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022482
22483 /* concat the not-shortened part of the path */
22484 vim_strncpy(*fname + len, endp, sfx_len);
22485 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022487
22488theend:
22489 vim_free(pbuf_unused);
22490 vim_free(save_fname);
22491
22492 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493}
22494
22495/*
22496 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022497 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498 */
22499 static int
22500shortpath_for_partial(fnamep, bufp, fnamelen)
22501 char_u **fnamep;
22502 char_u **bufp;
22503 int *fnamelen;
22504{
22505 int sepcount, len, tflen;
22506 char_u *p;
22507 char_u *pbuf, *tfname;
22508 int hasTilde;
22509
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022510 /* Count up the path separators from the RHS.. so we know which part
22511 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022513 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022514 if (vim_ispathsep(*p))
22515 ++sepcount;
22516
22517 /* Need full path first (use expand_env() to remove a "~/") */
22518 hasTilde = (**fnamep == '~');
22519 if (hasTilde)
22520 pbuf = tfname = expand_env_save(*fnamep);
22521 else
22522 pbuf = tfname = FullName_save(*fnamep, FALSE);
22523
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022524 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022526 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22527 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022528
22529 if (len == 0)
22530 {
22531 /* Don't have a valid filename, so shorten the rest of the
22532 * path if we can. This CAN give us invalid 8.3 filenames, but
22533 * there's not a lot of point in guessing what it might be.
22534 */
22535 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022536 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22537 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538 }
22539
22540 /* Count the paths backward to find the beginning of the desired string. */
22541 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022542 {
22543#ifdef FEAT_MBYTE
22544 if (has_mbyte)
22545 p -= mb_head_off(tfname, p);
22546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547 if (vim_ispathsep(*p))
22548 {
22549 if (sepcount == 0 || (hasTilde && sepcount == 1))
22550 break;
22551 else
22552 sepcount --;
22553 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022555 if (hasTilde)
22556 {
22557 --p;
22558 if (p >= tfname)
22559 *p = '~';
22560 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022561 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562 }
22563 else
22564 ++p;
22565
22566 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22567 vim_free(*bufp);
22568 *fnamelen = (int)STRLEN(p);
22569 *bufp = pbuf;
22570 *fnamep = p;
22571
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022572 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573}
22574#endif /* WIN3264 */
22575
22576/*
22577 * Adjust a filename, according to a string of modifiers.
22578 * *fnamep must be NUL terminated when called. When returning, the length is
22579 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022580 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 * When there is an error, *fnamep is set to NULL.
22582 */
22583 int
22584modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22585 char_u *src; /* string with modifiers */
22586 int *usedlen; /* characters after src that are used */
22587 char_u **fnamep; /* file name so far */
22588 char_u **bufp; /* buffer for allocated file name or NULL */
22589 int *fnamelen; /* length of fnamep */
22590{
22591 int valid = 0;
22592 char_u *tail;
22593 char_u *s, *p, *pbuf;
22594 char_u dirname[MAXPATHL];
22595 int c;
22596 int has_fullname = 0;
22597#ifdef WIN3264
22598 int has_shortname = 0;
22599#endif
22600
22601repeat:
22602 /* ":p" - full path/file_name */
22603 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22604 {
22605 has_fullname = 1;
22606
22607 valid |= VALID_PATH;
22608 *usedlen += 2;
22609
22610 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22611 if ((*fnamep)[0] == '~'
22612#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22613 && ((*fnamep)[1] == '/'
22614# ifdef BACKSLASH_IN_FILENAME
22615 || (*fnamep)[1] == '\\'
22616# endif
22617 || (*fnamep)[1] == NUL)
22618
22619#endif
22620 )
22621 {
22622 *fnamep = expand_env_save(*fnamep);
22623 vim_free(*bufp); /* free any allocated file name */
22624 *bufp = *fnamep;
22625 if (*fnamep == NULL)
22626 return -1;
22627 }
22628
22629 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022630 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631 {
22632 if (vim_ispathsep(*p)
22633 && p[1] == '.'
22634 && (p[2] == NUL
22635 || vim_ispathsep(p[2])
22636 || (p[2] == '.'
22637 && (p[3] == NUL || vim_ispathsep(p[3])))))
22638 break;
22639 }
22640
22641 /* FullName_save() is slow, don't use it when not needed. */
22642 if (*p != NUL || !vim_isAbsName(*fnamep))
22643 {
22644 *fnamep = FullName_save(*fnamep, *p != NUL);
22645 vim_free(*bufp); /* free any allocated file name */
22646 *bufp = *fnamep;
22647 if (*fnamep == NULL)
22648 return -1;
22649 }
22650
22651 /* Append a path separator to a directory. */
22652 if (mch_isdir(*fnamep))
22653 {
22654 /* Make room for one or two extra characters. */
22655 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22656 vim_free(*bufp); /* free any allocated file name */
22657 *bufp = *fnamep;
22658 if (*fnamep == NULL)
22659 return -1;
22660 add_pathsep(*fnamep);
22661 }
22662 }
22663
22664 /* ":." - path relative to the current directory */
22665 /* ":~" - path relative to the home directory */
22666 /* ":8" - shortname path - postponed till after */
22667 while (src[*usedlen] == ':'
22668 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22669 {
22670 *usedlen += 2;
22671 if (c == '8')
22672 {
22673#ifdef WIN3264
22674 has_shortname = 1; /* Postpone this. */
22675#endif
22676 continue;
22677 }
22678 pbuf = NULL;
22679 /* Need full path first (use expand_env() to remove a "~/") */
22680 if (!has_fullname)
22681 {
22682 if (c == '.' && **fnamep == '~')
22683 p = pbuf = expand_env_save(*fnamep);
22684 else
22685 p = pbuf = FullName_save(*fnamep, FALSE);
22686 }
22687 else
22688 p = *fnamep;
22689
22690 has_fullname = 0;
22691
22692 if (p != NULL)
22693 {
22694 if (c == '.')
22695 {
22696 mch_dirname(dirname, MAXPATHL);
22697 s = shorten_fname(p, dirname);
22698 if (s != NULL)
22699 {
22700 *fnamep = s;
22701 if (pbuf != NULL)
22702 {
22703 vim_free(*bufp); /* free any allocated file name */
22704 *bufp = pbuf;
22705 pbuf = NULL;
22706 }
22707 }
22708 }
22709 else
22710 {
22711 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22712 /* Only replace it when it starts with '~' */
22713 if (*dirname == '~')
22714 {
22715 s = vim_strsave(dirname);
22716 if (s != NULL)
22717 {
22718 *fnamep = s;
22719 vim_free(*bufp);
22720 *bufp = s;
22721 }
22722 }
22723 }
22724 vim_free(pbuf);
22725 }
22726 }
22727
22728 tail = gettail(*fnamep);
22729 *fnamelen = (int)STRLEN(*fnamep);
22730
22731 /* ":h" - head, remove "/file_name", can be repeated */
22732 /* Don't remove the first "/" or "c:\" */
22733 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22734 {
22735 valid |= VALID_HEAD;
22736 *usedlen += 2;
22737 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022738 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022739 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740 *fnamelen = (int)(tail - *fnamep);
22741#ifdef VMS
22742 if (*fnamelen > 0)
22743 *fnamelen += 1; /* the path separator is part of the path */
22744#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022745 if (*fnamelen == 0)
22746 {
22747 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22748 p = vim_strsave((char_u *)".");
22749 if (p == NULL)
22750 return -1;
22751 vim_free(*bufp);
22752 *bufp = *fnamep = tail = p;
22753 *fnamelen = 1;
22754 }
22755 else
22756 {
22757 while (tail > s && !after_pathsep(s, tail))
22758 mb_ptr_back(*fnamep, tail);
22759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760 }
22761
22762 /* ":8" - shortname */
22763 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22764 {
22765 *usedlen += 2;
22766#ifdef WIN3264
22767 has_shortname = 1;
22768#endif
22769 }
22770
22771#ifdef WIN3264
22772 /* Check shortname after we have done 'heads' and before we do 'tails'
22773 */
22774 if (has_shortname)
22775 {
22776 pbuf = NULL;
22777 /* Copy the string if it is shortened by :h */
22778 if (*fnamelen < (int)STRLEN(*fnamep))
22779 {
22780 p = vim_strnsave(*fnamep, *fnamelen);
22781 if (p == 0)
22782 return -1;
22783 vim_free(*bufp);
22784 *bufp = *fnamep = p;
22785 }
22786
22787 /* Split into two implementations - makes it easier. First is where
22788 * there isn't a full name already, second is where there is.
22789 */
22790 if (!has_fullname && !vim_isAbsName(*fnamep))
22791 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022792 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793 return -1;
22794 }
22795 else
22796 {
22797 int l;
22798
22799 /* Simple case, already have the full-name
22800 * Nearly always shorter, so try first time. */
22801 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022802 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022803 return -1;
22804
22805 if (l == 0)
22806 {
22807 /* Couldn't find the filename.. search the paths.
22808 */
22809 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022810 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 return -1;
22812 }
22813 *fnamelen = l;
22814 }
22815 }
22816#endif /* WIN3264 */
22817
22818 /* ":t" - tail, just the basename */
22819 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22820 {
22821 *usedlen += 2;
22822 *fnamelen -= (int)(tail - *fnamep);
22823 *fnamep = tail;
22824 }
22825
22826 /* ":e" - extension, can be repeated */
22827 /* ":r" - root, without extension, can be repeated */
22828 while (src[*usedlen] == ':'
22829 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22830 {
22831 /* find a '.' in the tail:
22832 * - for second :e: before the current fname
22833 * - otherwise: The last '.'
22834 */
22835 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22836 s = *fnamep - 2;
22837 else
22838 s = *fnamep + *fnamelen - 1;
22839 for ( ; s > tail; --s)
22840 if (s[0] == '.')
22841 break;
22842 if (src[*usedlen + 1] == 'e') /* :e */
22843 {
22844 if (s > tail)
22845 {
22846 *fnamelen += (int)(*fnamep - (s + 1));
22847 *fnamep = s + 1;
22848#ifdef VMS
22849 /* cut version from the extension */
22850 s = *fnamep + *fnamelen - 1;
22851 for ( ; s > *fnamep; --s)
22852 if (s[0] == ';')
22853 break;
22854 if (s > *fnamep)
22855 *fnamelen = s - *fnamep;
22856#endif
22857 }
22858 else if (*fnamep <= tail)
22859 *fnamelen = 0;
22860 }
22861 else /* :r */
22862 {
22863 if (s > tail) /* remove one extension */
22864 *fnamelen = (int)(s - *fnamep);
22865 }
22866 *usedlen += 2;
22867 }
22868
22869 /* ":s?pat?foo?" - substitute */
22870 /* ":gs?pat?foo?" - global substitute */
22871 if (src[*usedlen] == ':'
22872 && (src[*usedlen + 1] == 's'
22873 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
22874 {
22875 char_u *str;
22876 char_u *pat;
22877 char_u *sub;
22878 int sep;
22879 char_u *flags;
22880 int didit = FALSE;
22881
22882 flags = (char_u *)"";
22883 s = src + *usedlen + 2;
22884 if (src[*usedlen + 1] == 'g')
22885 {
22886 flags = (char_u *)"g";
22887 ++s;
22888 }
22889
22890 sep = *s++;
22891 if (sep)
22892 {
22893 /* find end of pattern */
22894 p = vim_strchr(s, sep);
22895 if (p != NULL)
22896 {
22897 pat = vim_strnsave(s, (int)(p - s));
22898 if (pat != NULL)
22899 {
22900 s = p + 1;
22901 /* find end of substitution */
22902 p = vim_strchr(s, sep);
22903 if (p != NULL)
22904 {
22905 sub = vim_strnsave(s, (int)(p - s));
22906 str = vim_strnsave(*fnamep, *fnamelen);
22907 if (sub != NULL && str != NULL)
22908 {
22909 *usedlen = (int)(p + 1 - src);
22910 s = do_string_sub(str, pat, sub, flags);
22911 if (s != NULL)
22912 {
22913 *fnamep = s;
22914 *fnamelen = (int)STRLEN(s);
22915 vim_free(*bufp);
22916 *bufp = s;
22917 didit = TRUE;
22918 }
22919 }
22920 vim_free(sub);
22921 vim_free(str);
22922 }
22923 vim_free(pat);
22924 }
22925 }
22926 /* after using ":s", repeat all the modifiers */
22927 if (didit)
22928 goto repeat;
22929 }
22930 }
22931
22932 return valid;
22933}
22934
22935/*
22936 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22937 * "flags" can be "g" to do a global substitute.
22938 * Returns an allocated string, NULL for error.
22939 */
22940 char_u *
22941do_string_sub(str, pat, sub, flags)
22942 char_u *str;
22943 char_u *pat;
22944 char_u *sub;
22945 char_u *flags;
22946{
22947 int sublen;
22948 regmatch_T regmatch;
22949 int i;
22950 int do_all;
22951 char_u *tail;
22952 garray_T ga;
22953 char_u *ret;
22954 char_u *save_cpo;
22955
22956 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22957 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022958 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022959
22960 ga_init2(&ga, 1, 200);
22961
22962 do_all = (flags[0] == 'g');
22963
22964 regmatch.rm_ic = p_ic;
22965 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
22966 if (regmatch.regprog != NULL)
22967 {
22968 tail = str;
22969 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
22970 {
22971 /*
22972 * Get some space for a temporary buffer to do the substitution
22973 * into. It will contain:
22974 * - The text up to where the match is.
22975 * - The substituted text.
22976 * - The text after the match.
22977 */
22978 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
22979 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
22980 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
22981 {
22982 ga_clear(&ga);
22983 break;
22984 }
22985
22986 /* copy the text up to where the match is */
22987 i = (int)(regmatch.startp[0] - tail);
22988 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
22989 /* add the substituted text */
22990 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
22991 + ga.ga_len + i, TRUE, TRUE, FALSE);
22992 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993 /* avoid getting stuck on a match with an empty string */
22994 if (tail == regmatch.endp[0])
22995 {
22996 if (*tail == NUL)
22997 break;
22998 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
22999 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023000 }
23001 else
23002 {
23003 tail = regmatch.endp[0];
23004 if (*tail == NUL)
23005 break;
23006 }
23007 if (!do_all)
23008 break;
23009 }
23010
23011 if (ga.ga_data != NULL)
23012 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23013
23014 vim_free(regmatch.regprog);
23015 }
23016
23017 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23018 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023019 if (p_cpo == empty_option)
23020 p_cpo = save_cpo;
23021 else
23022 /* Darn, evaluating {sub} expression changed the value. */
23023 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023024
23025 return ret;
23026}
23027
23028#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */